@dsmrt/axiom-config 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ export interface AwsConfigs {
2
+ account: string;
3
+ region: string;
4
+ profile: string;
5
+ baseParameterPath: string;
6
+ }
7
+ export interface Config {
8
+ name: string;
9
+ env: string;
10
+ aws: AwsConfigs;
11
+ }
12
+ export interface LoadConfigInput {
13
+ /**
14
+ * defaults to prod
15
+ */
16
+ env?: string;
17
+ /**
18
+ * Override configs
19
+ */
20
+ overrides?: Config;
21
+ /**
22
+ * Load config from a sub directory
23
+ */
24
+ cwd?: string;
25
+ }
26
+ export declare const importConfigFromPath: (path: string) => Config;
27
+ export declare const loadConfig: <T extends object>(input?: LoadConfigInput) => Config & T;
28
+ /**
29
+ * Simple object check.
30
+ */
31
+ export declare function isObject(item: unknown): unknown;
32
+ /**
33
+ * Deep merge two objects.
34
+ */
35
+ export declare function mergeDeep(target: any, ...sources: any[]): any;
36
+ export declare const configPath: (input?: LoadConfigInput) => string;
package/lib/index.js ADDED
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.configPath = exports.mergeDeep = exports.isObject = exports.loadConfig = exports.importConfigFromPath = void 0;
4
+ const fs_1 = require("fs");
5
+ const find_up_1 = require("find-up");
6
+ // TODO - Add validation here
7
+ const importConfigFromPath = (path) => {
8
+ if (/\.json$/.test(path)) {
9
+ return JSON.parse((0, fs_1.readFileSync)(path).toString());
10
+ }
11
+ if (/\.(m)?[j|t]s$/.test(path)) {
12
+ /* eslint-disable */
13
+ return require(path);
14
+ }
15
+ throw new Error(`Path not found: {path}`);
16
+ };
17
+ exports.importConfigFromPath = importConfigFromPath;
18
+ const loadConfig = (input) => {
19
+ // get the base file
20
+ const baseConfigFile = (0, exports.configPath)(input);
21
+ const baseConfig = (0, exports.importConfigFromPath)(baseConfigFile);
22
+ let overrides = input?.overrides || {};
23
+ // get the environment file
24
+ if (input?.env) {
25
+ const devConfig = (0, exports.importConfigFromPath)((0, exports.configPath)(input));
26
+ overrides = mergeDeep(devConfig, overrides);
27
+ }
28
+ // merge env file
29
+ return mergeDeep(baseConfig, overrides);
30
+ };
31
+ exports.loadConfig = loadConfig;
32
+ /**
33
+ * Simple object check.
34
+ */
35
+ function isObject(item) {
36
+ return item && typeof item === "object" && !Array.isArray(item);
37
+ }
38
+ exports.isObject = isObject;
39
+ /**
40
+ * Deep merge two objects.
41
+ */
42
+ function mergeDeep(target, ...sources) {
43
+ if (!sources.length)
44
+ return target;
45
+ const source = sources.shift();
46
+ if (isObject(target) && isObject(source)) {
47
+ for (const key in source) {
48
+ if (isObject(source[key])) {
49
+ if (!target[key])
50
+ Object.assign(target, { [key]: {} });
51
+ mergeDeep(target[key], source[key]);
52
+ }
53
+ else {
54
+ Object.assign(target, { [key]: source[key] });
55
+ }
56
+ }
57
+ }
58
+ return mergeDeep(target, ...sources);
59
+ }
60
+ exports.mergeDeep = mergeDeep;
61
+ const configPath = (input) => {
62
+ const envIndicator = input?.env ? `.${input.env}` : "";
63
+ const p = (0, find_up_1.sync)([
64
+ `.axiom${envIndicator}.json`,
65
+ `.axiom${envIndicator}.js`,
66
+ `.axiom${envIndicator}.mjs`,
67
+ `.axiom${envIndicator}.ts`,
68
+ `.axiom${envIndicator}.mts`,
69
+ ], { cwd: input?.cwd });
70
+ if (p === undefined)
71
+ throw new Error(`Axiom config files not found: .axiom${envIndicator}.json, .axiom${envIndicator}.js .axiom${envIndicator}.ts`);
72
+ return p;
73
+ };
74
+ exports.configPath = configPath;
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@dsmrt/axiom-config",
3
+ "version": "0.0.1",
4
+ "description": "Config library for axiom cli",
5
+ "main": "lib/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git@github.com:dsmrt/axiom.git",
9
+ "directory": "config"
10
+ },
11
+ "publishConfig": {
12
+ "registry": "https://registry.npmjs.org/"
13
+ },
14
+ "scripts": {
15
+ "lint": "eslint ./src/ --ext .ts",
16
+ "lint:fix": "eslint ./src/ --ext .ts --fix",
17
+ "test": "vitest run --coverage -c ./vitest.config.ts",
18
+ "watch": "vitest watch --coverage -c ./vitest.config.ts",
19
+ "build": "tsc -d -p ./tsconfig.json"
20
+ },
21
+ "keywords": [
22
+ "axiom",
23
+ "config",
24
+ "aws"
25
+ ],
26
+ "author": {
27
+ "name": "Damien Smrt",
28
+ "url": "https://dsmrt.com"
29
+ },
30
+ "license": "MIT",
31
+ "devDependencies": {
32
+ "@types/node": "^20.9.0",
33
+ "@types/yargs": "^17.0.31",
34
+ "@typescript-eslint/eslint-plugin": "^6.12.0",
35
+ "@typescript-eslint/parser": "^6.12.0",
36
+ "@vitest/coverage-v8": "^0.34.6",
37
+ "eslint": "^8.54.0",
38
+ "prettier": "^3.1.0",
39
+ "ts-node": "^10.9.1",
40
+ "typescript": "^5.2.2",
41
+ "vitest": "^0.34.6"
42
+ },
43
+ "dependencies": {
44
+ "find-up": "^5.0.0",
45
+ "yargs": "^17.7.2"
46
+ }
47
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @type {import("../../index").Config}
3
+ */
4
+ const config = {
5
+ name: "my-prod-app",
6
+ env: "dev",
7
+ aws: {
8
+ account: "dev-account",
9
+ profile: "dev-profile",
10
+ region: "us-north-1",
11
+ baseParameterPath: "/dev-path",
12
+ },
13
+ };
14
+
15
+ module.exports = config;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @type {import("../../index").Config}
3
+ */
4
+ const config = {
5
+ name: "my-prod-app",
6
+ env: "prod",
7
+ aws: {
8
+ account: "prod-account",
9
+ profile: "prod-profile",
10
+ region: "us-east-1",
11
+ baseParameterPath: "/prod-path",
12
+ },
13
+ };
14
+ module.exports = config;
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "my-prod-app",
3
+ "env": "dev",
4
+ "aws": {
5
+ "account": "dev-account",
6
+ "profile": "dev-profile",
7
+ "region": "us-north-1",
8
+ "baseParameterPath": "/dev-path"
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "my-prod-app",
3
+ "env": "prod",
4
+ "aws": {
5
+ "account": "prod-account",
6
+ "profile": "prod-profile",
7
+ "region": "us-east-1",
8
+ "baseParameterPath": "/prod-path"
9
+ }
10
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2022",
4
+ "module": "node16",
5
+ "strict": true,
6
+ "esModuleInterop": true,
7
+ "moduleResolution": "node16",
8
+ "outDir": "lib"
9
+ },
10
+ "include": ["src/index.ts"]
11
+ }