Compare commits
No commits in common. "master" and "v5.0.13" have entirely different histories.
57
changelog.md
57
changelog.md
@ -1,57 +0,0 @@
|
|||||||
# Changelog
|
|
||||||
|
|
||||||
## 2024-11-06 - 5.1.2 - fix(appdata)
|
|
||||||
Fix iteration over overwriteObject in AppData class
|
|
||||||
|
|
||||||
- Corrected the for loop from in to of inside the AppData class for iterating over overwriteObject keys.
|
|
||||||
|
|
||||||
## 2024-11-05 - 5.1.1 - fix(AppData)
|
|
||||||
Fix issue with overwrite object handling in AppData class
|
|
||||||
|
|
||||||
- Corrected logic to handle cases when overwriteObject is undefined.
|
|
||||||
|
|
||||||
## 2024-11-05 - 5.1.0 - feat(appdata)
|
|
||||||
Add support for overwriting keys using the overwriteObject option in AppData
|
|
||||||
|
|
||||||
- Introduced the overwriteObject option in IAppDataOptions to allow overwriting specific keys in the AppData class.
|
|
||||||
|
|
||||||
## 2024-06-19 - 5.0.17 - 5.0.23 - Core Updates
|
|
||||||
Routine maintenance and updates to the core components.
|
|
||||||
|
|
||||||
- Multiple core updates and fixes improving stability
|
|
||||||
|
|
||||||
## 2024-06-12 - 5.0.13 - 5.0.16 - Core Updates
|
|
||||||
Maintenance focus on core systems with enhancements and problem resolutions.
|
|
||||||
|
|
||||||
- Enhancements and updates in the core functionality
|
|
||||||
|
|
||||||
## 2024-05-29 - 5.0.13 - Documentation Update
|
|
||||||
Descriptive improvements aligned with current features.
|
|
||||||
|
|
||||||
- Updated core description for better clarity in documentation
|
|
||||||
|
|
||||||
## 2024-04-01 - 5.0.10 - Configuration Update
|
|
||||||
Improved configuration management for build processes.
|
|
||||||
|
|
||||||
- Updated `npmextra.json` to reflect changes in git repository management
|
|
||||||
|
|
||||||
## 2024-02-12 - 5.0.0 - 5.0.9 - Major Core Enhancements
|
|
||||||
A series of critical updates with resolved issues in the core components.
|
|
||||||
|
|
||||||
- Introduction of new core features
|
|
||||||
- Several core system updates
|
|
||||||
|
|
||||||
## 2024-02-12 - 4.0.16 - Major Version Transition
|
|
||||||
Migration to the new major version with impactful changes.
|
|
||||||
|
|
||||||
- BREAKING CHANGE: Significant updates requiring attention for smooth transition
|
|
||||||
|
|
||||||
## 2023-08-24 - 3.0.9 - 4.0.16 - Organization Updates
|
|
||||||
Formatted updates with attention to organizational standards and practice.
|
|
||||||
|
|
||||||
- SWITCH to a new organizational scheme
|
|
||||||
|
|
||||||
## 2023-07-11 - 3.0.9 - Organizational Enhancement
|
|
||||||
Shifts aligning with contemporary structuring and logistics.
|
|
||||||
|
|
||||||
- Strategic realignment with organizational principles
|
|
@ -14,7 +14,7 @@
|
|||||||
"githost": "code.foss.global",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "push.rocks",
|
"gitscope": "push.rocks",
|
||||||
"gitrepo": "npmextra",
|
"gitrepo": "npmextra",
|
||||||
"description": "A utility to enhance npm with additional configuration, tool management capabilities, and a key-value store for project setups.",
|
"description": "Enhances npm with additional configuration and tool management capabilities, including a key-value store for project setups.",
|
||||||
"npmPackagename": "@push.rocks/npmextra",
|
"npmPackagename": "@push.rocks/npmextra",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
@ -23,16 +23,11 @@
|
|||||||
"tool management",
|
"tool management",
|
||||||
"key-value store",
|
"key-value store",
|
||||||
"project setup",
|
"project setup",
|
||||||
|
"npm scripts",
|
||||||
"typescript",
|
"typescript",
|
||||||
"environment setup",
|
"environment setup",
|
||||||
"dependencies management",
|
"dependencies management",
|
||||||
"npm package enhancement",
|
"npm package enhancement"
|
||||||
"automation",
|
|
||||||
"async operations",
|
|
||||||
"app configuration",
|
|
||||||
"smart file handling",
|
|
||||||
"workflow improvement",
|
|
||||||
"persistent storage"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
36
package.json
36
package.json
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/npmextra",
|
"name": "@push.rocks/npmextra",
|
||||||
"version": "5.1.2",
|
"version": "5.0.13",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "A utility to enhance npm with additional configuration, tool management capabilities, and a key-value store for project setups.",
|
"description": "Enhances npm with additional configuration and tool management capabilities, including a key-value store for project setups.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -12,31 +12,30 @@
|
|||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://code.foss.global/push.rocks/npmextra.git"
|
"url": "git+ssh://git@gitlab.com/pushrocks/npmextra.git"
|
||||||
},
|
},
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://gitlab.com/pushrocks/npmextra/issues"
|
"url": "https://gitlab.com/pushrocks/npmextra/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://code.foss.global/push.rocks/npmextra",
|
"homepage": "https://gitlab.com/pushrocks/npmextra#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/qenv": "^6.0.5",
|
"@push.rocks/qenv": "^6.0.5",
|
||||||
"@push.rocks/smartfile": "^11.0.20",
|
"@push.rocks/smartfile": "^11.0.4",
|
||||||
"@push.rocks/smartjson": "^5.0.20",
|
"@push.rocks/smartjson": "^5.0.10",
|
||||||
"@push.rocks/smartlog": "^3.0.7",
|
"@push.rocks/smartlog": "^3.0.2",
|
||||||
"@push.rocks/smartpath": "^5.0.18",
|
"@push.rocks/smartpath": "^5.0.11",
|
||||||
"@push.rocks/smartpromise": "^4.0.2",
|
"@push.rocks/smartpromise": "^4.0.2",
|
||||||
"@push.rocks/smartrx": "^3.0.7",
|
"@push.rocks/smartrx": "^3.0.7",
|
||||||
"@push.rocks/taskbuffer": "^3.1.7",
|
"@push.rocks/taskbuffer": "^3.1.7"
|
||||||
"@tsclass/tsclass": "^4.0.59"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.1.80",
|
"@git.zone/tsbuild": "^2.1.66",
|
||||||
"@git.zone/tsrun": "^1.2.44",
|
"@git.zone/tsrun": "^1.2.44",
|
||||||
"@git.zone/tstest": "^1.0.90",
|
"@git.zone/tstest": "^1.0.77",
|
||||||
"@push.rocks/tapbundle": "^5.0.23",
|
"@push.rocks/tapbundle": "^5.0.15",
|
||||||
"@types/node": "^20.14.5"
|
"@types/node": "^20.11.17"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@ -60,15 +59,10 @@
|
|||||||
"tool management",
|
"tool management",
|
||||||
"key-value store",
|
"key-value store",
|
||||||
"project setup",
|
"project setup",
|
||||||
|
"npm scripts",
|
||||||
"typescript",
|
"typescript",
|
||||||
"environment setup",
|
"environment setup",
|
||||||
"dependencies management",
|
"dependencies management",
|
||||||
"npm package enhancement",
|
"npm package enhancement"
|
||||||
"automation",
|
|
||||||
"async operations",
|
|
||||||
"app configuration",
|
|
||||||
"smart file handling",
|
|
||||||
"workflow improvement",
|
|
||||||
"persistent storage"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
8268
pnpm-lock.yaml
generated
8268
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
214
readme.md
214
readme.md
@ -1,5 +1,5 @@
|
|||||||
# @push.rocks/npmextra
|
# @push.rocks/npmextra
|
||||||
A utility to enhance npm with additional configuration, tool management capabilities, and a key-value store for project setups.
|
Enhances npm with additional configuration and tool management capabilities.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
To install `@push.rocks/npmextra`, use the following npm command:
|
To install `@push.rocks/npmextra`, use the following npm command:
|
||||||
@ -11,10 +11,10 @@ npm install @push.rocks/npmextra --save
|
|||||||
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.
|
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
|
## Usage
|
||||||
`@push.rocks/npmextra` is designed to supplement npm functionalities with enhanced configuration and tool management. It facilitates the management of 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.
|
`@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.
|
||||||
|
|
||||||
### Initial Setup and Configuration
|
### Initial Setup and Configuration
|
||||||
To start using `npmextra` in your project, first include it with an import statement:
|
To start using npmextra in your project, first include it in your project with an import statement:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Npmextra } from '@push.rocks/npmextra';
|
import { Npmextra } from '@push.rocks/npmextra';
|
||||||
@ -27,7 +27,7 @@ const npmExtraInstance = new Npmextra('/path/to/your/project');
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Managing Tool Configurations with `npmextra.json`
|
### 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, `npmextra` enables you to define tool-specific settings within this centralized configuration file, which can then be accessed programmatically.
|
`@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`
|
#### Creating and Utilizing `npmextra.json`
|
||||||
|
|
||||||
@ -49,16 +49,16 @@ For example, to configure a hypothetical tool named `toolname`, define its setti
|
|||||||
With the configuration defined, you can easily access these settings in your TypeScript code as follows:
|
With the configuration defined, you can easily access these settings in your TypeScript code as follows:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// Import the npmextra module
|
// import the npmextra module
|
||||||
import { Npmextra } from '@push.rocks/npmextra';
|
import { Npmextra } from '@push.rocks/npmextra';
|
||||||
|
|
||||||
// Create an instance pointing at the current directory
|
// create an instance pointing at the current directory
|
||||||
const npmExtraInstance = new Npmextra();
|
const npmExtraInstance = new Npmextra();
|
||||||
|
|
||||||
// Retrieve the configuration for 'toolname', merging defaults with any found in npmextra.json
|
// retrieve the configuration for 'toolname', merging defaults with any found in npmextra.json
|
||||||
const toolConfig = npmExtraInstance.dataFor<{ setting1: string, setting2: string }>('toolname', {
|
const toolConfig = npmExtraInstance.dataFor('toolname', {
|
||||||
setting1: 'defaultValue1',
|
defaultKey1: 'defaultValue1',
|
||||||
setting2: 'defaultValue2'
|
defaultKey2: 'defaultValue2'
|
||||||
});
|
});
|
||||||
|
|
||||||
// toolConfig now contains the merged settings from npmextra.json and provided defaults
|
// toolConfig now contains the merged settings from npmextra.json and provided defaults
|
||||||
@ -75,10 +75,7 @@ To utilize the KeyValueStore, create an instance specifying its scope (e.g., 'us
|
|||||||
```typescript
|
```typescript
|
||||||
import { KeyValueStore } from '@push.rocks/npmextra';
|
import { KeyValueStore } from '@push.rocks/npmextra';
|
||||||
|
|
||||||
const kvStore = new KeyValueStore<'userHomeDir'>({
|
const kvStore = new KeyValueStore<'userHomeDir'>('userHomeDir', 'myUniqueAppName');
|
||||||
typeArg: 'userHomeDir',
|
|
||||||
identityArg: 'myUniqueAppName'
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You can then use the `writeKey`, `readKey`, `writeAll`, and `readAll` methods to manage your store respectively.
|
You can then use the `writeKey`, `readKey`, `writeAll`, and `readAll` methods to manage your store respectively.
|
||||||
@ -104,192 +101,13 @@ const allData = await kvStore.readAll();
|
|||||||
console.log(allData); // Outputs the entire store's contents
|
console.log(allData); // Outputs the entire store's contents
|
||||||
```
|
```
|
||||||
|
|
||||||
### Advanced Key-Value Store Management
|
### 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.
|
||||||
|
|
||||||
In addition to basic read/write operations, `npmextra`’s `KeyValueStore` supports advanced scenarios like mandatory keys and custom file paths.
|
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.
|
||||||
|
|
||||||
#### Example: Mandatory Keys and Custom Paths
|
### 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.
|
||||||
Consider a scenario where your application requires specific keys to be present in the KeyValueStore. You can define mandatory keys and use a custom path for your store like this:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { KeyValueStore } from '@push.rocks/npmextra';
|
|
||||||
|
|
||||||
interface CustomData {
|
|
||||||
key1: string;
|
|
||||||
key2: number;
|
|
||||||
key3?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
const kvStore = new KeyValueStore<CustomData>({
|
|
||||||
typeArg: 'custom',
|
|
||||||
identityArg: 'customApp',
|
|
||||||
customPath: '/custom/path/to/store.json',
|
|
||||||
mandatoryKeys: ['key1', 'key2']
|
|
||||||
});
|
|
||||||
|
|
||||||
// Ensure all mandatory keys are present
|
|
||||||
const missingKeys = await kvStore.getMissingMandatoryKeys();
|
|
||||||
if (missingKeys.length) {
|
|
||||||
console.log(`Missing mandatory keys: ${missingKeys.join(', ')}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use the KeyValueStore
|
|
||||||
await kvStore.writeKey('key1', 'value1');
|
|
||||||
await kvStore.writeKey('key2', 123);
|
|
||||||
|
|
||||||
const key1Value = await kvStore.readKey('key1');
|
|
||||||
const allData = await kvStore.readAll();
|
|
||||||
|
|
||||||
console.log(key1Value); // Outputs: value1
|
|
||||||
console.log(allData); // Outputs: { key1: 'value1', key2: 123 }
|
|
||||||
```
|
|
||||||
|
|
||||||
### Combining AppData and KeyValueStore
|
|
||||||
|
|
||||||
The `AppData` class extends the functionality of `KeyValueStore` by integrating environmental variables, specifying additional configurations, and providing a more structured approach to data management.
|
|
||||||
|
|
||||||
#### Example: AppData Usage
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { AppData } from '@push.rocks/npmextra';
|
|
||||||
|
|
||||||
interface AppSettings {
|
|
||||||
settingA: string;
|
|
||||||
settingB: number;
|
|
||||||
nestedSetting: {
|
|
||||||
innerSetting: boolean;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const appDataInstance = await AppData.createAndInit<AppSettings>({
|
|
||||||
dirPath: '/custom/path/to/appdata',
|
|
||||||
requiredKeys: ['settingA', 'settingB'],
|
|
||||||
envMapping: {
|
|
||||||
settingA: 'MY_ENV_A',
|
|
||||||
settingB: 'hard:42',
|
|
||||||
nestedSetting: {
|
|
||||||
innerSetting: 'MY_ENV_INNER'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const appDataKvStore = await appDataInstance.getKvStore();
|
|
||||||
|
|
||||||
// Writing values
|
|
||||||
await appDataKvStore.writeKey('settingA', 'exampleValue');
|
|
||||||
await appDataKvStore.writeKey('settingB', 100);
|
|
||||||
await appDataKvStore.writeKey('nestedSetting', { innerSetting: true });
|
|
||||||
|
|
||||||
// Reading values
|
|
||||||
const settingA = await appDataKvStore.readKey('settingA');
|
|
||||||
const allSettings = await appDataKvStore.readAll();
|
|
||||||
|
|
||||||
console.log(settingA); // Outputs: 'exampleValue'
|
|
||||||
console.log(allSettings); // Outputs: { settingA: 'exampleValue', settingB: 100, nestedSetting: { innerSetting: true } }
|
|
||||||
```
|
|
||||||
|
|
||||||
### Error Handling and Debugging
|
|
||||||
|
|
||||||
Proper error handling ensures your integrations with `npmextra` are robust and stable. Below are some strategies for error handling and debugging potential issues.
|
|
||||||
|
|
||||||
#### Example: Error Handling in KeyValueStore
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { KeyValueStore } from '@push.rocks/npmextra';
|
|
||||||
|
|
||||||
const kvStore = new KeyValueStore('userHomeDir', 'errorHandlingApp');
|
|
||||||
|
|
||||||
try {
|
|
||||||
await kvStore.writeKey('importantKey', 'importantValue');
|
|
||||||
const value = await kvStore.readKey('importantKey');
|
|
||||||
console.log(value); // Outputs: importantValue
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error managing key-value store:', error);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Debugging Configuration Issues in `npmextra.json`
|
|
||||||
|
|
||||||
To debug configuration issues, you can utilize conditional logging and checks:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { Npmextra } from '@push.rocks/npmextra';
|
|
||||||
|
|
||||||
const npmExtraInstance = new Npmextra();
|
|
||||||
const toolConfig = npmExtraInstance.dataFor('toolname', {
|
|
||||||
configKey1: 'defaultValue1',
|
|
||||||
configKey2: 'defaultValue2'
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!toolConfig.configKey1) {
|
|
||||||
console.error('configKey1 is missing in npmextra.json');
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(toolConfig);
|
|
||||||
```
|
|
||||||
|
|
||||||
### Integration Tests
|
|
||||||
|
|
||||||
Writing tests ensures that your integration with `npmextra` works as expected. Below are examples of integration tests for both `Npmextra` and `KeyValueStore`.
|
|
||||||
|
|
||||||
#### Example: Testing `Npmextra` Class
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { expect, tap } from '@push.rocks/tapbundle';
|
|
||||||
import { Npmextra } from '@push.rocks/npmextra';
|
|
||||||
|
|
||||||
let npmExtraInstance: Npmextra;
|
|
||||||
|
|
||||||
tap.test('should create an instance of Npmextra', async () => {
|
|
||||||
npmExtraInstance = new Npmextra();
|
|
||||||
expect(npmExtraInstance).toBeInstanceOf(Npmextra);
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should load configuration from npmextra.json', async () => {
|
|
||||||
const config = npmExtraInstance.dataFor('toolname', {
|
|
||||||
defaultKey1: 'defaultValue1',
|
|
||||||
});
|
|
||||||
expect(config).toHaveProperty('defaultKey1');
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.start();
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Example: Testing `KeyValueStore` Class
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { expect, tap } from '@push.rocks/tapbundle';
|
|
||||||
import { KeyValueStore } from '@push.rocks/npmextra';
|
|
||||||
|
|
||||||
let kvStore: KeyValueStore<{ key1: string, key2: number }>;
|
|
||||||
|
|
||||||
tap.test('should create a KeyValueStore instance', async () => {
|
|
||||||
kvStore = new KeyValueStore({
|
|
||||||
typeArg: 'userHomeDir',
|
|
||||||
identityArg: 'testApp'
|
|
||||||
});
|
|
||||||
expect(kvStore).toBeInstanceOf(KeyValueStore);
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should write and read back a value', async () => {
|
|
||||||
await kvStore.writeKey('key1', 'value1');
|
|
||||||
const result = await kvStore.readKey('key1');
|
|
||||||
expect(result).toEqual('value1');
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should write and read back multiple values', async () => {
|
|
||||||
await kvStore.writeAll({ key1: 'updatedValue1', key2: 2 });
|
|
||||||
const result = await kvStore.readAll();
|
|
||||||
expect(result).toEqual({ key1: 'updatedValue1', key2: 2 });
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.start();
|
|
||||||
```
|
|
||||||
|
|
||||||
### Summary
|
|
||||||
|
|
||||||
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, `npmextra` provides an efficient and streamlined solution. Leveraging these robust features will ensure your project is well-configured and maintainable.
|
|
||||||
|
|
||||||
## License and Legal Information
|
## License and Legal Information
|
||||||
|
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
import { expect, tap } from '@push.rocks/tapbundle';
|
|
||||||
import path = require('path');
|
|
||||||
|
|
||||||
// module to test
|
|
||||||
import * as npmextra from '../ts/index.js';
|
|
||||||
|
|
||||||
interface ITestOptions {
|
|
||||||
hi: string;
|
|
||||||
deep: {
|
|
||||||
deep1: string;
|
|
||||||
deep2: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let testAppdata: npmextra.AppData<ITestOptions>;
|
|
||||||
|
|
||||||
tap.test('should create a valid AppData', async () => {
|
|
||||||
testAppdata = new npmextra.AppData<ITestOptions>({
|
|
||||||
envMapping: {
|
|
||||||
deep: {
|
|
||||||
deep1: '',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
export default tap.start();
|
|
@ -2,14 +2,10 @@ import { expect, tap } from '@push.rocks/tapbundle';
|
|||||||
|
|
||||||
import * as npmextra from '../ts/index.js';
|
import * as npmextra from '../ts/index.js';
|
||||||
|
|
||||||
let myKeyValueStore: npmextra.KeyValueStore<any>;
|
let myKeyValueStore: npmextra.KeyValueStore;
|
||||||
|
|
||||||
tap.test('should create a keyValueStore', async () => {
|
tap.test('should create a keyValueStore', async () => {
|
||||||
myKeyValueStore = new npmextra.KeyValueStore<any>({
|
myKeyValueStore = new npmextra.KeyValueStore('custom', 'test', 'test/somekv.json');
|
||||||
typeArg: 'custom',
|
|
||||||
identityArg: 'test',
|
|
||||||
customPath: 'test/somekv.json',
|
|
||||||
});
|
|
||||||
expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore);
|
expect(myKeyValueStore).toBeInstanceOf(npmextra.KeyValueStore);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* autocreated commitinfo by @push.rocks/commitinfo
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/npmextra',
|
name: '@push.rocks/npmextra',
|
||||||
version: '5.1.2',
|
version: '5.0.13',
|
||||||
description: 'A utility to enhance npm with additional configuration, tool management capabilities, and a key-value store for project setups.'
|
description: 'Enhances npm with additional configuration and tool management capabilities, including a key-value store for project setups.'
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,18 @@
|
|||||||
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 interface IAppDataOptions<T = any> {
|
export interface IAppDataOptions {
|
||||||
dirPath?: string;
|
dirPath?: string;
|
||||||
requiredKeys?: Array<keyof T>;
|
requiredKeys?: string[];
|
||||||
|
|
||||||
/**
|
|
||||||
* wether keys should be persisted on disk or not
|
|
||||||
*/
|
|
||||||
ephermal?: boolean;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* kvStoreKey: 'MY_ENV_VAR'
|
* kvStoreKey: 'MY_ENV_VAR'
|
||||||
*/
|
*/
|
||||||
envMapping?: plugins.tsclass.typeFest.PartialDeep<T>;
|
envMapping?: {
|
||||||
overwriteObject?: plugins.tsclass.typeFest.PartialDeep<T>;
|
[key: string]: string | object;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AppData<T = any> {
|
export class AppData<T = any> {
|
||||||
@ -25,20 +22,17 @@ export class AppData<T = any> {
|
|||||||
* @param pathArg
|
* @param pathArg
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
public static async createAndInit<T = any>(
|
public static async createAndInit<T = any>(optionsArg: IAppDataOptions = {}): Promise<AppData<T>> {
|
||||||
optionsArg: IAppDataOptions<T> = {}
|
|
||||||
): Promise<AppData<T>> {
|
|
||||||
const appData = new AppData<T>(optionsArg);
|
const appData = new AppData<T>(optionsArg);
|
||||||
await appData.readyDeferred.promise;
|
await appData.readyDeferred.promise;
|
||||||
return appData;
|
return appData;
|
||||||
}
|
}
|
||||||
|
|
||||||
// instance
|
// instance
|
||||||
public readyDeferred = plugins.smartpromise.defer<void>();
|
public readyDeferred = plugins.smartpromise.defer();
|
||||||
public options: IAppDataOptions<T>;
|
public options: IAppDataOptions;
|
||||||
private kvStore: KeyValueStore<T>;
|
private kvStore: KeyValueStore<T>;
|
||||||
|
constructor(optionsArg: IAppDataOptions = {}) {
|
||||||
constructor(optionsArg: IAppDataOptions<T> = {}) {
|
|
||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
@ -48,7 +42,7 @@ export class AppData<T = any> {
|
|||||||
* @param pathArg
|
* @param pathArg
|
||||||
*/
|
*/
|
||||||
private async init(pathArg?: string) {
|
private async init(pathArg?: string) {
|
||||||
if (this.options.dirPath || this.options.ephermal) {
|
if (this.options.dirPath) {
|
||||||
// ok, nothing to do here;
|
// ok, nothing to do here;
|
||||||
} else {
|
} else {
|
||||||
const appDataDir = '/app/data';
|
const appDataDir = '/app/data';
|
||||||
@ -65,90 +59,31 @@ export class AppData<T = any> {
|
|||||||
this.options.dirPath = nogitAppData;
|
this.options.dirPath = nogitAppData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.kvStore = new KeyValueStore({
|
||||||
this.kvStore = new KeyValueStore<T>({
|
typeArg: 'custom',
|
||||||
typeArg: this.options.ephermal ? 'ephemeral' : 'custom',
|
|
||||||
identityArg: 'appkv',
|
identityArg: 'appkv',
|
||||||
customPath: this.options.dirPath,
|
customPath: this.options.dirPath,
|
||||||
mandatoryKeys: this.options.requiredKeys as Array<keyof T>,
|
mandatoryKeys: this.options.requiredKeys
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.options.envMapping) {
|
if (this.options.envMapping) {
|
||||||
const qenvInstance = new plugins.qenv.Qenv(
|
const qenvInstance = new plugins.qenv.Qenv(process.cwd(), plugins.path.join(process.cwd(), '.nogit'));
|
||||||
process.cwd(),
|
|
||||||
plugins.path.join(process.cwd(), '.nogit')
|
|
||||||
);
|
|
||||||
|
|
||||||
// Recursive function to handle nested objects, now includes key parameter
|
// Recursive function to handle nested objects, now includes key parameter
|
||||||
const processEnvMapping = async (
|
const processEnvMapping = async (key: string, mappingValue: any, parentKey: string = ''): Promise<any> => {
|
||||||
key: keyof T,
|
|
||||||
mappingValue: any,
|
|
||||||
parentKey: keyof T | '' = ''
|
|
||||||
): Promise<any> => {
|
|
||||||
if (typeof mappingValue === 'string') {
|
if (typeof mappingValue === 'string') {
|
||||||
let envValue: string | boolean | T[keyof T];
|
let envValue: string;
|
||||||
let convert: 'none' | 'json' | 'base64' | 'boolean' = 'none';
|
if (mappingValue.startsWith('hard:')) {
|
||||||
switch (true) {
|
envValue = mappingValue.replace('hard:', '');
|
||||||
case mappingValue.startsWith('hard:'):
|
} else {
|
||||||
envValue = mappingValue.replace('hard:', '') as T[keyof T];
|
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue);
|
||||||
break;
|
|
||||||
case mappingValue.startsWith('hard_boolean:'):
|
|
||||||
envValue = mappingValue.replace('hard_boolean:', '') === 'true';
|
|
||||||
convert = 'boolean';
|
|
||||||
break;
|
|
||||||
case mappingValue.startsWith('hard_json:'):
|
|
||||||
envValue = JSON.parse(mappingValue.replace('hard_json:', '')) as T[keyof T];
|
|
||||||
convert = 'json';
|
|
||||||
break;
|
|
||||||
case mappingValue.startsWith('hard_base64:'):
|
|
||||||
envValue = Buffer.from(
|
|
||||||
mappingValue.replace('hard_base64:', ''),
|
|
||||||
'base64'
|
|
||||||
).toString() as T[keyof T];
|
|
||||||
convert = 'base64';
|
|
||||||
break;
|
|
||||||
case mappingValue.startsWith('boolean:'):
|
|
||||||
envValue = (await qenvInstance.getEnvVarOnDemand(
|
|
||||||
mappingValue.replace('boolean:', '')
|
|
||||||
)) as T[keyof T];
|
|
||||||
convert = 'boolean';
|
|
||||||
break;
|
|
||||||
case mappingValue.startsWith('json:'):
|
|
||||||
envValue = (await qenvInstance.getEnvVarOnDemand(
|
|
||||||
mappingValue.replace('json:', '')
|
|
||||||
)) as T[keyof T];
|
|
||||||
convert = 'json';
|
|
||||||
break;
|
|
||||||
case mappingValue.startsWith('base64:'):
|
|
||||||
envValue = (await qenvInstance.getEnvVarOnDemand(
|
|
||||||
mappingValue.replace('base64:', '')
|
|
||||||
)) as T[keyof T];
|
|
||||||
convert = 'base64';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
envValue = (await qenvInstance.getEnvVarOnDemand(mappingValue)) as T[keyof T];
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// lets format the env value
|
|
||||||
if (envValue) {
|
if (envValue) {
|
||||||
if (typeof envValue === 'string' && convert === 'boolean') {
|
if (mappingValue.endsWith('_JSON')) {
|
||||||
envValue = envValue === 'true';
|
envValue = JSON.parse(envValue);
|
||||||
}
|
|
||||||
if (
|
|
||||||
typeof envValue === 'string' &&
|
|
||||||
(mappingValue.endsWith('_JSON') || convert === 'json')
|
|
||||||
) {
|
|
||||||
envValue = JSON.parse(envValue as string) as T[keyof T];
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
typeof envValue === 'string' &&
|
|
||||||
(mappingValue.endsWith('_BASE64') || convert === 'base64')
|
|
||||||
) {
|
|
||||||
envValue = Buffer.from(envValue as string, 'base64').toString();
|
|
||||||
}
|
}
|
||||||
if (!parentKey) {
|
if (!parentKey) {
|
||||||
await this.kvStore.writeKey(key, envValue as any);
|
await this.kvStore.writeKey(key, envValue);
|
||||||
} else {
|
} else {
|
||||||
return envValue;
|
return envValue;
|
||||||
}
|
}
|
||||||
@ -156,16 +91,16 @@ export class AppData<T = any> {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
} else if (typeof mappingValue === 'object' && mappingValue !== null) {
|
} else if (typeof mappingValue === 'object' && mappingValue !== null) {
|
||||||
const resultObject: Partial<T> = {};
|
const resultObject = {};
|
||||||
for (const innerKey in mappingValue) {
|
for (const innerKey in mappingValue) {
|
||||||
const nestedValue = mappingValue[innerKey];
|
const nestedValue = mappingValue[innerKey];
|
||||||
// For nested objects, call recursively but do not immediately write to kvStore
|
// For nested objects, call recursively but do not immediately write to kvStore
|
||||||
const nestedResult = await processEnvMapping(innerKey as keyof T, nestedValue, key);
|
const nestedResult = await processEnvMapping(innerKey, nestedValue, key);
|
||||||
resultObject[innerKey as keyof T] = nestedResult;
|
resultObject[innerKey] = nestedResult;
|
||||||
}
|
}
|
||||||
if (parentKey === '') {
|
if (parentKey === '') {
|
||||||
// Only write to kvStore if at the top level
|
// Only write to kvStore if at the top level
|
||||||
await this.kvStore.writeKey(key, resultObject as T[keyof T]);
|
await this.kvStore.writeKey(key, resultObject);
|
||||||
} else {
|
} else {
|
||||||
// For nested objects, return the constructed object instead of writing to kvStore
|
// For nested objects, return the constructed object instead of writing to kvStore
|
||||||
return resultObject;
|
return resultObject;
|
||||||
@ -174,29 +109,24 @@ export class AppData<T = any> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for (const key in this.options.envMapping) {
|
for (const key in this.options.envMapping) {
|
||||||
await processEnvMapping(key as keyof T, this.options.envMapping[key]);
|
await processEnvMapping(key, this.options.envMapping[key]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.overwriteObject) {
|
|
||||||
for (const key of Object.keys(this.options.overwriteObject)) {
|
|
||||||
console.log(`-> heads up: overwriting key ${key} from options.overwriteObject`);
|
|
||||||
await this.kvStore.writeKey(key as keyof T, this.options.overwriteObject[key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.readyDeferred.resolve();
|
this.readyDeferred.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns a kvstore that resides in appdata
|
* returns a kvtore that resides in appdata
|
||||||
*/
|
*/
|
||||||
public async getKvStore(): Promise<KeyValueStore<T>> {
|
public async getKvStore() {
|
||||||
await this.readyDeferred.promise;
|
await this.readyDeferred.promise;
|
||||||
return this.kvStore;
|
return this.kvStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async logMissingKeys(): Promise<Array<keyof T>> {
|
public async logMissingKeys() {
|
||||||
const kvStore = await this.getKvStore();
|
const kvStore = await this.getKvStore();
|
||||||
const missingMandatoryKeys = await kvStore.getMissingMandatoryKeys();
|
const missingMandatoryKeys = await kvStore.getMissingMandatoryKeys();
|
||||||
if (missingMandatoryKeys.length > 0) {
|
if (missingMandatoryKeys.length > 0) {
|
||||||
@ -211,9 +141,9 @@ export class AppData<T = any> {
|
|||||||
return missingMandatoryKeys;
|
return missingMandatoryKeys;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
|
public async waitForAndGetKey(keyArg: string) {
|
||||||
await this.readyDeferred.promise;
|
await this.readyDeferred.promise;
|
||||||
await this.kvStore.waitForKeysPresent([keyArg]);
|
await this.kvStore.waitForKeysPresent([keyArg]);
|
||||||
return this.kvStore.readKey(keyArg);
|
return this.kvStore.readKey[keyArg];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,13 @@ import * as paths from './npmextra.paths.js';
|
|||||||
|
|
||||||
import { Task } from '@push.rocks/taskbuffer';
|
import { Task } from '@push.rocks/taskbuffer';
|
||||||
|
|
||||||
export type TKeyValueStore = 'custom' | 'userHomeDir' | 'ephemeral';
|
export type TKeyValueStore = 'custom' | 'userHomeDir';
|
||||||
|
|
||||||
export interface IKvStoreConstructorOptions<T> {
|
export interface IKvStoreConstructorOptions {
|
||||||
typeArg: TKeyValueStore;
|
typeArg: TKeyValueStore;
|
||||||
identityArg: string;
|
identityArg: string;
|
||||||
customPath?: string;
|
customPath?: string;
|
||||||
mandatoryKeys?: Array<keyof T>;
|
mandatoryKeys?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,9 +17,9 @@ export interface IKvStoreConstructorOptions<T> {
|
|||||||
*/
|
*/
|
||||||
export class KeyValueStore<T = any> {
|
export class KeyValueStore<T = any> {
|
||||||
private dataObject: Partial<T> = {};
|
private dataObject: Partial<T> = {};
|
||||||
private deletedObject: Partial<T> = {};
|
private deletedObject: any = {};
|
||||||
private mandatoryKeys: Set<keyof T> = new Set();
|
private mandatoryKeys: Set<string> = new Set();
|
||||||
public changeSubject = new plugins.smartrx.rxjs.Subject<Partial<T>>();
|
public changeSubject = new plugins.smartrx.rxjs.Subject();
|
||||||
|
|
||||||
private storedStateString: string = '';
|
private storedStateString: string = '';
|
||||||
public syncTask = new Task({
|
public syncTask = new Task({
|
||||||
@ -28,12 +28,12 @@ export class KeyValueStore<T = any> {
|
|||||||
bufferMax: 1,
|
bufferMax: 1,
|
||||||
execDelay: 0,
|
execDelay: 0,
|
||||||
taskFunction: async () => {
|
taskFunction: async () => {
|
||||||
if (this.type !== 'ephemeral') {
|
|
||||||
this.dataObject = {
|
this.dataObject = {
|
||||||
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
...plugins.smartfile.fs.toObjectSync(this.filePath),
|
||||||
...this.dataObject,
|
...this.dataObject,
|
||||||
};
|
};
|
||||||
for (const key of Object.keys(this.deletedObject) as Array<keyof T>) {
|
for (const key of Object.keys(this.deletedObject)) {
|
||||||
delete this.dataObject[key];
|
delete this.dataObject[key];
|
||||||
}
|
}
|
||||||
this.deletedObject = {};
|
this.deletedObject = {};
|
||||||
@ -41,7 +41,6 @@ export class KeyValueStore<T = any> {
|
|||||||
plugins.smartjson.stringifyPretty(this.dataObject),
|
plugins.smartjson.stringifyPretty(this.dataObject),
|
||||||
this.filePath
|
this.filePath
|
||||||
);
|
);
|
||||||
}
|
|
||||||
const newStateString = plugins.smartjson.stringify(this.dataObject);
|
const newStateString = plugins.smartjson.stringify(this.dataObject);
|
||||||
|
|
||||||
// change detection
|
// change detection
|
||||||
@ -56,10 +55,6 @@ export class KeyValueStore<T = any> {
|
|||||||
* computes the identity and filePath
|
* computes the identity and filePath
|
||||||
*/
|
*/
|
||||||
private initFilePath = () => {
|
private initFilePath = () => {
|
||||||
if (this.type === 'ephemeral') {
|
|
||||||
// No file path is needed for ephemeral type
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (this.customPath) {
|
if (this.customPath) {
|
||||||
// Use custom path if provided
|
// Use custom path if provided
|
||||||
const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd);
|
const absolutePath = plugins.smartpath.transform.makeAbsolute(this.customPath, paths.cwd);
|
||||||
@ -85,7 +80,7 @@ export class KeyValueStore<T = any> {
|
|||||||
// if no custom path is provided, try to store at home directory
|
// if no custom path is provided, try to store at home directory
|
||||||
public type: TKeyValueStore;
|
public type: TKeyValueStore;
|
||||||
public identity: string;
|
public identity: string;
|
||||||
public filePath?: string;
|
public filePath: string;
|
||||||
private customPath?: string; // Optionally allow custom path
|
private customPath?: string; // Optionally allow custom path
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,7 +89,7 @@ export class KeyValueStore<T = any> {
|
|||||||
* @param identityArg
|
* @param identityArg
|
||||||
* @param customPath Optional custom path for the keyValue store
|
* @param customPath Optional custom path for the keyValue store
|
||||||
*/
|
*/
|
||||||
constructor(optionsArg: IKvStoreConstructorOptions<T>) {
|
constructor(optionsArg: IKvStoreConstructorOptions) {
|
||||||
if (optionsArg.customPath && optionsArg.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');
|
||||||
}
|
}
|
||||||
@ -113,7 +108,7 @@ export class KeyValueStore<T = any> {
|
|||||||
/**
|
/**
|
||||||
* reads all keyValue pairs at once and returns them
|
* reads all keyValue pairs at once and returns them
|
||||||
*/
|
*/
|
||||||
public async readAll(): Promise<Partial<T>> {
|
public async readAll() {
|
||||||
await this.syncTask.trigger();
|
await this.syncTask.trigger();
|
||||||
return this.dataObject;
|
return this.dataObject;
|
||||||
}
|
}
|
||||||
@ -121,21 +116,21 @@ export class KeyValueStore<T = any> {
|
|||||||
/**
|
/**
|
||||||
* reads a keyValueFile from disk
|
* reads a keyValueFile from disk
|
||||||
*/
|
*/
|
||||||
public async readKey<K extends keyof T>(keyArg: K): Promise<T[K]> {
|
public async readKey(keyArg: string) {
|
||||||
await this.syncTask.trigger();
|
await this.syncTask.trigger();
|
||||||
return this.dataObject[keyArg] as T[K];
|
return this.dataObject[keyArg];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* writes a specific key to the keyValueStore
|
* writes a specific key to the keyValueStore
|
||||||
*/
|
*/
|
||||||
public async writeKey<K extends keyof T>(keyArg: K, valueArg: T[K]): Promise<void> {
|
public async writeKey(keyArg: string, valueArg: any) {
|
||||||
await this.writeAll({
|
await this.writeAll({
|
||||||
[keyArg]: valueArg,
|
[keyArg]: valueArg,
|
||||||
} as unknown as Partial<T>);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteKey<K extends keyof T>(keyArg: K): Promise<void> {
|
public async deleteKey(keyArg: string) {
|
||||||
this.deletedObject[keyArg] = this.dataObject[keyArg];
|
this.deletedObject[keyArg] = this.dataObject[keyArg];
|
||||||
await this.syncTask.trigger();
|
await this.syncTask.trigger();
|
||||||
}
|
}
|
||||||
@ -143,7 +138,7 @@ export class KeyValueStore<T = any> {
|
|||||||
/**
|
/**
|
||||||
* writes all keyValue pairs in the object argument
|
* writes all keyValue pairs in the object argument
|
||||||
*/
|
*/
|
||||||
public async writeAll(keyValueObject: Partial<T>): Promise<void> {
|
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();
|
||||||
}
|
}
|
||||||
@ -151,21 +146,19 @@ export class KeyValueStore<T = any> {
|
|||||||
/**
|
/**
|
||||||
* wipes a key value store from disk
|
* wipes a key value store from disk
|
||||||
*/
|
*/
|
||||||
public async wipe(): Promise<void> {
|
public async wipe() {
|
||||||
this.dataObject = {};
|
this.dataObject = {};
|
||||||
if (this.type !== 'ephemeral') {
|
|
||||||
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
|
* resets the KeyValueStore to the initial state by syncing first, deleting all keys, and then triggering a sync again
|
||||||
*/
|
*/
|
||||||
public async reset(): Promise<void> {
|
public async reset() {
|
||||||
await this.syncTask.trigger(); // Sync to get the latest state
|
await this.syncTask.trigger(); // Sync to get the latest state
|
||||||
|
|
||||||
// Delete all keys from the dataObject and add them to deletedObject
|
// Delete all keys from the dataObject and add them to deletedObject
|
||||||
for (const key of Object.keys(this.dataObject) as Array<keyof T>) {
|
for (const key of Object.keys(this.dataObject)) {
|
||||||
this.deletedObject[key] = this.dataObject[key];
|
this.deletedObject[key] = this.dataObject[key];
|
||||||
delete this.dataObject[key];
|
delete this.dataObject[key];
|
||||||
}
|
}
|
||||||
@ -173,21 +166,21 @@ export class KeyValueStore<T = any> {
|
|||||||
await this.syncTask.trigger(); // Sync again to reflect the deletion
|
await this.syncTask.trigger(); // Sync again to reflect the deletion
|
||||||
}
|
}
|
||||||
|
|
||||||
private setMandatoryKeys(keys: Array<keyof T>) {
|
private setMandatoryKeys(keys: string[]) {
|
||||||
keys.forEach(key => this.mandatoryKeys.add(key));
|
keys.forEach(key => this.mandatoryKeys.add(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getMissingMandatoryKeys(): Promise<Array<keyof T>> {
|
public async getMissingMandatoryKeys(): Promise<string[]> {
|
||||||
await this.readAll();
|
await this.readAll();
|
||||||
return Array.from(this.mandatoryKeys).filter(key => !(key in this.dataObject));
|
return Array.from(this.mandatoryKeys).filter(key => !(key in this.dataObject));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async waitForKeysPresent<K extends keyof T>(keysArg: K[]): Promise<void> {
|
public async waitForKeysPresent(keysArg: string[]): Promise<void> {
|
||||||
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||||
if (missingKeys.length === 0) {
|
if (missingKeys.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const subscription = this.changeSubject.subscribe(() => {
|
const subscription = this.changeSubject.subscribe(() => {
|
||||||
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
const missingKeys = keysArg.filter(keyArg => !this.dataObject[keyArg]);
|
||||||
if (missingKeys.length === 0) {
|
if (missingKeys.length === 0) {
|
||||||
@ -197,9 +190,4 @@ export class KeyValueStore<T = any> {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
|
|
||||||
await this.waitForKeysPresent([keyArg]);
|
|
||||||
return this.readKey(keyArg);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,9 +1,3 @@
|
|||||||
import * as tsclass from '@tsclass/tsclass';
|
|
||||||
|
|
||||||
export {
|
|
||||||
tsclass
|
|
||||||
}
|
|
||||||
|
|
||||||
import * as qenv from '@push.rocks/qenv';
|
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';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user