Files

323 lines
11 KiB
Markdown
Raw Permalink Normal View History

# @push.rocks/smartconfig 🚀
2024-04-14 03:45:31 +02:00
A comprehensive TypeScript configuration management library providing centralized tool configs, persistent key-value storage, and powerful environment variable mapping with automatic type conversions.
## Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
## Install 📦
2024-04-14 03:45:31 +02:00
```bash
npm install @push.rocks/smartconfig --save
# or
pnpm add @push.rocks/smartconfig
2024-04-14 03:45:31 +02:00
```
## Quick Start ⚡
```typescript
import { Smartconfig, AppData, KeyValueStore } from '@push.rocks/smartconfig';
2021-01-27 21:10:31 +00:00
// 1. Read tool config from .smartconfig.json
const sc = new Smartconfig();
const eslintOpts = sc.dataFor('eslint', { extends: 'standard' });
2024-04-14 03:45:31 +02:00
// 2. Map env vars to typed config (with auto-conversion)
const appData = await AppData.createAndInit<{ port: number; debug: boolean }>({
envMapping: {
port: 'PORT',
debug: 'boolean:DEBUG',
},
});
2021-01-27 21:10:31 +00:00
// 3. Persist data between runs
const kv = new KeyValueStore({ typeArg: 'userHomeDir', identityArg: 'myapp' });
await kv.writeKey('lastRun', Date.now());
```
## Three Core Classes 🏗️
### 1. `Smartconfig` — Centralized Tool Configuration
2024-04-14 03:45:31 +02:00
Reads a `.smartconfig.json` file from a project directory and merges its contents with your defaults. One file, every tool.
2021-01-27 21:10:31 +00:00
2024-04-14 03:45:31 +02:00
```typescript
import { Smartconfig } from '@push.rocks/smartconfig';
2021-01-27 21:10:31 +00:00
const sc = new Smartconfig(); // uses cwd
const sc2 = new Smartconfig('/my/project'); // or specify a path
2021-01-27 21:10:31 +00:00
const prettierConfig = sc.dataFor<PrettierConfig>('prettier', {
semi: false,
singleQuote: true,
});
```
2021-01-27 21:10:31 +00:00
**`.smartconfig.json`** example:
2021-01-27 21:10:31 +00:00
```json
{
"prettier": {
"semi": true,
"printWidth": 120
},
"eslint": {
"extends": "@company/eslint-config"
2021-01-27 21:10:31 +00:00
}
}
```
Values from the file override the defaults you pass in. Missing keys fall back to your defaults.
2024-04-14 03:45:31 +02:00
**Properties:**
- `smartconfigJsonExists: boolean` — whether `.smartconfig.json` was found
- `smartconfigJsonData: any` — the parsed JSON contents
**Methods:**
- `dataFor<T>(toolName: string, defaults: T): T` — returns merged config
---
### 2. `KeyValueStore<T>` — Persistent Data Storage
A generic, typed key-value store that persists JSON to disk (or stays in-memory for tests). Supports change detection via RxJS observables.
2024-04-14 03:45:31 +02:00
2021-01-27 21:10:31 +00:00
```typescript
import { KeyValueStore } from '@push.rocks/smartconfig';
2024-04-14 03:45:31 +02:00
interface Settings {
username: string;
theme: 'light' | 'dark';
}
2021-01-27 21:10:31 +00:00
// Store in ~/.smartconfig/kv/
const kv = new KeyValueStore<Settings>({
typeArg: 'userHomeDir',
identityArg: 'myApp',
mandatoryKeys: ['username'],
2021-01-27 21:10:31 +00:00
});
2024-04-14 03:45:31 +02:00
await kv.writeKey('username', 'jane');
await kv.writeKey('theme', 'dark');
2024-04-14 03:45:31 +02:00
const user = await kv.readKey('username'); // 'jane'
const all = await kv.readAll(); // { username: 'jane', theme: 'dark' }
2024-04-14 03:45:31 +02:00
// React to changes
kv.changeSubject.subscribe((data) => console.log('changed:', data));
2024-04-14 03:45:31 +02:00
```
**Storage types:**
| `typeArg` | Where it goes | Use case |
|-----------|--------------|----------|
| `'userHomeDir'` | `~/.smartconfig/kv/<identity>.json` | CLI tools, per-user state |
| `'custom'` | Your path (file or directory) | App data, project-local state |
| `'ephemeral'` | Memory only — nothing on disk | Tests |
**Methods:**
| Method | Description |
|--------|------------|
| `readKey(key)` | Read a single value |
| `writeKey(key, value)` | Write a single value |
| `readAll()` | Read everything |
| `writeAll(obj)` | Merge an object into the store |
| `deleteKey(key)` | Remove a key |
| `reset()` | Wipe all keys (synced to disk) |
| `wipe()` | Delete the backing file entirely |
| `getMissingMandatoryKeys()` | Returns keys declared mandatory but not yet set |
| `waitForKeysPresent(keys)` | Returns a Promise that resolves once all listed keys exist |
| `waitForAndGetKey(key)` | Waits for a key, then returns its value |
---
2024-06-12 20:10:19 +02:00
### 3. `AppData<T>` — Environment Variable Mapping 🌟
2024-06-12 20:10:19 +02:00
The flagship class. Maps environment variables (or hardcoded values) into a typed config object with automatic type conversions, nested object support, and smart storage path selection.
2024-06-12 20:18:27 +02:00
2024-06-12 20:10:19 +02:00
```typescript
import { AppData } from '@push.rocks/smartconfig';
2024-06-12 20:10:19 +02:00
interface Config {
apiUrl: string;
apiKey: string;
features: {
analytics: boolean;
payment: boolean;
};
redis: {
host: string;
password: string;
};
2024-06-12 20:10:19 +02:00
}
const appData = await AppData.createAndInit<Config>({
requiredKeys: ['apiKey'],
envMapping: {
apiUrl: 'API_URL', // plain env var
apiKey: 'hard:dev-key-123', // hardcoded fallback
features: {
analytics: 'boolean:ENABLE_ANALYTICS', // converts "true"/"false" → boolean
payment: 'hard_boolean:true', // hardcoded boolean
},
redis: {
host: 'REDIS_HOST',
password: 'base64:REDIS_PASSWORD_B64', // base64-decode at load time
},
},
overwriteObject: {
apiUrl: 'http://localhost:3000', // force override after env mapping
},
2024-06-12 20:10:19 +02:00
});
const store = await appData.getKvStore();
const url = await store.readKey('apiUrl');
```
2024-06-12 20:10:19 +02:00
#### Mapping Prefixes
2024-06-12 20:10:19 +02:00
| Prefix | What it does | Example mapping | Result |
|--------|-------------|-----------------|--------|
| *(none)* | Raw env var as string | `'MY_VAR'` | `process.env.MY_VAR` |
| `hard:` | Hardcoded string | `'hard:hello'` | `"hello"` |
| `boolean:` | Env var → `true`/`false` | `'boolean:FLAG'` | `true` or `false` |
| `json:` | Env var → `JSON.parse()` | `'json:CONFIG'` | parsed object |
| `base64:` | Env var → base64 decode | `'base64:SECRET'` | decoded string |
| `hard_boolean:` | Hardcoded boolean | `'hard_boolean:false'` | `false` |
| `hard_json:` | Hardcoded JSON | `'hard_json:{"a":1}'` | `{ a: 1 }` |
| `hard_base64:` | Hardcoded base64 | `'hard_base64:SGVsbG8='` | `"Hello"` |
2024-06-12 20:10:19 +02:00
Suffix detection also works: a mapping ending in `_JSON` or `_BASE64` triggers the corresponding transform automatically.
2024-06-12 20:10:19 +02:00
#### Boolean Conversion Rules
2024-06-12 20:18:27 +02:00
The `boolean:` prefix (and `hard_boolean:`) recognizes:
2024-06-12 20:10:19 +02:00
- **true**: `"true"`, `"1"`, `"yes"`, `"y"`, `"on"` (case-insensitive)
- **false**: `"false"`, `"0"`, `"no"`, `"n"`, `"off"` (case-insensitive)
2024-06-12 20:10:19 +02:00
#### Nested Objects
Mapping values can be objects — they are resolved recursively:
2024-06-12 20:10:19 +02:00
```typescript
envMapping: {
database: {
host: 'DB_HOST',
port: 'hard:5432',
credentials: {
user: 'DB_USER',
password: 'base64:DB_PASS_B64',
ssl: 'boolean:DB_SSL',
},
},
}
```
#### Smart Storage Path
2024-06-12 20:10:19 +02:00
When no `dirPath` is specified, AppData auto-selects:
2024-06-12 20:10:19 +02:00
1. `/app/data` — if it exists (containers)
2. `/data` — if it exists (alternate container path)
3. `.nogit/appdata` — local dev fallback
2024-06-12 20:10:19 +02:00
Or pass `ephemeral: true` for zero disk I/O (great for tests).
2024-06-12 20:10:19 +02:00
#### Static Helpers
2024-06-12 20:10:19 +02:00
Quick one-shot env var reads without creating an AppData instance:
2024-06-12 20:10:19 +02:00
```typescript
const isEnabled = await AppData.valueAsBoolean('FEATURE_FLAG');
const config = await AppData.valueAsJson<MyType>('CONFIG_JSON');
const secret = await AppData.valueAsBase64('ENCODED_SECRET');
const url = await AppData.valueAsString('API_URL');
const port = await AppData.valueAsNumber('PORT');
2024-06-12 20:10:19 +02:00
```
#### Instance Methods
2024-06-12 20:10:19 +02:00
| Method | Description |
|--------|------------|
| `getKvStore()` | Returns the underlying `KeyValueStore<T>` |
| `logMissingKeys()` | Logs and returns any required keys that are missing |
| `waitForAndGetKey(key)` | Blocks until a key is present, then returns it |
2024-06-12 20:10:19 +02:00
---
2024-06-12 20:10:19 +02:00
## Security 🔐
AppData automatically redacts sensitive values in its console logs. Keys matching patterns like `secret`, `token`, `password`, `api`, `auth`, `jwt`, etc. are truncated. JWT tokens (starting with `eyJ`) are also detected and shortened. Your actual stored values are never modified — only log output is redacted.
---
2024-06-12 20:10:19 +02:00
## Real-World Example 🌍
2024-06-12 20:10:19 +02:00
```typescript
import { Smartconfig, AppData, KeyValueStore } from '@push.rocks/smartconfig';
interface CliConfig {
githubToken: string;
model: 'gpt-3' | 'gpt-4';
cache: { enabled: boolean; ttl: number };
}
2024-06-12 20:10:19 +02:00
// Tool-level config from .smartconfig.json
const sc = new Smartconfig();
const toolDefaults = sc.dataFor('mycli', { defaultModel: 'gpt-3' });
// Env-mapped runtime config
const appData = await AppData.createAndInit<CliConfig>({
requiredKeys: ['githubToken'],
envMapping: {
githubToken: 'GITHUB_TOKEN',
model: 'hard:gpt-4',
cache: {
enabled: 'boolean:ENABLE_CACHE',
ttl: 'hard:3600',
},
},
});
2024-06-12 20:10:19 +02:00
// Persistent user-level cache
const cache = new KeyValueStore({
typeArg: 'userHomeDir',
identityArg: 'mycli-cache',
});
// Check mandatory keys
const missing = await appData.logMissingKeys();
if (missing.length > 0) {
console.error('Missing config — set these env vars and retry.');
process.exit(1);
}
const store = await appData.getKvStore();
const settings = await store.readAll();
console.log(`Model: ${settings.model}, Cache: ${settings.cache.enabled}`);
```
2024-06-12 20:18:27 +02:00
2024-04-14 03:45:31 +02:00
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
2024-04-14 03:45:31 +02:00
**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.
2021-01-27 21:10:31 +00:00
2024-04-14 03:45:31 +02:00
### Trademarks
2021-01-27 21:10:31 +00:00
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 or third parties, 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 or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
2021-01-27 21:10:31 +00:00
2024-04-14 03:45:31 +02:00
### Company Information
2021-01-27 21:10:31 +00:00
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
2021-01-27 21:10:31 +00:00
For any legal inquiries or further information, please contact us via email at hello@task.vc.
2021-01-27 21:10:31 +00:00
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.