2022-04-04 21:49:24 +00:00
|
|
|
//--- Modules/Imports ---//
|
2022-06-22 05:24:06 +00:00
|
|
|
import { Filesystem, Directory, Encoding } from "@capacitor/filesystem";
|
|
|
|
import { fs } from "./constants";
|
2022-04-07 00:23:01 +00:00
|
|
|
|
|
|
|
//--- Set Up App Directory ---//
|
2022-05-30 20:42:55 +00:00
|
|
|
const APP_DIRECTORY = Directory.Data;
|
2022-04-07 00:23:01 +00:00
|
|
|
|
2022-05-30 22:24:37 +00:00
|
|
|
//--- Ensure Plugins Folder ---//
|
2022-04-07 00:23:01 +00:00
|
|
|
const ensureStructure = new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
2022-04-11 01:00:42 +00:00
|
|
|
await Filesystem.mkdir({
|
2022-06-22 05:24:06 +00:00
|
|
|
directory: APP_DIRECTORY,
|
|
|
|
recursive: true,
|
2022-04-07 00:23:01 +00:00
|
|
|
path: fs.plugins,
|
|
|
|
});
|
2022-06-22 05:24:06 +00:00
|
|
|
} catch (e) {
|
|
|
|
/* Exists */
|
|
|
|
}
|
2022-04-07 00:23:01 +00:00
|
|
|
|
2022-05-30 22:24:37 +00:00
|
|
|
resolve();
|
2022-06-22 05:24:06 +00:00
|
|
|
});
|
2022-04-07 00:23:01 +00:00
|
|
|
|
2022-04-04 21:49:24 +00:00
|
|
|
const module = {
|
2022-05-30 17:54:29 +00:00
|
|
|
//--- List Plugins ---//
|
2022-04-07 00:23:01 +00:00
|
|
|
list: new Promise(async (resolve, reject) => {
|
2022-06-15 19:00:26 +00:00
|
|
|
await ensureStructure;
|
2022-04-11 01:00:42 +00:00
|
|
|
|
2022-05-30 17:54:29 +00:00
|
|
|
const plugins = await Filesystem.readdir({
|
2022-05-30 20:42:55 +00:00
|
|
|
path: fs.plugins,
|
2022-06-22 05:24:06 +00:00
|
|
|
directory: APP_DIRECTORY,
|
|
|
|
}).catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
2022-05-30 17:54:29 +00:00
|
|
|
resolve(plugins);
|
2022-05-30 20:42:55 +00:00
|
|
|
}),
|
|
|
|
|
2022-05-31 14:01:40 +00:00
|
|
|
async addPlugin(content) {
|
2022-06-15 19:00:26 +00:00
|
|
|
await ensureStructure;
|
2022-05-30 22:24:37 +00:00
|
|
|
new Promise(async (resolve, reject) => {
|
|
|
|
const fileName = require("./utils").getCpn(); // Im not sure what this is actually meant for but im using it as a random string generator
|
2022-06-22 05:24:06 +00:00
|
|
|
console.log("Saving Plugin As" + fileName);
|
2022-05-30 22:24:37 +00:00
|
|
|
await Filesystem.writeFile({
|
2022-06-22 05:24:06 +00:00
|
|
|
path: fs.plugins + "/" + fileName + ".js",
|
2022-05-30 22:24:37 +00:00
|
|
|
directory: APP_DIRECTORY,
|
|
|
|
data: content,
|
|
|
|
encoding: Encoding.UTF8,
|
|
|
|
});
|
2022-06-22 05:24:06 +00:00
|
|
|
});
|
|
|
|
},
|
2022-04-04 21:49:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//--- Start ---//
|
|
|
|
export default ({ app }, inject) => {
|
|
|
|
inject("tppl", module);
|
|
|
|
};
|