Compare commits
66 Commits
Author | SHA1 | Date | |
---|---|---|---|
b342602ca0 | |||
bb72e1c2c0 | |||
40ad00e75a | |||
de1db395cd | |||
83968d578c | |||
155cd8bd6a | |||
5eebd0fd02 | |||
686aa0d2e7 | |||
f97d72da28 | |||
78d1540ce0 | |||
916dd116c1 | |||
5fd57cb56b | |||
1a106e322c | |||
36ded40032 | |||
44f5f84195 | |||
72655a6a0e | |||
961df11121 | |||
ee6f5e63ee | |||
a40b6adeef | |||
6ee324a0ef | |||
215a48a409 | |||
aeaa6149e4 | |||
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 |
@ -119,6 +119,6 @@ jobs:
|
||||
run: |
|
||||
npmci node install stable
|
||||
npmci npm install
|
||||
pnpm install -g @gitzone/tsdoc
|
||||
pnpm install -g @git.zone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
||||
|
@ -11,12 +11,27 @@
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "npmextra",
|
||||
"description": "do more with npm",
|
||||
"description": "Enhances npm with additional configuration and tool management capabilities, including a key-value store for project setups.",
|
||||
"npmPackagename": "@push.rocks/npmextra",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"npm",
|
||||
"configuration management",
|
||||
"tool management",
|
||||
"key-value store",
|
||||
"project setup",
|
||||
"npm scripts",
|
||||
"typescript",
|
||||
"environment setup",
|
||||
"dependencies management",
|
||||
"npm package enhancement"
|
||||
]
|
||||
}
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
}
|
||||
}
|
44
package.json
44
package.json
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@push.rocks/npmextra",
|
||||
"version": "4.0.0",
|
||||
"version": "5.0.14",
|
||||
"private": false,
|
||||
"description": "do more with npm",
|
||||
"description": "Enhances npm with additional configuration and tool management capabilities, including a key-value store for project setups.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"scripts": {
|
||||
@ -12,28 +12,30 @@
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@gitlab.com/pushrocks/npmextra.git"
|
||||
"url": "https://code.foss.global/push.rocks/npmextra.git"
|
||||
},
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/pushrocks/npmextra/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/npmextra#README",
|
||||
"homepage": "https://code.foss.global/push.rocks/npmextra",
|
||||
"dependencies": {
|
||||
"@push.rocks/smartfile": "^10.0.30",
|
||||
"@push.rocks/smartjson": "^5.0.8",
|
||||
"@push.rocks/smartlog": "^3.0.2",
|
||||
"@push.rocks/smartpath": "^5.0.11",
|
||||
"@push.rocks/qenv": "^6.0.5",
|
||||
"@push.rocks/smartfile": "^11.0.20",
|
||||
"@push.rocks/smartjson": "^5.0.20",
|
||||
"@push.rocks/smartlog": "^3.0.7",
|
||||
"@push.rocks/smartpath": "^5.0.18",
|
||||
"@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": {
|
||||
"@gitzone/tsbuild": "^2.1.66",
|
||||
"@gitzone/tsrun": "^1.2.44",
|
||||
"@gitzone/tstest": "^1.0.77",
|
||||
"@push.rocks/tapbundle": "^5.0.15",
|
||||
"@types/node": "^20.5.3"
|
||||
"@git.zone/tsbuild": "^2.1.80",
|
||||
"@git.zone/tsrun": "^1.2.44",
|
||||
"@git.zone/tstest": "^1.0.90",
|
||||
"@push.rocks/tapbundle": "^5.0.23",
|
||||
"@types/node": "^20.14.2"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
@ -50,5 +52,17 @@
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
],
|
||||
"type": "module"
|
||||
"type": "module",
|
||||
"keywords": [
|
||||
"npm",
|
||||
"configuration management",
|
||||
"tool management",
|
||||
"key-value store",
|
||||
"project setup",
|
||||
"npm scripts",
|
||||
"typescript",
|
||||
"environment setup",
|
||||
"dependencies management",
|
||||
"npm package enhancement"
|
||||
]
|
||||
}
|
||||
|
9456
pnpm-lock.yaml
generated
9456
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
0
readme.hints.md
Normal file
0
readme.hints.md
Normal file
145
readme.md
145
readme.md
@ -1,70 +1,129 @@
|
||||
# @push.rocks/npmextra
|
||||
do more with npm
|
||||
Enhances npm with additional configuration and tool management capabilities.
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/npmextra)
|
||||
* [gitlab.com (source)](https://gitlab.com/push.rocks/npmextra)
|
||||
* [github.com (source mirror)](https://github.com/push.rocks/npmextra)
|
||||
* [docs (typedoc)](https://push.rocks.gitlab.io/npmextra/)
|
||||
## Install
|
||||
To install `@push.rocks/npmextra`, use the following npm command:
|
||||
|
||||
## Status for master
|
||||
```bash
|
||||
npm install @push.rocks/npmextra --save
|
||||
```
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
This package is available on [npm](https://www.npmjs.com/package/@push.rocks/npmextra) and can be installed into your project as a dependency to enhance npm with additional configuration and tool management capabilities.
|
||||
|
||||
## Usage
|
||||
`@push.rocks/npmextra` is designed to supplement npm functionalities with enhanced configuration and tool management. It facilitates managing project configurations and tool setups in a consolidated manner, enabling a smoother workflow and maintenance process. Below are detailed use cases and examples implemented with ESM syntax and TypeScript.
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
### Initial Setup and Configuration
|
||||
To start using npmextra in your project, first include it in your project with an import statement:
|
||||
|
||||
## Using npmextra for managing toolconfigs
|
||||
```typescript
|
||||
import { Npmextra } from '@push.rocks/npmextra';
|
||||
```
|
||||
|
||||
The main purpose of npmextra is to configure different tools using one npmextra.json. This way complex setups avoid additional complexity caused by too many configuration files.
|
||||
Instantiate the `Npmextra` class optionally with a custom path to your project's working directory. If no path is provided, the current working directory (`process.cwd()`) is used.
|
||||
|
||||
npmextra.json
|
||||
```typescript
|
||||
const npmExtraInstance = new Npmextra('/path/to/your/project');
|
||||
```
|
||||
|
||||
### Managing Tool Configurations with `npmextra.json`
|
||||
`@push.rocks/npmextra` excels in unifying tool configurations through a single `npmextra.json` file. Instead of scattering configurations across multiple files, `@push.rocks/npmextra` enables you to define tool-specific settings within this centralized configuration file.
|
||||
|
||||
#### Creating and Utilizing `npmextra.json`
|
||||
|
||||
Create a `npmextra.json` in your project root with the following structure:
|
||||
|
||||
```json
|
||||
{
|
||||
"sometool": {
|
||||
"defaultKey1": "awesomeValueFromConfig"
|
||||
"toolname": {
|
||||
"setting1": "value1",
|
||||
"setting2": "value2"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```typescript
|
||||
import { Npmextra } from 'npmextra';
|
||||
For example, to configure a hypothetical tool named `toolname`, define its settings as shown above.
|
||||
|
||||
let myNpmExtra = new Npmextra('my/path/to/cwd'); // cwd argument is optional
|
||||
mergedData = myNpmExtra.dataFor('sometool', {
|
||||
// gets merged with whatever is in the configfile
|
||||
defaultKey1: 'defaultValue1', // so this will get overwritten with "awesomeValueFromConfig"
|
||||
defaultKey2: 'defaultValue2', // this one will pass through unaltered
|
||||
#### Accessing Configuration in Your Project
|
||||
|
||||
With the configuration defined, you can easily access these settings in your TypeScript code as follows:
|
||||
|
||||
```typescript
|
||||
// import the npmextra module
|
||||
import { Npmextra } from '@push.rocks/npmextra';
|
||||
|
||||
// create an instance pointing at the current directory
|
||||
const npmExtraInstance = new Npmextra();
|
||||
|
||||
// retrieve the configuration for 'toolname', merging defaults with any found in npmextra.json
|
||||
const toolConfig = npmExtraInstance.dataFor('toolname', {
|
||||
defaultKey1: 'defaultValue1',
|
||||
defaultKey2: 'defaultValue2'
|
||||
});
|
||||
|
||||
// toolConfig now contains the merged settings from npmextra.json and provided defaults
|
||||
console.log(toolConfig);
|
||||
```
|
||||
|
||||
### Tools that already use the config feature of npmextra
|
||||
### Key-Value Store Management
|
||||
`@push.rocks/npmextra` also includes a Key-Value Store (KeyValueStore) functionality enabling persistent storage of key-value pairs between script executions.
|
||||
|
||||
- [npmts](https://www.npmjs.com/package/npmts)
|
||||
- [npmci](https://www.npmjs.com/package/npmci)
|
||||
- [npmdocker](https://www.npmjs.com/package/npmdocker)
|
||||
#### Setting Up KeyValueStore
|
||||
|
||||
## Using the KeyValueStore
|
||||
To utilize the KeyValueStore, create an instance specifying its scope (e.g., 'userHomeDir') and a unique identity for your application or tool:
|
||||
|
||||
## Contribution
|
||||
```typescript
|
||||
import { KeyValueStore } from '@push.rocks/npmextra';
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
const kvStore = new KeyValueStore<'userHomeDir'>('userHomeDir', 'myUniqueAppName');
|
||||
```
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
You can then use the `writeKey`, `readKey`, `writeAll`, and `readAll` methods to manage your store respectively.
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
#### Example: Storing and Retrieving Data
|
||||
|
||||
```typescript
|
||||
// Write a single key-value pair
|
||||
await kvStore.writeKey('username', 'johnDoe');
|
||||
|
||||
// Read a single key
|
||||
const username = await kvStore.readKey('username');
|
||||
console.log(username); // Outputs: johnDoe
|
||||
|
||||
// Write multiple key-value pairs
|
||||
await kvStore.writeAll({
|
||||
email: 'john@example.com',
|
||||
isAdmin: true
|
||||
});
|
||||
|
||||
// Read all key-value pairs
|
||||
const allData = await kvStore.readAll();
|
||||
console.log(allData); // Outputs the entire store's contents
|
||||
```
|
||||
|
||||
### Integrating with Tools
|
||||
`@push.rocks/npmextra` seamlessly integrates with numerous tools, enabling them to leverage `npmextra.json` for configuration purposes. Tools such as `npmts`, `npmci`, and `npmdocker` are already utilizing this feature for enhanced configuration management.
|
||||
|
||||
For tool developers, integrating with `npmextra` requires reading the tool-specific configuration from `npmextra.json` and adjusting the tool's behavior based on these settings. This creates a unified and streamlined configuration process across different tools used within a project.
|
||||
|
||||
### Conclusion
|
||||
By centralizing configuration management and offering a versatile key-value store, `@push.rocks/npmextra` significantly simplifies the setup and management of tools and settings in modern JavaScript and TypeScript projects. Whether you're managing project-wide configurations or need persistent storage for key-value pairs, `@push.rocks/npmextra` provides an efficient and streamlined solution.
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
|
@ -1 +1,3 @@
|
||||
{"myKey":"myValue"}
|
||||
{
|
||||
"myKey": "myValue"
|
||||
}
|
@ -2,10 +2,14 @@ import { expect, tap } from '@push.rocks/tapbundle';
|
||||
|
||||
import * as npmextra from '../ts/index.js';
|
||||
|
||||
let myKeyValueStore: npmextra.KeyValueStore;
|
||||
let myKeyValueStore: npmextra.KeyValueStore<any>;
|
||||
|
||||
tap.test('should create a keyValueStore', async () => {
|
||||
myKeyValueStore = new npmextra.KeyValueStore('custom', 'test', 'test/somekv.json');
|
||||
myKeyValueStore = new npmextra.KeyValueStore<any>({
|
||||
typeArg: 'custom',
|
||||
identityArg: 'test',
|
||||
customPath: 'test/somekv.json',
|
||||
});
|
||||
expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore);
|
||||
});
|
||||
|
||||
@ -25,4 +29,4 @@ tap.test('expect to add an object to the kv Store', async () => {
|
||||
await expect(await myKeyValueStore.readKey('myKey')).toEqual('myValue');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
tap.start();
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/npmextra',
|
||||
version: '4.0.0',
|
||||
description: 'do more with npm'
|
||||
version: '5.0.14',
|
||||
description: 'Enhances npm with additional configuration and tool management capabilities, including a key-value store for project setups.'
|
||||
}
|
||||
|
@ -1,37 +1,149 @@
|
||||
import * as plugins from './npmextra.plugins.js';
|
||||
import * as paths from './npmextra.paths.js';
|
||||
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
|
||||
import { env } from 'process';
|
||||
|
||||
export class AppData {
|
||||
public readyDeferred = plugins.smartpromise.defer();
|
||||
public dirPathArg: string;
|
||||
private kvStore: KeyValueStore;
|
||||
constructor(pathArg?: string) {
|
||||
this.init(pathArg);
|
||||
export interface IAppDataOptions<T = any> {
|
||||
dirPath?: string;
|
||||
requiredKeys?: Array<keyof T>;
|
||||
|
||||
/**
|
||||
* kvStoreKey: 'MY_ENV_VAR'
|
||||
*/
|
||||
envMapping?: {
|
||||
[key in keyof T]?: 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<T> = {}): Promise<AppData<T>> {
|
||||
const appData = new AppData<T>(optionsArg);
|
||||
await appData.readyDeferred.promise;
|
||||
return appData;
|
||||
}
|
||||
|
||||
// instance
|
||||
public readyDeferred = plugins.smartpromise.defer<void>();
|
||||
public options: IAppDataOptions<T>;
|
||||
private kvStore: KeyValueStore<T>;
|
||||
|
||||
constructor(optionsArg: IAppDataOptions<T> = {}) {
|
||||
this.options = optionsArg;
|
||||
this.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* inits app data
|
||||
* @param pathArg
|
||||
*/
|
||||
private async init(pathArg?: string) {
|
||||
if (pathArg) {
|
||||
this.dirPathArg = pathArg;
|
||||
if (this.options.dirPath) {
|
||||
// ok, nothing to do here;
|
||||
} 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;
|
||||
this.dirPathArg = dataDir;
|
||||
this.options.dirPath = appDataDir;
|
||||
} 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<T>({
|
||||
typeArg: 'custom',
|
||||
identityArg: 'appkv',
|
||||
customPath: this.options.dirPath,
|
||||
mandatoryKeys: this.options.requiredKeys as Array<keyof T>
|
||||
});
|
||||
|
||||
if (this.options.envMapping) {
|
||||
const qenvInstance = new plugins.qenv.Qenv(process.cwd(), plugins.path.join(process.cwd(), '.nogit'));
|
||||
|
||||
// Recursive function to handle nested objects, now includes key parameter
|
||||
const processEnvMapping = async (key: keyof T, mappingValue: any, parentKey: keyof T | '' = ''): Promise<any> => {
|
||||
if (typeof mappingValue === 'string') {
|
||||
let envValue: string | T[keyof T];
|
||||
if (mappingValue.startsWith('hard:')) {
|
||||
envValue = mappingValue.replace('hard:', '') as T[keyof T];
|
||||
} else {
|
||||
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue) as T[keyof T];
|
||||
}
|
||||
if (envValue) {
|
||||
if (typeof envValue === 'string' && mappingValue.endsWith('_JSON')) {
|
||||
envValue = JSON.parse(envValue) as T[keyof T];
|
||||
}
|
||||
if (!parentKey) {
|
||||
await this.kvStore.writeKey(key, envValue);
|
||||
} else {
|
||||
return envValue;
|
||||
}
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
} else if (typeof mappingValue === 'object' && mappingValue !== null) {
|
||||
const resultObject: Partial<T> = {};
|
||||
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 as keyof T, nestedValue, key);
|
||||
resultObject[innerKey as keyof T] = nestedResult;
|
||||
}
|
||||
if (parentKey === '') {
|
||||
// Only write to kvStore if at the top level
|
||||
await this.kvStore.writeKey(key, resultObject as T[keyof T]);
|
||||
} 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 as keyof T, this.options.envMapping[key]);
|
||||
}
|
||||
}
|
||||
|
||||
this.readyDeferred.resolve();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a kvtore that resides in appdata
|
||||
* returns a kvstore that resides in appdata
|
||||
*/
|
||||
public async getKvStore() {
|
||||
public async getKvStore(): Promise<KeyValueStore<T>> {
|
||||
await this.readyDeferred.promise;
|
||||
return this.kvStore;
|
||||
}
|
||||
|
||||
public async logMissingKeys(): Promise<Array<keyof T>> {
|
||||
const kvStore = await this.getKvStore();
|
||||
const missingMandatoryKeys = await 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');
|
||||
}
|
||||
return missingMandatoryKeys;
|
||||
}
|
||||
|
||||
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
|
||||
await this.readyDeferred.promise;
|
||||
await this.kvStore.waitForKeysPresent([keyArg]);
|
||||
return this.kvStore.readKey(keyArg);
|
||||
}
|
||||
}
|
@ -5,12 +5,23 @@ import { Task } from '@push.rocks/taskbuffer';
|
||||
|
||||
export type TKeyValueStore = 'custom' | 'userHomeDir';
|
||||
|
||||
export interface IKvStoreConstructorOptions<T> {
|
||||
typeArg: TKeyValueStore;
|
||||
identityArg: string;
|
||||
customPath?: string;
|
||||
mandatoryKeys?: Array<keyof T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* kvStore is a simple key value store to store data about projects between runs
|
||||
*/
|
||||
export class KeyValueStore {
|
||||
private dataObject: any = {};
|
||||
private deletedObject: any = {};
|
||||
export class KeyValueStore<T = any> {
|
||||
private dataObject: Partial<T> = {};
|
||||
private deletedObject: Partial<T> = {};
|
||||
private mandatoryKeys: Set<keyof T> = new Set();
|
||||
public changeSubject = new plugins.smartrx.rxjs.Subject<Partial<T>>();
|
||||
|
||||
private storedStateString: string = '';
|
||||
public syncTask = new Task({
|
||||
name: 'syncTask',
|
||||
buffered: true,
|
||||
@ -21,14 +32,21 @@ export class KeyValueStore {
|
||||
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
||||
...this.dataObject,
|
||||
};
|
||||
for (const key of Object.keys(this.deletedObject)) {
|
||||
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.stringify(this.dataObject),
|
||||
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);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@ -70,23 +88,26 @@ export class KeyValueStore {
|
||||
* @param identityArg
|
||||
* @param customPath Optional custom path for the keyValue store
|
||||
*/
|
||||
constructor(typeArg: TKeyValueStore, identityArg: string, customPath?: string) {
|
||||
if (customPath && typeArg !== 'custom') {
|
||||
constructor(optionsArg: IKvStoreConstructorOptions<T>) {
|
||||
if (optionsArg.customPath && optionsArg.typeArg !== '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');
|
||||
}
|
||||
this.type = typeArg;
|
||||
this.identity = identityArg;
|
||||
this.customPath = customPath; // Store custom path if provided
|
||||
this.type = optionsArg.typeArg;
|
||||
this.identity = optionsArg.identityArg;
|
||||
this.customPath = optionsArg.customPath; // Store custom path if provided
|
||||
this.initFilePath();
|
||||
if (optionsArg.mandatoryKeys) {
|
||||
this.setMandatoryKeys(optionsArg.mandatoryKeys);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* reads all keyValue pairs at once and returns them
|
||||
*/
|
||||
public async readAll() {
|
||||
public async readAll(): Promise<Partial<T>> {
|
||||
await this.syncTask.trigger();
|
||||
return this.dataObject;
|
||||
}
|
||||
@ -94,21 +115,21 @@ export class KeyValueStore {
|
||||
/**
|
||||
* reads a keyValueFile from disk
|
||||
*/
|
||||
public async readKey(keyArg: string) {
|
||||
public async readKey<K extends keyof T>(keyArg: K): Promise<T[K]> {
|
||||
await this.syncTask.trigger();
|
||||
return this.dataObject[keyArg];
|
||||
return this.dataObject[keyArg] as T[K];
|
||||
}
|
||||
|
||||
/**
|
||||
* writes a specific key to the keyValueStore
|
||||
*/
|
||||
public async writeKey(keyArg: string, valueArg: any) {
|
||||
public async writeKey<K extends keyof T>(keyArg: K, valueArg: T[K]): Promise<void> {
|
||||
await this.writeAll({
|
||||
[keyArg]: valueArg,
|
||||
});
|
||||
} as unknown as Partial<T>);
|
||||
}
|
||||
|
||||
public async deleteKey(keyArg: string) {
|
||||
public async deleteKey<K extends keyof T>(keyArg: K): Promise<void> {
|
||||
this.deletedObject[keyArg] = this.dataObject[keyArg];
|
||||
await this.syncTask.trigger();
|
||||
}
|
||||
@ -116,7 +137,7 @@ export class KeyValueStore {
|
||||
/**
|
||||
* writes all keyValue pairs in the object argument
|
||||
*/
|
||||
public async writeAll(keyValueObject: { [key: string]: any }) {
|
||||
public async writeAll(keyValueObject: Partial<T>): Promise<void> {
|
||||
this.dataObject = { ...this.dataObject, ...keyValueObject };
|
||||
await this.syncTask.trigger();
|
||||
}
|
||||
@ -124,7 +145,7 @@ export class KeyValueStore {
|
||||
/**
|
||||
* wipes a key value store from disk
|
||||
*/
|
||||
public async wipe() {
|
||||
public async wipe(): Promise<void> {
|
||||
this.dataObject = {};
|
||||
await plugins.smartfile.fs.remove(this.filePath);
|
||||
}
|
||||
@ -132,15 +153,45 @@ export class KeyValueStore {
|
||||
/**
|
||||
* resets the KeyValueStore to the initial state by syncing first, deleting all keys, and then triggering a sync again
|
||||
*/
|
||||
public async reset() {
|
||||
public async reset(): Promise<void> {
|
||||
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)) {
|
||||
for (const key of Object.keys(this.dataObject) as Array<keyof T>) {
|
||||
this.deletedObject[key] = this.dataObject[key];
|
||||
delete this.dataObject[key];
|
||||
}
|
||||
|
||||
await this.syncTask.trigger(); // Sync again to reflect the deletion
|
||||
}
|
||||
}
|
||||
|
||||
private setMandatoryKeys(keys: Array<keyof T>) {
|
||||
keys.forEach(key => this.mandatoryKeys.add(key));
|
||||
}
|
||||
|
||||
public async getMissingMandatoryKeys(): Promise<Array<keyof T>> {
|
||||
await this.readAll();
|
||||
return Array.from(this.mandatoryKeys).filter(key => !(key in this.dataObject));
|
||||
}
|
||||
|
||||
public async waitForKeysPresent<K extends keyof T>(keysArg: K[]): Promise<void> {
|
||||
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||
if (missingKeys.length === 0) {
|
||||
return;
|
||||
}
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const subscription = this.changeSubject.subscribe(() => {
|
||||
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||
if (missingKeys.length === 0) {
|
||||
subscription.unsubscribe();
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
|
||||
await this.waitForKeysPresent([keyArg]);
|
||||
return this.readKey(keyArg);
|
||||
}
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
import * as qenv from '@push.rocks/qenv';
|
||||
import * as smartlog from '@push.rocks/smartlog';
|
||||
import * as path from 'path';
|
||||
import * as smartfile from '@push.rocks/smartfile';
|
||||
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 { smartlog, path, smartfile, smartjson, smartpath, smartpromise, taskbuffer };
|
||||
export { qenv, smartlog, path, smartfile, smartjson, smartpath, smartpromise, smartrx, taskbuffer };
|
||||
|
@ -3,9 +3,12 @@
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
}
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user