feat: added the ability to change the base path for the data files

before starting node, use the command `export SUB_STORE_DATA_BASE_PATH="<YOUR_PATH>"`
This commit is contained in:
Peng-YM 2023-09-04 23:16:13 +08:00
parent ea31b1d0ec
commit 28186f596f

View File

@ -49,18 +49,23 @@ export class OpenAPI {
if (isNode) { if (isNode) {
// create a json for root cache // create a json for root cache
let fpath = 'root.json'; const basePath =
if (!this.node.fs.existsSync(fpath)) { eval('process.env.SUB_STORE_DATA_BASE_PATH') || '.';
this.node.fs.writeFileSync(fpath, JSON.stringify({}), { let rootPath = `${basePath}/root.json`;
this.log(`Root path: ${rootPath}`);
if (!this.node.fs.existsSync(rootPath)) {
this.node.fs.writeFileSync(rootPath, JSON.stringify({}), {
flag: 'wx', flag: 'wx',
}); });
this.root = {}; this.root = {};
} else { } else {
this.root = JSON.parse(this.node.fs.readFileSync(`${fpath}`)); this.root = JSON.parse(this.node.fs.readFileSync(`${rootPath}`));
} }
// create a json file with the given name if not exists // create a json file with the given name if not exists
fpath = `${this.name}.json`; let fpath = `${basePath}/${this.name}.json`;
this.log(`Data path: ${fpath}`);
if (!this.node.fs.existsSync(fpath)) { if (!this.node.fs.existsSync(fpath)) {
this.node.fs.writeFileSync(fpath, JSON.stringify({}), { this.node.fs.writeFileSync(fpath, JSON.stringify({}), {
flag: 'wx', flag: 'wx',
@ -68,7 +73,7 @@ export class OpenAPI {
this.cache = {}; this.cache = {};
} else { } else {
this.cache = JSON.parse( this.cache = JSON.parse(
this.node.fs.readFileSync(`${this.name}.json`), this.node.fs.readFileSync(`${fpath}`),
); );
} }
} }
@ -80,14 +85,17 @@ export class OpenAPI {
if (isQX) $prefs.setValueForKey(data, this.name); if (isQX) $prefs.setValueForKey(data, this.name);
if (isLoon || isSurge) $persistentStore.write(data, this.name); if (isLoon || isSurge) $persistentStore.write(data, this.name);
if (isNode) { if (isNode) {
const basePath =
eval('process.env.SUB_STORE_DATA_BASE_PATH') || '.';
this.node.fs.writeFileSync( this.node.fs.writeFileSync(
`${this.name}.json`, `${basePath}/${this.name}.json`,
data, data,
{ flag: 'w' }, { flag: 'w' },
(err) => console.log(err), (err) => console.log(err),
); );
this.node.fs.writeFileSync( this.node.fs.writeFileSync(
'root.json', `${basePath}/root.json`,
JSON.stringify(this.root, null, 2), JSON.stringify(this.root, null, 2),
{ flag: 'w' }, { flag: 'w' },
(err) => console.log(err), (err) => console.log(err),