Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
45deb1a8d2 | |||
4c9d3c7148 | |||
320c627d4f | |||
d834e0a220 | |||
86427ac05d | |||
dc59682c15 | |||
45ebf0944c | |||
677aa4f0ea | |||
7f0985f24d | |||
5f2f7e2b39 | |||
26a6ac9651 | |||
072ee31c3f | |||
8fa3fd8ac1 | |||
102422c9c7 | |||
2e95824ff4 | |||
c85c0d3035 |
13
package.json
13
package.json
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/npmextra",
|
"name": "@push.rocks/npmextra",
|
||||||
"version": "3.0.10",
|
"version": "4.0.4",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "do more with npm",
|
"description": "do more with npm",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/ --web)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(tsbuild --web)",
|
"build": "(tsbuild --web --allowimplicitany)",
|
||||||
"buildDocs": "tsdoc"
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -21,18 +21,19 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/npmextra#README",
|
"homepage": "https://gitlab.com/pushrocks/npmextra#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/smartfile": "^10.0.26",
|
"@push.rocks/smartfile": "^10.0.30",
|
||||||
|
"@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.0.10"
|
"@push.rocks/taskbuffer": "^3.1.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.66",
|
"@gitzone/tsbuild": "^2.1.66",
|
||||||
"@gitzone/tsrun": "^1.2.44",
|
"@gitzone/tsrun": "^1.2.44",
|
||||||
"@gitzone/tstest": "^1.0.77",
|
"@gitzone/tstest": "^1.0.77",
|
||||||
"@push.rocks/tapbundle": "^5.0.8",
|
"@push.rocks/tapbundle": "^5.0.15",
|
||||||
"@types/node": "^20.4.6"
|
"@types/node": "^20.5.4"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
|
1201
pnpm-lock.yaml
generated
1201
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
3
test/somekv.json
Normal file
3
test/somekv.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"myKey": "myValue"
|
||||||
|
}
|
@ -5,10 +5,14 @@ import * as npmextra from '../ts/index.js';
|
|||||||
let myKeyValueStore: npmextra.KeyValueStore;
|
let myKeyValueStore: npmextra.KeyValueStore;
|
||||||
|
|
||||||
tap.test('should create a keyValueStore', async () => {
|
tap.test('should create a keyValueStore', async () => {
|
||||||
myKeyValueStore = new npmextra.KeyValueStore('custom', 'test');
|
myKeyValueStore = new npmextra.KeyValueStore('custom', 'test', 'test/somekv.json');
|
||||||
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.10',
|
version: '4.0.4',
|
||||||
description: 'do more with npm'
|
description: 'do more with npm'
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import * as plugins from './npmextra.plugins.js';
|
export * from './npmextra.classes.appdata.js';
|
||||||
|
|
||||||
export * from './npmextra.classes.npmextra.js';
|
|
||||||
export * from './npmextra.classes.keyvaluestore.js';
|
export * from './npmextra.classes.keyvaluestore.js';
|
||||||
|
export * from './npmextra.classes.npmextra.js';
|
||||||
|
49
ts/npmextra.classes.appdata.ts
Normal file
49
ts/npmextra.classes.appdata.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import * as plugins from './npmextra.plugins.js';
|
||||||
|
import * as paths from './npmextra.paths.js';
|
||||||
|
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
|
||||||
|
|
||||||
|
export class AppData {
|
||||||
|
public static async createAndInit(pathArg?: string) {
|
||||||
|
const appData = new AppData(pathArg);
|
||||||
|
await appData.init();
|
||||||
|
return appData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// instance
|
||||||
|
public readyDeferred = plugins.smartpromise.defer();
|
||||||
|
public dirPathArg: string;
|
||||||
|
private kvStore: KeyValueStore;
|
||||||
|
constructor(pathArg?: string) {
|
||||||
|
this.init(pathArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async init(pathArg?: string) {
|
||||||
|
if (pathArg) {
|
||||||
|
this.dirPathArg = pathArg;
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
|
if (appDataExists) {
|
||||||
|
this.dirPathArg = appDataDir;
|
||||||
|
} else if (dataExists) {
|
||||||
|
this.dirPathArg = dataDir;
|
||||||
|
} else {
|
||||||
|
await plugins.smartfile.fs.ensureDir(nogitAppData);
|
||||||
|
this.dirPathArg = nogitAppData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.kvStore = new KeyValueStore('custom', 'appkv', this.dirPathArg);
|
||||||
|
this.readyDeferred.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns a kvtore that resides in appdata
|
||||||
|
*/
|
||||||
|
public async getKvStore() {
|
||||||
|
await this.readyDeferred.promise;
|
||||||
|
return this.kvStore;
|
||||||
|
}
|
||||||
|
}
|
@ -3,10 +3,10 @@ import * as paths from './npmextra.paths.js';
|
|||||||
|
|
||||||
import { Task } from '@push.rocks/taskbuffer';
|
import { Task } from '@push.rocks/taskbuffer';
|
||||||
|
|
||||||
export type TKeyValueStore = 'path' | 'gitProject' | 'custom';
|
export type TKeyValueStore = 'custom' | 'userHomeDir';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* kvStore is a simple key vlaue 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 {
|
||||||
private dataObject: any = {};
|
private dataObject: any = {};
|
||||||
@ -25,42 +25,61 @@ export class KeyValueStore {
|
|||||||
delete this.dataObject[key];
|
delete this.dataObject[key];
|
||||||
}
|
}
|
||||||
this.deletedObject = {};
|
this.deletedObject = {};
|
||||||
await plugins.smartfile.memory.toFs(JSON.stringify(this.dataObject), this.filePath);
|
await plugins.smartfile.memory.toFs(
|
||||||
|
plugins.smartjson.stringifyPretty(this.dataObject),
|
||||||
|
this.filePath
|
||||||
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* computes the identity
|
* computes the identity and filePath
|
||||||
*/
|
*/
|
||||||
private initFilePath = () => {
|
private initFilePath = () => {
|
||||||
// determine the right base directory
|
if (this.customPath) {
|
||||||
|
// Use custom path if provided
|
||||||
|
const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd);
|
||||||
|
this.filePath = absolutePath;
|
||||||
|
if (plugins.smartfile.fs.isDirectorySync(this.filePath)) {
|
||||||
|
this.filePath = plugins.path.join(this.filePath, this.identity + '.json');
|
||||||
|
}
|
||||||
|
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let baseDir: string;
|
let baseDir: string;
|
||||||
if (this.type === 'custom') {
|
if (this.type === 'userHomeDir') {
|
||||||
baseDir = paths.kvCustomDir;
|
baseDir = paths.kvUserHomeDirBase;
|
||||||
} else if (this.type === 'gitProject') {
|
} else {
|
||||||
baseDir = paths.kvGitDir;
|
throw new Error('kv type not supported');
|
||||||
} else if (this.type === 'path') {
|
|
||||||
baseDir = paths.kvPathDir;
|
|
||||||
}
|
}
|
||||||
this.filePath = plugins.path.join(baseDir, this.identity + '.json');
|
this.filePath = plugins.path.join(baseDir, this.identity + '.json');
|
||||||
plugins.smartfile.fs.ensureDirSync(paths.kvCustomDir);
|
plugins.smartfile.fs.ensureDirSync(baseDir);
|
||||||
plugins.smartfile.fs.ensureDirSync(paths.kvGitDir);
|
|
||||||
plugins.smartfile.fs.ensureDirSync(paths.kvPathDir);
|
|
||||||
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
|
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
|
||||||
};
|
};
|
||||||
|
|
||||||
public type: TKeyValueStore; // the type of the kvStore
|
// if no custom path is provided, try to store at home directory
|
||||||
public identity: string; // the identity of the kvStore
|
public type: TKeyValueStore;
|
||||||
public filePath: string; // the filePath of the kvStore
|
public identity: string;
|
||||||
|
public filePath: string;
|
||||||
|
private customPath?: string; // Optionally allow custom path
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the constructor of keyvalue store
|
* the constructor of keyvalue store
|
||||||
* @param typeArg
|
* @param typeArg
|
||||||
* @param customStringArg
|
* @param identityArg
|
||||||
|
* @param customPath Optional custom path for the keyValue store
|
||||||
*/
|
*/
|
||||||
constructor(typeArg: TKeyValueStore, customStringArg: string) {
|
constructor(typeArg: TKeyValueStore, identityArg: string, customPath?: string) {
|
||||||
// set kvStoreType
|
if (customPath && typeArg !== 'custom') {
|
||||||
|
throw new Error('customPath can only be provided if typeArg is custom');
|
||||||
|
}
|
||||||
|
if (typeArg === 'custom' && !customPath) {
|
||||||
|
throw new Error('customPath must be provided if typeArg is custom');
|
||||||
|
}
|
||||||
this.type = typeArg;
|
this.type = typeArg;
|
||||||
this.identity = customStringArg;
|
this.identity = identityArg;
|
||||||
|
this.customPath = customPath; // Store custom path if provided
|
||||||
this.initFilePath();
|
this.initFilePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +116,7 @@ export class KeyValueStore {
|
|||||||
/**
|
/**
|
||||||
* writes all keyValue pairs in the object argument
|
* writes all keyValue pairs in the object argument
|
||||||
*/
|
*/
|
||||||
public async writeAll(keyValueObject) {
|
public async writeAll(keyValueObject: { [key: string]: any }) {
|
||||||
this.dataObject = { ...this.dataObject, ...keyValueObject };
|
this.dataObject = { ...this.dataObject, ...keyValueObject };
|
||||||
await this.syncTask.trigger();
|
await this.syncTask.trigger();
|
||||||
}
|
}
|
||||||
@ -109,4 +128,19 @@ 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,22 +16,7 @@ export let home = plugins.smartpath.get.home();
|
|||||||
/**
|
/**
|
||||||
* keyValue base path
|
* keyValue base path
|
||||||
*/
|
*/
|
||||||
export let kvBase = plugins.path.join(home, '.npmextra/kv');
|
export let kvUserHomeDirBase = plugins.path.join(home, '.npmextra/kv');
|
||||||
|
|
||||||
/**
|
|
||||||
* the base directory for custom string based key value store
|
|
||||||
*/
|
|
||||||
export let kvCustomDir = plugins.path.join(kvBase, 'custom');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* the subdir for git based keyValue
|
|
||||||
*/
|
|
||||||
export let kvGitDir = plugins.path.join(kvBase, 'git');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* keyValue for path based keyValue store
|
|
||||||
*/
|
|
||||||
export let kvPathDir = plugins.path.join(kvBase, 'path');
|
|
||||||
|
|
||||||
// files
|
// files
|
||||||
export let configFile = plugins.path.join(cwd, 'npmextra.json');
|
export let configFile = plugins.path.join(cwd, 'npmextra.json');
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
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 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 taskbuffer from '@push.rocks/taskbuffer';
|
import * as taskbuffer from '@push.rocks/taskbuffer';
|
||||||
|
|
||||||
export { smartlog, path, smartfile, smartpath, smartpromise, taskbuffer };
|
export { smartlog, path, smartfile, smartjson, smartpath, smartpromise, taskbuffer };
|
||||||
|
Reference in New Issue
Block a user