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-07-07 04:25:53 +00:00
|
|
|
Filesystem.readdir({
|
2022-05-30 20:42:55 +00:00
|
|
|
path: fs.plugins,
|
2022-06-22 05:24:06 +00:00
|
|
|
directory: APP_DIRECTORY,
|
2022-07-07 04:25:53 +00:00
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
resolve(res);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2022-06-22 05:24:06 +00:00
|
|
|
reject(err);
|
|
|
|
});
|
2022-07-07 04:25:53 +00:00
|
|
|
|
|
|
|
|
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-07-07 04:25:53 +00:00
|
|
|
path: fs.plugins + fileName + ".js",
|
2022-05-30 22:24:37 +00:00
|
|
|
directory: APP_DIRECTORY,
|
|
|
|
data: content,
|
|
|
|
encoding: Encoding.UTF8,
|
|
|
|
});
|
2022-07-12 20:05:54 +00:00
|
|
|
resolve();
|
2022-06-22 05:24:06 +00:00
|
|
|
});
|
|
|
|
},
|
2022-07-12 20:05:54 +00:00
|
|
|
|
|
|
|
async removePlugin(name) {
|
|
|
|
await ensureStructure;
|
|
|
|
new Promise(async (resolve, reject) => {
|
|
|
|
await Filesystem.deleteFile({
|
|
|
|
path: fs.plugins + name,
|
|
|
|
directory: APP_DIRECTORY,
|
|
|
|
});
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-04-04 21:49:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//--- Start ---//
|
|
|
|
export default ({ app }, inject) => {
|
|
|
|
inject("tppl", module);
|
|
|
|
};
|