@dsmrt/axiom-config 0.0.11 → 0.1.0
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/CHANGELOG.md +11 -0
- package/dist/index.d.mts +53 -0
- package/{lib → dist}/index.d.ts +12 -11
- package/dist/index.js +120 -0
- package/dist/index.mjs +98 -0
- package/lib/index.js +2 -26
- package/package.json +8 -4
- package/tsconfig.json +5 -4
package/CHANGELOG.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
interface AwsConfigs {
|
|
2
|
+
account: string;
|
|
3
|
+
region: string;
|
|
4
|
+
profile: string;
|
|
5
|
+
baseParameterPath: string;
|
|
6
|
+
}
|
|
7
|
+
interface BaseConfig {
|
|
8
|
+
name: string;
|
|
9
|
+
env: string;
|
|
10
|
+
aws: AwsConfigs;
|
|
11
|
+
prodEnvName?: string;
|
|
12
|
+
}
|
|
13
|
+
type Config<T = object> = T & BaseConfig;
|
|
14
|
+
interface ConfigMethods {
|
|
15
|
+
isProd(): boolean;
|
|
16
|
+
asParameterPath(name: string): string;
|
|
17
|
+
}
|
|
18
|
+
declare class ConfigContainer<T = object> implements BaseConfig, ConfigMethods {
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly env: string;
|
|
21
|
+
readonly aws: AwsConfigs;
|
|
22
|
+
readonly prodEnvName?: string;
|
|
23
|
+
constructor(config: T & BaseConfig);
|
|
24
|
+
isProd(): boolean;
|
|
25
|
+
asParameterPath(name: string): string;
|
|
26
|
+
}
|
|
27
|
+
interface LoadConfigInput {
|
|
28
|
+
/**
|
|
29
|
+
* defaults to prod
|
|
30
|
+
*/
|
|
31
|
+
env?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Override configs
|
|
34
|
+
*/
|
|
35
|
+
overrides?: Config;
|
|
36
|
+
/**
|
|
37
|
+
* Load config from a sub directory
|
|
38
|
+
*/
|
|
39
|
+
cwd?: string;
|
|
40
|
+
}
|
|
41
|
+
declare const importConfigFromPath: (path: string) => Config;
|
|
42
|
+
declare const loadConfig: <T extends object>(input?: LoadConfigInput) => ConfigContainer<T>;
|
|
43
|
+
/**
|
|
44
|
+
* Simple object check.
|
|
45
|
+
*/
|
|
46
|
+
declare function isObject(item: unknown): unknown;
|
|
47
|
+
/**
|
|
48
|
+
* Deep merge two objects.
|
|
49
|
+
*/
|
|
50
|
+
declare function mergeDeep(target: any, ...sources: any[]): any;
|
|
51
|
+
declare const configPath: (input?: LoadConfigInput) => string;
|
|
52
|
+
|
|
53
|
+
export { type AwsConfigs, type BaseConfig, type Config, ConfigContainer, type LoadConfigInput, configPath, importConfigFromPath, isObject, loadConfig, mergeDeep };
|
package/{lib → dist}/index.d.ts
RENAMED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
interface AwsConfigs {
|
|
2
2
|
account: string;
|
|
3
3
|
region: string;
|
|
4
4
|
profile: string;
|
|
5
5
|
baseParameterPath: string;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
interface BaseConfig {
|
|
8
8
|
name: string;
|
|
9
9
|
env: string;
|
|
10
10
|
aws: AwsConfigs;
|
|
11
11
|
prodEnvName?: string;
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
type Config<T = object> = T & BaseConfig;
|
|
14
14
|
interface ConfigMethods {
|
|
15
15
|
isProd(): boolean;
|
|
16
16
|
asParameterPath(name: string): string;
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
declare class ConfigContainer<T = object> implements BaseConfig, ConfigMethods {
|
|
19
19
|
readonly name: string;
|
|
20
20
|
readonly env: string;
|
|
21
21
|
readonly aws: AwsConfigs;
|
|
@@ -24,7 +24,7 @@ export declare class ConfigContainer<T = object> implements BaseConfig, ConfigMe
|
|
|
24
24
|
isProd(): boolean;
|
|
25
25
|
asParameterPath(name: string): string;
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
interface LoadConfigInput {
|
|
28
28
|
/**
|
|
29
29
|
* defaults to prod
|
|
30
30
|
*/
|
|
@@ -38,15 +38,16 @@ export interface LoadConfigInput {
|
|
|
38
38
|
*/
|
|
39
39
|
cwd?: string;
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
declare const importConfigFromPath: (path: string) => Config;
|
|
42
|
+
declare const loadConfig: <T extends object>(input?: LoadConfigInput) => ConfigContainer<T>;
|
|
43
43
|
/**
|
|
44
44
|
* Simple object check.
|
|
45
45
|
*/
|
|
46
|
-
|
|
46
|
+
declare function isObject(item: unknown): unknown;
|
|
47
47
|
/**
|
|
48
48
|
* Deep merge two objects.
|
|
49
49
|
*/
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
declare function mergeDeep(target: any, ...sources: any[]): any;
|
|
51
|
+
declare const configPath: (input?: LoadConfigInput) => string;
|
|
52
|
+
|
|
53
|
+
export { type AwsConfigs, type BaseConfig, type Config, ConfigContainer, type LoadConfigInput, configPath, importConfigFromPath, isObject, loadConfig, mergeDeep };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
ConfigContainer: () => ConfigContainer,
|
|
24
|
+
configPath: () => configPath,
|
|
25
|
+
importConfigFromPath: () => importConfigFromPath,
|
|
26
|
+
isObject: () => isObject,
|
|
27
|
+
loadConfig: () => loadConfig,
|
|
28
|
+
mergeDeep: () => mergeDeep
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(src_exports);
|
|
31
|
+
var import_fs = require("fs");
|
|
32
|
+
var import_find_up = require("find-up");
|
|
33
|
+
var ConfigContainer = class {
|
|
34
|
+
name;
|
|
35
|
+
env;
|
|
36
|
+
aws;
|
|
37
|
+
prodEnvName = "prod";
|
|
38
|
+
constructor(config) {
|
|
39
|
+
this.name = config.name;
|
|
40
|
+
this.env = config.env;
|
|
41
|
+
this.aws = config.aws;
|
|
42
|
+
this.prodEnvName ??= config.prodEnvName;
|
|
43
|
+
for (const prop in config) {
|
|
44
|
+
this[prop] = config[prop];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
isProd() {
|
|
48
|
+
return this.env == this.prodEnvName;
|
|
49
|
+
}
|
|
50
|
+
asParameterPath(name) {
|
|
51
|
+
return this.aws.baseParameterPath.replace(new RegExp("/+$"), "") + `/${name}`;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var importConfigFromPath = (path) => {
|
|
55
|
+
if (/\.json$/.test(path)) {
|
|
56
|
+
return JSON.parse((0, import_fs.readFileSync)(path).toString());
|
|
57
|
+
}
|
|
58
|
+
if (/\.(m)?[j|t]s$/.test(path)) {
|
|
59
|
+
return require(path);
|
|
60
|
+
}
|
|
61
|
+
throw new Error(`Path not found: {path}`);
|
|
62
|
+
};
|
|
63
|
+
var loadConfig = (input) => {
|
|
64
|
+
const baseConfigFile = configPath(input);
|
|
65
|
+
const baseConfig = importConfigFromPath(baseConfigFile);
|
|
66
|
+
let overrides = input?.overrides || {};
|
|
67
|
+
if (input?.env) {
|
|
68
|
+
const devConfig = importConfigFromPath(configPath(input));
|
|
69
|
+
overrides = mergeDeep(devConfig, overrides);
|
|
70
|
+
}
|
|
71
|
+
const configObject = mergeDeep(baseConfig, overrides);
|
|
72
|
+
return new ConfigContainer(configObject);
|
|
73
|
+
};
|
|
74
|
+
function isObject(item) {
|
|
75
|
+
return item && typeof item === "object" && !Array.isArray(item);
|
|
76
|
+
}
|
|
77
|
+
function mergeDeep(target, ...sources) {
|
|
78
|
+
if (!sources.length)
|
|
79
|
+
return target;
|
|
80
|
+
const source = sources.shift();
|
|
81
|
+
if (isObject(target) && isObject(source)) {
|
|
82
|
+
for (const key in source) {
|
|
83
|
+
if (isObject(source[key])) {
|
|
84
|
+
if (!target[key])
|
|
85
|
+
Object.assign(target, { [key]: {} });
|
|
86
|
+
mergeDeep(target[key], source[key]);
|
|
87
|
+
} else {
|
|
88
|
+
Object.assign(target, { [key]: source[key] });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return mergeDeep(target, ...sources);
|
|
93
|
+
}
|
|
94
|
+
var configPath = (input) => {
|
|
95
|
+
const envIndicator = input?.env ? `.${input.env}` : "";
|
|
96
|
+
const p = (0, import_find_up.sync)(
|
|
97
|
+
[
|
|
98
|
+
`.axiom${envIndicator}.json`,
|
|
99
|
+
`.axiom${envIndicator}.js`,
|
|
100
|
+
`.axiom${envIndicator}.mjs`,
|
|
101
|
+
`.axiom${envIndicator}.ts`,
|
|
102
|
+
`.axiom${envIndicator}.mts`
|
|
103
|
+
],
|
|
104
|
+
{ cwd: input?.cwd }
|
|
105
|
+
);
|
|
106
|
+
if (p === void 0)
|
|
107
|
+
throw new Error(
|
|
108
|
+
`Axiom config files not found: .axiom${envIndicator}.json, .axiom${envIndicator}.js .axiom${envIndicator}.ts`
|
|
109
|
+
);
|
|
110
|
+
return p;
|
|
111
|
+
};
|
|
112
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
113
|
+
0 && (module.exports = {
|
|
114
|
+
ConfigContainer,
|
|
115
|
+
configPath,
|
|
116
|
+
importConfigFromPath,
|
|
117
|
+
isObject,
|
|
118
|
+
loadConfig,
|
|
119
|
+
mergeDeep
|
|
120
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/index.ts
|
|
10
|
+
import { readFileSync } from "fs";
|
|
11
|
+
import { sync as findUpSync } from "find-up";
|
|
12
|
+
var ConfigContainer = class {
|
|
13
|
+
name;
|
|
14
|
+
env;
|
|
15
|
+
aws;
|
|
16
|
+
prodEnvName = "prod";
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.name = config.name;
|
|
19
|
+
this.env = config.env;
|
|
20
|
+
this.aws = config.aws;
|
|
21
|
+
this.prodEnvName ??= config.prodEnvName;
|
|
22
|
+
for (const prop in config) {
|
|
23
|
+
this[prop] = config[prop];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
isProd() {
|
|
27
|
+
return this.env == this.prodEnvName;
|
|
28
|
+
}
|
|
29
|
+
asParameterPath(name) {
|
|
30
|
+
return this.aws.baseParameterPath.replace(new RegExp("/+$"), "") + `/${name}`;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var importConfigFromPath = (path) => {
|
|
34
|
+
if (/\.json$/.test(path)) {
|
|
35
|
+
return JSON.parse(readFileSync(path).toString());
|
|
36
|
+
}
|
|
37
|
+
if (/\.(m)?[j|t]s$/.test(path)) {
|
|
38
|
+
return __require(path);
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`Path not found: {path}`);
|
|
41
|
+
};
|
|
42
|
+
var loadConfig = (input) => {
|
|
43
|
+
const baseConfigFile = configPath(input);
|
|
44
|
+
const baseConfig = importConfigFromPath(baseConfigFile);
|
|
45
|
+
let overrides = input?.overrides || {};
|
|
46
|
+
if (input?.env) {
|
|
47
|
+
const devConfig = importConfigFromPath(configPath(input));
|
|
48
|
+
overrides = mergeDeep(devConfig, overrides);
|
|
49
|
+
}
|
|
50
|
+
const configObject = mergeDeep(baseConfig, overrides);
|
|
51
|
+
return new ConfigContainer(configObject);
|
|
52
|
+
};
|
|
53
|
+
function isObject(item) {
|
|
54
|
+
return item && typeof item === "object" && !Array.isArray(item);
|
|
55
|
+
}
|
|
56
|
+
function mergeDeep(target, ...sources) {
|
|
57
|
+
if (!sources.length)
|
|
58
|
+
return target;
|
|
59
|
+
const source = sources.shift();
|
|
60
|
+
if (isObject(target) && isObject(source)) {
|
|
61
|
+
for (const key in source) {
|
|
62
|
+
if (isObject(source[key])) {
|
|
63
|
+
if (!target[key])
|
|
64
|
+
Object.assign(target, { [key]: {} });
|
|
65
|
+
mergeDeep(target[key], source[key]);
|
|
66
|
+
} else {
|
|
67
|
+
Object.assign(target, { [key]: source[key] });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return mergeDeep(target, ...sources);
|
|
72
|
+
}
|
|
73
|
+
var configPath = (input) => {
|
|
74
|
+
const envIndicator = input?.env ? `.${input.env}` : "";
|
|
75
|
+
const p = findUpSync(
|
|
76
|
+
[
|
|
77
|
+
`.axiom${envIndicator}.json`,
|
|
78
|
+
`.axiom${envIndicator}.js`,
|
|
79
|
+
`.axiom${envIndicator}.mjs`,
|
|
80
|
+
`.axiom${envIndicator}.ts`,
|
|
81
|
+
`.axiom${envIndicator}.mts`
|
|
82
|
+
],
|
|
83
|
+
{ cwd: input?.cwd }
|
|
84
|
+
);
|
|
85
|
+
if (p === void 0)
|
|
86
|
+
throw new Error(
|
|
87
|
+
`Axiom config files not found: .axiom${envIndicator}.json, .axiom${envIndicator}.js .axiom${envIndicator}.ts`
|
|
88
|
+
);
|
|
89
|
+
return p;
|
|
90
|
+
};
|
|
91
|
+
export {
|
|
92
|
+
ConfigContainer,
|
|
93
|
+
configPath,
|
|
94
|
+
importConfigFromPath,
|
|
95
|
+
isObject,
|
|
96
|
+
loadConfig,
|
|
97
|
+
mergeDeep
|
|
98
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -1,31 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.configPath = exports.mergeDeep = exports.isObject = exports.loadConfig = exports.importConfigFromPath =
|
|
3
|
+
exports.configPath = exports.mergeDeep = exports.isObject = exports.loadConfig = exports.importConfigFromPath = void 0;
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const find_up_1 = require("find-up");
|
|
6
|
-
class ConfigContainer {
|
|
7
|
-
name;
|
|
8
|
-
env;
|
|
9
|
-
aws;
|
|
10
|
-
prodEnvName = "prod";
|
|
11
|
-
constructor(config) {
|
|
12
|
-
this.name = config.name;
|
|
13
|
-
this.env = config.env;
|
|
14
|
-
this.aws = config.aws;
|
|
15
|
-
this.prodEnvName ??= config.prodEnvName;
|
|
16
|
-
for (const prop in config) {
|
|
17
|
-
// @ts-expect-error due to the interface being dynamic, we can't predict their properties
|
|
18
|
-
this[prop] = config[prop];
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
isProd() {
|
|
22
|
-
return this.env == this.prodEnvName;
|
|
23
|
-
}
|
|
24
|
-
asParameterPath(name) {
|
|
25
|
-
return (this.aws.baseParameterPath.replace(new RegExp("/+$"), "") + `/${name}`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
exports.ConfigContainer = ConfigContainer;
|
|
29
6
|
// TODO - Add validation here
|
|
30
7
|
const importConfigFromPath = (path) => {
|
|
31
8
|
if (/\.json$/.test(path)) {
|
|
@@ -48,9 +25,8 @@ const loadConfig = (input) => {
|
|
|
48
25
|
const devConfig = (0, exports.importConfigFromPath)((0, exports.configPath)(input));
|
|
49
26
|
overrides = mergeDeep(devConfig, overrides);
|
|
50
27
|
}
|
|
51
|
-
const configObject = mergeDeep(baseConfig, overrides);
|
|
52
28
|
// merge env file
|
|
53
|
-
return
|
|
29
|
+
return mergeDeep(baseConfig, overrides);
|
|
54
30
|
};
|
|
55
31
|
exports.loadConfig = loadConfig;
|
|
56
32
|
/**
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dsmrt/axiom-config",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Config library for axiom cli",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
6
8
|
"repository": {
|
|
7
9
|
"type": "git",
|
|
8
10
|
"url": "git@github.com:dsmrt/axiom.git",
|
|
@@ -22,6 +24,7 @@
|
|
|
22
24
|
},
|
|
23
25
|
"license": "MIT",
|
|
24
26
|
"devDependencies": {
|
|
27
|
+
"@changesets/cli": "^2.27.1",
|
|
25
28
|
"@types/node": "^20.9.0",
|
|
26
29
|
"@types/yargs": "^17.0.31",
|
|
27
30
|
"@typescript-eslint/eslint-plugin": "^6.12.0",
|
|
@@ -30,6 +33,7 @@
|
|
|
30
33
|
"eslint": "^8.54.0",
|
|
31
34
|
"prettier": "^3.1.0",
|
|
32
35
|
"ts-node": "^10.9.1",
|
|
36
|
+
"tsup": "^8.0.1",
|
|
33
37
|
"typescript": "^5.2.2",
|
|
34
38
|
"vitest": "^0.34.6"
|
|
35
39
|
},
|
|
@@ -42,7 +46,7 @@
|
|
|
42
46
|
"lint:fix": "eslint ./src/ --ext .ts --fix",
|
|
43
47
|
"test": "vitest run --coverage -c ./vitest.config.ts",
|
|
44
48
|
"watch": "vitest watch --coverage -c ./vitest.config.ts",
|
|
45
|
-
"build": "
|
|
46
|
-
"
|
|
49
|
+
"build": "tsup --entry ./src/bin/axiom.ts --entry ./src/index.ts --format cjs,esm --dts --clean",
|
|
50
|
+
"release": "pnpm run build && pnpm changeset publish"
|
|
47
51
|
}
|
|
48
52
|
}
|
package/tsconfig.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "es2022",
|
|
4
|
-
"module": "
|
|
5
|
-
"strict": true,
|
|
4
|
+
"module": "commonjs",
|
|
6
5
|
"esModuleInterop": true,
|
|
7
|
-
"
|
|
8
|
-
"
|
|
6
|
+
"forceConsistentCasingInFileNames": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"noEmit": true
|
|
9
10
|
},
|
|
10
11
|
"include": ["src/index.ts"]
|
|
11
12
|
}
|