fix(storage): migrate filesystem operations to smartfs and tighten TypeScript initialization checks
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartconfig',
|
||||
version: '6.1.0',
|
||||
version: '6.1.1',
|
||||
description: 'A comprehensive configuration management library providing key-value storage, environment variable mapping, and tool configuration.'
|
||||
}
|
||||
|
||||
@@ -403,7 +403,7 @@ export class AppData<T = any> {
|
||||
// instance
|
||||
public readyDeferred = plugins.smartpromise.defer<void>();
|
||||
public options: IAppDataOptions<T>;
|
||||
private kvStore: KeyValueStore<T>;
|
||||
private kvStore!: KeyValueStore<T>;
|
||||
|
||||
constructor(optionsArg: IAppDataOptions<T> = {}) {
|
||||
this.options = optionsArg;
|
||||
@@ -430,8 +430,8 @@ export class AppData<T = any> {
|
||||
const appDataDir = '/app/data';
|
||||
const dataDir = '/data';
|
||||
const nogitAppData = '.nogit/appdata';
|
||||
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
|
||||
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
|
||||
const appDataExists = plugins.nodeFs.existsSync(appDataDir) && plugins.nodeFs.statSync(appDataDir).isDirectory();
|
||||
const dataExists = plugins.nodeFs.existsSync(dataDir) && plugins.nodeFs.statSync(dataDir).isDirectory();
|
||||
if (appDataExists) {
|
||||
this.options.dirPath = appDataDir;
|
||||
console.log(` 📁 Auto-selected container directory: ${appDataDir}`);
|
||||
@@ -439,7 +439,7 @@ export class AppData<T = any> {
|
||||
this.options.dirPath = dataDir;
|
||||
console.log(` 📁 Auto-selected data directory: ${dataDir}`);
|
||||
} else {
|
||||
await plugins.smartfile.fs.ensureDir(nogitAppData);
|
||||
await plugins.smartFs.directory(nogitAppData).create();
|
||||
this.options.dirPath = nogitAppData;
|
||||
console.log(` 📁 Auto-selected local directory: ${nogitAppData}`);
|
||||
}
|
||||
@@ -494,17 +494,18 @@ export class AppData<T = any> {
|
||||
|
||||
// Apply overwrite object after env mapping
|
||||
if (this.options.overwriteObject) {
|
||||
const overwriteKeys = Object.keys(this.options.overwriteObject);
|
||||
const overwriteObject = this.options.overwriteObject as Record<string, unknown>;
|
||||
const overwriteKeys = Object.keys(overwriteObject);
|
||||
console.log(`🔄 Applying overwriteObject with ${overwriteKeys.length} key(s)...`);
|
||||
|
||||
for (const key of overwriteKeys) {
|
||||
const value = this.options.overwriteObject[key];
|
||||
const value = overwriteObject[key];
|
||||
const valueType = Array.isArray(value) ? 'array' : typeof value;
|
||||
console.log(` 🔧 Overwriting key "${key}" with ${valueType} value`);
|
||||
|
||||
await this.kvStore.writeKey(
|
||||
key as keyof T,
|
||||
value,
|
||||
value as T[keyof T],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+17
-12
@@ -26,21 +26,18 @@ export class KeyValueStore<T = any> {
|
||||
name: 'syncTask',
|
||||
buffered: true,
|
||||
bufferMax: 1,
|
||||
execDelay: 0,
|
||||
taskFunction: async () => {
|
||||
if (this.type !== 'ephemeral') {
|
||||
const storedJson = await plugins.smartFs.file(this.filePath!).encoding('utf8').read() as string;
|
||||
this.dataObject = {
|
||||
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
||||
...plugins.smartjson.parse(storedJson || '{}'),
|
||||
...this.dataObject,
|
||||
};
|
||||
for (const key of Object.keys(this.deletedObject) as Array<keyof T>) {
|
||||
delete this.dataObject[key];
|
||||
}
|
||||
this.deletedObject = {};
|
||||
await plugins.smartfile.memory.toFs(
|
||||
plugins.smartjson.stringifyPretty(this.dataObject),
|
||||
this.filePath,
|
||||
);
|
||||
await plugins.smartFs.file(this.filePath!).encoding('utf8').write(plugins.smartjson.stringifyPretty(this.dataObject));
|
||||
}
|
||||
const newStateString = plugins.smartjson.stringify(this.dataObject);
|
||||
|
||||
@@ -67,13 +64,16 @@ export class KeyValueStore<T = any> {
|
||||
paths.cwd,
|
||||
);
|
||||
this.filePath = absolutePath;
|
||||
if (plugins.smartfile.fs.isDirectorySync(this.filePath)) {
|
||||
if (plugins.nodeFs.existsSync(this.filePath) && plugins.nodeFs.statSync(this.filePath).isDirectory()) {
|
||||
this.filePath = plugins.path.join(
|
||||
this.filePath,
|
||||
this.identity + '.json',
|
||||
);
|
||||
}
|
||||
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
|
||||
plugins.nodeFs.mkdirSync(plugins.path.dirname(this.filePath), { recursive: true });
|
||||
if (!plugins.nodeFs.existsSync(this.filePath)) {
|
||||
plugins.nodeFs.writeFileSync(this.filePath, '{}');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -84,8 +84,10 @@ export class KeyValueStore<T = any> {
|
||||
throw new Error('kv type not supported');
|
||||
}
|
||||
this.filePath = plugins.path.join(baseDir, this.identity + '.json');
|
||||
plugins.smartfile.fs.ensureDirSync(baseDir);
|
||||
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
|
||||
plugins.nodeFs.mkdirSync(baseDir, { recursive: true });
|
||||
if (!plugins.nodeFs.existsSync(this.filePath)) {
|
||||
plugins.nodeFs.writeFileSync(this.filePath, '{}');
|
||||
}
|
||||
};
|
||||
|
||||
// if no custom path is provided, try to store at home directory
|
||||
@@ -162,8 +164,11 @@ export class KeyValueStore<T = any> {
|
||||
*/
|
||||
public async wipe(): Promise<void> {
|
||||
this.dataObject = {};
|
||||
if (this.type !== 'ephemeral') {
|
||||
await plugins.smartfile.fs.remove(this.filePath);
|
||||
if (this.type !== 'ephemeral' && this.filePath) {
|
||||
const file = plugins.smartFs.file(this.filePath);
|
||||
if (await file.exists()) {
|
||||
await file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ import * as paths from './paths.js';
|
||||
*/
|
||||
export class Smartconfig {
|
||||
cwd: string;
|
||||
lookupPath: string;
|
||||
smartconfigJsonExists: boolean;
|
||||
lookupPath!: string;
|
||||
smartconfigJsonExists = false;
|
||||
smartconfigJsonData: any;
|
||||
|
||||
/**
|
||||
@@ -48,9 +48,7 @@ export class Smartconfig {
|
||||
* checks if the JSON exists
|
||||
*/
|
||||
private checkSmartconfigJsonExists() {
|
||||
this.smartconfigJsonExists = plugins.smartfile.fs.fileExistsSync(
|
||||
this.lookupPath,
|
||||
);
|
||||
this.smartconfigJsonExists = plugins.nodeFs.existsSync(this.lookupPath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,8 +67,8 @@ export class Smartconfig {
|
||||
*/
|
||||
private checkSmartconfigJsonData() {
|
||||
if (this.smartconfigJsonExists) {
|
||||
this.smartconfigJsonData = plugins.smartfile.fs.toObjectSync(
|
||||
this.lookupPath,
|
||||
this.smartconfigJsonData = plugins.smartjson.parse(
|
||||
plugins.nodeFs.readFileSync(this.lookupPath, 'utf8'),
|
||||
);
|
||||
} else {
|
||||
this.smartconfigJsonData = {};
|
||||
|
||||
+6
-2
@@ -4,19 +4,23 @@ export { tsclass };
|
||||
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
import * as smartlog from '@push.rocks/smartlog';
|
||||
import * as nodeFs from 'node:fs';
|
||||
import * as path from 'path';
|
||||
import * as smartfile from '@push.rocks/smartfile';
|
||||
import * as smartfs from '@push.rocks/smartfs';
|
||||
import * as smartjson from '@push.rocks/smartjson';
|
||||
import * as smartpath from '@push.rocks/smartpath';
|
||||
import * as smartpromise from '@push.rocks/smartpromise';
|
||||
import * as smartrx from '@push.rocks/smartrx';
|
||||
import * as taskbuffer from '@push.rocks/taskbuffer';
|
||||
|
||||
export const smartFs = new smartfs.SmartFs(new smartfs.SmartFsProviderNode());
|
||||
|
||||
export {
|
||||
qenv,
|
||||
smartlog,
|
||||
nodeFs,
|
||||
path,
|
||||
smartfile,
|
||||
smartfs,
|
||||
smartjson,
|
||||
smartpath,
|
||||
smartpromise,
|
||||
|
||||
Reference in New Issue
Block a user