Compare commits
48 Commits
Author | SHA1 | Date | |
---|---|---|---|
75059dfca8 | |||
90c0f30ce1 | |||
9a55303978 | |||
224125fb8e | |||
df9d197508 | |||
ae33716af4 | |||
31b64eda5e | |||
7f3437e3cd | |||
8903bee78d | |||
a02b45a673 | |||
e7883f5997 | |||
39ab01b4af | |||
bb9de1b13b | |||
080e133e9f | |||
a284c58a68 | |||
18bb54831d | |||
141c7ed8a7 | |||
037481f195 | |||
e414e392d3 | |||
4f7e382bc9 | |||
1c0b428606 | |||
90e8625771 | |||
abbce0d4a1 | |||
93c65acc95 | |||
a68d59a4d2 | |||
0133dca698 | |||
6174490e8e | |||
d952a761b2 | |||
05909f776e | |||
779883fbab | |||
349f074bb9 | |||
d209661586 | |||
3ce6036478 | |||
54bf310403 | |||
2ec485048b | |||
caca370e77 | |||
45deb1a8d2 | |||
4c9d3c7148 | |||
320c627d4f | |||
d834e0a220 | |||
86427ac05d | |||
dc59682c15 | |||
45ebf0944c | |||
677aa4f0ea | |||
7f0985f24d | |||
5f2f7e2b39 | |||
26a6ac9651 | |||
072ee31c3f |
@ -119,6 +119,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
npmci node install stable
|
npmci node install stable
|
||||||
npmci npm install
|
npmci npm install
|
||||||
pnpm install -g @gitzone/tsdoc
|
pnpm install -g @git.zone/tsdoc
|
||||||
npmci command tsdoc
|
npmci command tsdoc
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
|
18
package.json
18
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/npmextra",
|
"name": "@push.rocks/npmextra",
|
||||||
"version": "3.0.12",
|
"version": "5.0.5",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with npm",
|
"description": "do more with npm",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -21,19 +21,21 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/npmextra#README",
|
"homepage": "https://gitlab.com/pushrocks/npmextra#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/smartfile": "^10.0.30",
|
"@push.rocks/qenv": "^6.0.5",
|
||||||
"@push.rocks/smartjson": "^5.0.8",
|
"@push.rocks/smartfile": "^11.0.4",
|
||||||
|
"@push.rocks/smartjson": "^5.0.10",
|
||||||
"@push.rocks/smartlog": "^3.0.2",
|
"@push.rocks/smartlog": "^3.0.2",
|
||||||
"@push.rocks/smartpath": "^5.0.11",
|
"@push.rocks/smartpath": "^5.0.11",
|
||||||
"@push.rocks/smartpromise": "^4.0.2",
|
"@push.rocks/smartpromise": "^4.0.2",
|
||||||
"@push.rocks/taskbuffer": "^3.1.6"
|
"@push.rocks/smartrx": "^3.0.7",
|
||||||
|
"@push.rocks/taskbuffer": "^3.1.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.66",
|
"@git.zone/tsbuild": "^2.1.66",
|
||||||
"@gitzone/tsrun": "^1.2.44",
|
"@git.zone/tsrun": "^1.2.44",
|
||||||
"@gitzone/tstest": "^1.0.77",
|
"@git.zone/tstest": "^1.0.77",
|
||||||
"@push.rocks/tapbundle": "^5.0.15",
|
"@push.rocks/tapbundle": "^5.0.15",
|
||||||
"@types/node": "^20.5.3"
|
"@types/node": "^20.11.17"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
|
2516
pnpm-lock.yaml
generated
2516
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1 +1,3 @@
|
|||||||
{"myKey":"myValue"}
|
{
|
||||||
|
"myKey": "myValue"
|
||||||
|
}
|
@ -9,6 +9,10 @@ tap.test('should create a keyValueStore', async () => {
|
|||||||
expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore);
|
expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should reset the keyValueStore', async () => {
|
||||||
|
await myKeyValueStore.reset();
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('expect result to be empty', async () => {
|
tap.test('expect result to be empty', async () => {
|
||||||
let result = await myKeyValueStore.readAll();
|
let result = await myKeyValueStore.readAll();
|
||||||
expect(JSON.stringify(result)).toEqual('{}');
|
expect(JSON.stringify(result)).toEqual('{}');
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/npmextra',
|
name: '@push.rocks/npmextra',
|
||||||
version: '3.0.12',
|
version: '5.0.5',
|
||||||
description: 'do more with npm'
|
description: 'do more with npm'
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,111 @@
|
|||||||
import * as plugins from './npmextra.plugins.js';
|
import * as plugins from './npmextra.plugins.js';
|
||||||
import * as paths from './npmextra.paths.js';
|
import * as paths from './npmextra.paths.js';
|
||||||
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
|
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
|
||||||
|
import { env } from 'process';
|
||||||
|
|
||||||
export class AppData {
|
export interface IAppDataOptions {
|
||||||
public readyDeferred = plugins.smartpromise.defer();
|
dirPath?: string;
|
||||||
public dirPathArg: string;
|
requiredKeys?: string[];
|
||||||
private kvStore: KeyValueStore;
|
|
||||||
constructor(pathArg?: string) {
|
/**
|
||||||
this.init(pathArg);
|
* kvStoreKey: 'MY_ENV_VAR'
|
||||||
|
*/
|
||||||
|
envMapping?: {
|
||||||
|
[key: string]: string | object;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class AppData<T = any> {
|
||||||
|
/**
|
||||||
|
* creates appdata. If no pathArg is given, data will be stored here:
|
||||||
|
* ${PWD}/.nogit/appdata
|
||||||
|
* @param pathArg
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
public static async createAndInit<T = any>(optionsArg: IAppDataOptions = {}): Promise<AppData<T>> {
|
||||||
|
const appData = new AppData<T>(optionsArg);
|
||||||
|
await appData.readyDeferred.promise;
|
||||||
|
return appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// instance
|
||||||
|
public readyDeferred = plugins.smartpromise.defer();
|
||||||
|
public options: IAppDataOptions;
|
||||||
|
private kvStore: KeyValueStore<T>;
|
||||||
|
constructor(optionsArg: IAppDataOptions = {}) {
|
||||||
|
this.options = optionsArg;
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inits app data
|
||||||
|
* @param pathArg
|
||||||
|
*/
|
||||||
private async init(pathArg?: string) {
|
private async init(pathArg?: string) {
|
||||||
if (pathArg) {
|
if (this.options.dirPath) {
|
||||||
this.dirPathArg = pathArg;
|
// ok, nothing to do here;
|
||||||
} else {
|
} else {
|
||||||
const appDataDir = '/app/data';
|
const appDataDir = '/app/data';
|
||||||
const dataDir = '/data';
|
const dataDir = '/data';
|
||||||
|
const nogitAppData = '.nogit/appdata';
|
||||||
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
|
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
|
||||||
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
|
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
|
||||||
if (appDataExists) {
|
if (appDataExists) {
|
||||||
this.dirPathArg = appDataDir;
|
this.options.dirPath = appDataDir;
|
||||||
this.dirPathArg = dataDir;
|
} else if (dataExists) {
|
||||||
|
this.options.dirPath = dataDir;
|
||||||
|
} else {
|
||||||
|
await plugins.smartfile.fs.ensureDir(nogitAppData);
|
||||||
|
this.options.dirPath = nogitAppData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.kvStore = new KeyValueStore('custom', 'appkv', this.dirPathArg);
|
this.kvStore = new KeyValueStore({
|
||||||
|
typeArg: 'custom',
|
||||||
|
identityArg: 'appkv',
|
||||||
|
customPath: this.options.dirPath,
|
||||||
|
mandatoryKeys: this.options.requiredKeys
|
||||||
|
});
|
||||||
|
|
||||||
|
if (this.options.envMapping) {
|
||||||
|
const qenvInstance = new plugins.qenv.Qenv();
|
||||||
|
|
||||||
|
// Recursive function to handle nested objects, now includes key parameter
|
||||||
|
const processEnvMapping = async (key: string, mappingValue: any, parentKey: string = ''): Promise<any> => {
|
||||||
|
if (typeof mappingValue === 'string') {
|
||||||
|
let envValue = await qenvInstance.getEnvVarOnDemand(mappingValue);
|
||||||
|
if (envValue) {
|
||||||
|
if (mappingValue.endsWith('_JSON')) {
|
||||||
|
envValue = JSON.parse(envValue);
|
||||||
|
}
|
||||||
|
// Determine the correct key to use (top-level or nested)
|
||||||
|
const effectiveKey = parentKey || key;
|
||||||
|
await this.kvStore.writeKey(effectiveKey, envValue);
|
||||||
|
}
|
||||||
|
} else if (typeof mappingValue === 'object' && mappingValue !== null) {
|
||||||
|
const resultObject = {};
|
||||||
|
for (const innerKey in mappingValue) {
|
||||||
|
const nestedValue = mappingValue[innerKey];
|
||||||
|
// For nested objects, call recursively but do not immediately write to kvStore
|
||||||
|
const nestedResult = await processEnvMapping(innerKey, nestedValue, key);
|
||||||
|
resultObject[innerKey] = nestedResult;
|
||||||
|
}
|
||||||
|
if (parentKey === '') {
|
||||||
|
// Only write to kvStore if at the top level
|
||||||
|
await this.kvStore.writeKey(key, resultObject);
|
||||||
|
} else {
|
||||||
|
// For nested objects, return the constructed object instead of writing to kvStore
|
||||||
|
return resultObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const key in this.options.envMapping) {
|
||||||
|
await processEnvMapping(key, this.options.envMapping[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
this.readyDeferred.resolve();
|
this.readyDeferred.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,4 +116,24 @@ export class AppData {
|
|||||||
await this.readyDeferred.promise;
|
await this.readyDeferred.promise;
|
||||||
return this.kvStore;
|
return this.kvStore;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public async logMissingKeys() {
|
||||||
|
const kvStore = await this.getKvStore();
|
||||||
|
const missingMandatoryKeys = kvStore.getMissingMandatoryKeys();
|
||||||
|
if (missingMandatoryKeys.length > 0) {
|
||||||
|
console.log(
|
||||||
|
`The following mandatory keys are missing in the appdata:\n -> ${missingMandatoryKeys.join(
|
||||||
|
',\n -> '
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.log('All mandatory keys are present in the appdata');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async waitForAndGetKey(keyArg: string) {
|
||||||
|
await this.readyDeferred.promise;
|
||||||
|
await this.kvStore.waitForKeysPresent([keyArg]);
|
||||||
|
return this.kvStore.readKey[keyArg];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5,18 +5,30 @@ import { Task } from '@push.rocks/taskbuffer';
|
|||||||
|
|
||||||
export type TKeyValueStore = 'custom' | 'userHomeDir';
|
export type TKeyValueStore = 'custom' | 'userHomeDir';
|
||||||
|
|
||||||
|
export interface IKvStoreConstructorOptions {
|
||||||
|
typeArg: TKeyValueStore;
|
||||||
|
identityArg: string;
|
||||||
|
customPath?: string;
|
||||||
|
mandatoryKeys?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* kvStore is a simple key value store to store data about projects between runs
|
* kvStore is a simple key value store to store data about projects between runs
|
||||||
*/
|
*/
|
||||||
export class KeyValueStore {
|
export class KeyValueStore<T = any> {
|
||||||
private dataObject: any = {};
|
private dataObject: Partial<T> = {};
|
||||||
private deletedObject: any = {};
|
private deletedObject: any = {};
|
||||||
|
private mandatoryKeys: Set<string> = new Set();
|
||||||
|
public changeSubject = new plugins.smartrx.rxjs.Subject();
|
||||||
|
|
||||||
|
private storedStateString: string = '';
|
||||||
public syncTask = new Task({
|
public syncTask = new Task({
|
||||||
name: 'syncTask',
|
name: 'syncTask',
|
||||||
buffered: true,
|
buffered: true,
|
||||||
bufferMax: 1,
|
bufferMax: 1,
|
||||||
execDelay: 0,
|
execDelay: 0,
|
||||||
taskFunction: async () => {
|
taskFunction: async () => {
|
||||||
|
|
||||||
this.dataObject = {
|
this.dataObject = {
|
||||||
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
||||||
...this.dataObject,
|
...this.dataObject,
|
||||||
@ -25,16 +37,27 @@ export class KeyValueStore {
|
|||||||
delete this.dataObject[key];
|
delete this.dataObject[key];
|
||||||
}
|
}
|
||||||
this.deletedObject = {};
|
this.deletedObject = {};
|
||||||
await plugins.smartfile.memory.toFs(plugins.smartjson.stringify(this.dataObject), this.filePath);
|
await plugins.smartfile.memory.toFs(
|
||||||
|
plugins.smartjson.stringifyPretty(this.dataObject),
|
||||||
|
this.filePath
|
||||||
|
);
|
||||||
|
const newStateString = plugins.smartjson.stringify(this.dataObject);
|
||||||
|
|
||||||
|
// change detection
|
||||||
|
if (newStateString !== this.storedStateString) {
|
||||||
|
this.storedStateString = newStateString;
|
||||||
|
this.changeSubject.next(this.dataObject);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* computes the identity and filePath
|
* computes the identity and filePath
|
||||||
*/
|
*/
|
||||||
private initFilePath = () => {
|
private initFilePath = () => {
|
||||||
if (this.customPath) { // Use custom path if provided
|
if (this.customPath) {
|
||||||
const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd)
|
// Use custom path if provided
|
||||||
|
const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd);
|
||||||
this.filePath = absolutePath;
|
this.filePath = absolutePath;
|
||||||
if (plugins.smartfile.fs.isDirectorySync(this.filePath)) {
|
if (plugins.smartfile.fs.isDirectorySync(this.filePath)) {
|
||||||
this.filePath = plugins.path.join(this.filePath, this.identity + '.json');
|
this.filePath = plugins.path.join(this.filePath, this.identity + '.json');
|
||||||
@ -42,7 +65,7 @@ export class KeyValueStore {
|
|||||||
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
|
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let baseDir: string;
|
let baseDir: string;
|
||||||
if (this.type === 'userHomeDir') {
|
if (this.type === 'userHomeDir') {
|
||||||
baseDir = paths.kvUserHomeDirBase;
|
baseDir = paths.kvUserHomeDirBase;
|
||||||
@ -66,17 +89,20 @@ export class KeyValueStore {
|
|||||||
* @param identityArg
|
* @param identityArg
|
||||||
* @param customPath Optional custom path for the keyValue store
|
* @param customPath Optional custom path for the keyValue store
|
||||||
*/
|
*/
|
||||||
constructor(typeArg: TKeyValueStore, identityArg: string, customPath?: string) {
|
constructor(optionsArg: IKvStoreConstructorOptions) {
|
||||||
if (customPath && typeArg !== 'custom') {
|
if (optionsArg.customPath && optionsArg.typeArg !== 'custom') {
|
||||||
throw new Error('customPath can only be provided if typeArg is custom');
|
throw new Error('customPath can only be provided if typeArg is custom');
|
||||||
}
|
}
|
||||||
if (typeArg === 'custom' && !customPath) {
|
if (optionsArg.typeArg === 'custom' && !optionsArg.customPath) {
|
||||||
throw new Error('customPath must be provided if typeArg is custom');
|
throw new Error('customPath must be provided if typeArg is custom');
|
||||||
}
|
}
|
||||||
this.type = typeArg;
|
this.type = optionsArg.typeArg;
|
||||||
this.identity = identityArg;
|
this.identity = optionsArg.identityArg;
|
||||||
this.customPath = customPath; // Store custom path if provided
|
this.customPath = optionsArg.customPath; // Store custom path if provided
|
||||||
this.initFilePath();
|
this.initFilePath();
|
||||||
|
if (optionsArg.mandatoryKeys) {
|
||||||
|
this.setMandatoryKeys(optionsArg.mandatoryKeys);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -124,4 +150,43 @@ export class KeyValueStore {
|
|||||||
this.dataObject = {};
|
this.dataObject = {};
|
||||||
await plugins.smartfile.fs.remove(this.filePath);
|
await plugins.smartfile.fs.remove(this.filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* resets the KeyValueStore to the initial state by syncing first, deleting all keys, and then triggering a sync again
|
||||||
|
*/
|
||||||
|
public async reset() {
|
||||||
|
await this.syncTask.trigger(); // Sync to get the latest state
|
||||||
|
|
||||||
|
// Delete all keys from the dataObject and add them to deletedObject
|
||||||
|
for (const key of Object.keys(this.dataObject)) {
|
||||||
|
this.deletedObject[key] = this.dataObject[key];
|
||||||
|
delete this.dataObject[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.syncTask.trigger(); // Sync again to reflect the deletion
|
||||||
|
}
|
||||||
|
|
||||||
|
private setMandatoryKeys(keys: string[]) {
|
||||||
|
keys.forEach(key => this.mandatoryKeys.add(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
public getMissingMandatoryKeys(): string[] {
|
||||||
|
return Array.from(this.mandatoryKeys).filter(key => !(key in this.dataObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async waitForKeysPresent(keysArg: string[]): Promise<void> {
|
||||||
|
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||||
|
if (missingKeys.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const subscription = this.changeSubject.subscribe(() => {
|
||||||
|
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||||
|
if (missingKeys.length === 0) {
|
||||||
|
subscription.unsubscribe();
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
|
import * as qenv from '@push.rocks/qenv';
|
||||||
import * as smartlog from '@push.rocks/smartlog';
|
import * as smartlog from '@push.rocks/smartlog';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as smartfile from '@push.rocks/smartfile';
|
import * as smartfile from '@push.rocks/smartfile';
|
||||||
import * as smartjson from '@push.rocks/smartjson';
|
import * as smartjson from '@push.rocks/smartjson';
|
||||||
import * as smartpath from '@push.rocks/smartpath';
|
import * as smartpath from '@push.rocks/smartpath';
|
||||||
import * as smartpromise from '@push.rocks/smartpromise';
|
import * as smartpromise from '@push.rocks/smartpromise';
|
||||||
|
import * as smartrx from '@push.rocks/smartrx';
|
||||||
import * as taskbuffer from '@push.rocks/taskbuffer';
|
import * as taskbuffer from '@push.rocks/taskbuffer';
|
||||||
|
|
||||||
export { smartlog, path, smartfile, smartjson, smartpath, smartpromise, taskbuffer };
|
export { qenv, smartlog, path, smartfile, smartjson, smartpath, smartpromise, smartrx, taskbuffer };
|
||||||
|
@ -3,9 +3,12 @@
|
|||||||
"experimentalDecorators": true,
|
"experimentalDecorators": true,
|
||||||
"useDefineForClassFields": false,
|
"useDefineForClassFields": false,
|
||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "ES2022",
|
"module": "NodeNext",
|
||||||
"moduleResolution": "nodenext",
|
"moduleResolution": "NodeNext",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"verbatimModuleSyntax": true,
|
"verbatimModuleSyntax": true
|
||||||
}
|
},
|
||||||
|
"exclude": [
|
||||||
|
"dist_*/**/*.d.ts"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user