2022-04-04 21:49:24 +00:00
|
|
|
//--- Modules/Imports ---//
|
2022-04-07 00:23:01 +00:00
|
|
|
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
|
|
|
|
import { fs } from './constants';
|
|
|
|
|
|
|
|
//--- 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({
|
|
|
|
directory: APP_DIRECTORY, recursive: true,
|
2022-04-07 00:23:01 +00:00
|
|
|
path: fs.plugins,
|
|
|
|
});
|
|
|
|
} catch (e) { /* Exists */ }
|
|
|
|
|
2022-05-30 22:24:37 +00:00
|
|
|
resolve();
|
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-05-30 22:24:37 +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-05-30 17:54:29 +00:00
|
|
|
directory: APP_DIRECTORY
|
2022-05-30 22:24:37 +00:00
|
|
|
}).catch(err => { reject(err) })
|
2022-05-30 17:54:29 +00:00
|
|
|
resolve(plugins);
|
2022-04-07 00:23:01 +00:00
|
|
|
|
2022-05-30 20:42:55 +00:00
|
|
|
}),
|
|
|
|
|
2022-06-03 22:45:34 +00:00
|
|
|
|
2022-05-31 14:01:40 +00:00
|
|
|
async addPlugin(content) {
|
2022-06-03 22:45:34 +00:00
|
|
|
|
2022-05-30 22:24:37 +00:00
|
|
|
await ensureStructure();
|
|
|
|
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
|
|
|
|
console.log("Saving Plugin As"+ fileName)
|
|
|
|
await Filesystem.writeFile({
|
|
|
|
path: fs.plugins+"/"+fileName+".js",
|
|
|
|
directory: APP_DIRECTORY,
|
|
|
|
data: content,
|
|
|
|
encoding: Encoding.UTF8,
|
|
|
|
});
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
2022-04-07 00:23:01 +00:00
|
|
|
|
2022-04-04 21:49:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
//--- Start ---//
|
|
|
|
export default ({ app }, inject) => {
|
|
|
|
inject("tppl", module);
|
|
|
|
};
|