@dsmrt/axiom-config 1.0.0 → 1.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 +6 -0
- package/dist/index.js +87 -18
- package/dist/index.mjs +87 -18
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -29,7 +29,13 @@ __export(index_exports, {
|
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(index_exports);
|
|
31
31
|
var import_node_fs = require("fs");
|
|
32
|
-
var
|
|
32
|
+
var import_glob = require("glob");
|
|
33
|
+
var isDebugEnabled = () => process.env.AXIOM_DEBUG === "true" || process.env.AXIOM_DEBUG === "1";
|
|
34
|
+
var debug = (message, ...args) => {
|
|
35
|
+
if (isDebugEnabled()) {
|
|
36
|
+
console.error(`[axiom:config] ${message}`, ...args);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
33
39
|
var ConfigContainer = class {
|
|
34
40
|
name;
|
|
35
41
|
env;
|
|
@@ -52,31 +58,76 @@ var ConfigContainer = class {
|
|
|
52
58
|
}
|
|
53
59
|
};
|
|
54
60
|
var importConfigFromPath = async (path) => {
|
|
61
|
+
debug(`Attempting to import config from: ${path}`);
|
|
55
62
|
if (/\.json$/.test(path)) {
|
|
56
|
-
|
|
63
|
+
debug(`Loading JSON config file: ${path}`);
|
|
64
|
+
try {
|
|
65
|
+
const config = JSON.parse((0, import_node_fs.readFileSync)(path).toString());
|
|
66
|
+
debug(`Successfully loaded JSON config with name: ${config.name}`);
|
|
67
|
+
return config;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
debug(`Failed to parse JSON config: ${error}`);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
57
72
|
}
|
|
58
73
|
if (/\.((m)?ts|mjs)$/.test(path)) {
|
|
59
|
-
|
|
60
|
-
|
|
74
|
+
debug(`Loading TypeScript/ESM config file: ${path}`);
|
|
75
|
+
try {
|
|
76
|
+
const loaded = await import(path);
|
|
77
|
+
const config = loaded.default || loaded;
|
|
78
|
+
debug(`Successfully loaded TS/ESM config with name: ${config.name}`);
|
|
79
|
+
return config;
|
|
80
|
+
} catch (error) {
|
|
81
|
+
debug(`Failed to import TS/ESM config: ${error}`);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
61
84
|
}
|
|
62
85
|
if (/\.(m)?js$/.test(path)) {
|
|
63
|
-
|
|
64
|
-
|
|
86
|
+
debug(`Loading JavaScript config file: ${path}`);
|
|
87
|
+
try {
|
|
88
|
+
const loaded = require(path);
|
|
89
|
+
const config = loaded.default || loaded;
|
|
90
|
+
debug(`Successfully loaded JS config with name: ${config.name}`);
|
|
91
|
+
return config;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
debug(`Failed to require JS config: ${error}`);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
65
96
|
}
|
|
97
|
+
debug(`Unsupported file type: ${path}`);
|
|
66
98
|
throw new Error(`Unsupported file type or path not found: ${path}`);
|
|
67
99
|
};
|
|
68
100
|
var loadConfig = async (input) => {
|
|
101
|
+
debug(
|
|
102
|
+
`Loading config with options:`,
|
|
103
|
+
JSON.stringify({
|
|
104
|
+
env: input?.env,
|
|
105
|
+
cwd: input?.cwd,
|
|
106
|
+
hasOverrides: !!input?.overrides
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
debug(`Looking for base config file...`);
|
|
69
110
|
const baseConfigFile = configPath({
|
|
70
111
|
...input,
|
|
71
112
|
env: void 0
|
|
72
113
|
});
|
|
114
|
+
debug(`Loading base config from: ${baseConfigFile}`);
|
|
73
115
|
const baseConfig = await importConfigFromPath(baseConfigFile);
|
|
116
|
+
debug(`Base config loaded: ${baseConfig.name} (env: ${baseConfig.env})`);
|
|
74
117
|
let overrides = input?.overrides || {};
|
|
75
118
|
if (input?.env) {
|
|
76
|
-
|
|
119
|
+
debug(`Looking for environment-specific config for: ${input.env}`);
|
|
120
|
+
const envConfigPath = configPath(input);
|
|
121
|
+
debug(`Loading env config from: ${envConfigPath}`);
|
|
122
|
+
const devConfig = await importConfigFromPath(envConfigPath);
|
|
123
|
+
debug(`Env config loaded: ${devConfig.name} (env: ${devConfig.env})`);
|
|
77
124
|
overrides = mergeDeep(devConfig, overrides);
|
|
125
|
+
debug(`Merged env config with overrides`);
|
|
78
126
|
}
|
|
79
127
|
const configObject = mergeDeep(baseConfig, overrides);
|
|
128
|
+
debug(
|
|
129
|
+
`Final config: ${configObject.name} (env: ${configObject.env}, isProd: ${configObject.env === (configObject.prodEnvName || "prod")})`
|
|
130
|
+
);
|
|
80
131
|
return new ConfigContainer(configObject);
|
|
81
132
|
};
|
|
82
133
|
function isObject(item) {
|
|
@@ -99,21 +150,39 @@ function mergeDeep(target, ...sources) {
|
|
|
99
150
|
}
|
|
100
151
|
var configPath = (input) => {
|
|
101
152
|
const envIndicator = input?.env ? `.${input.env}` : "";
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
`.axiom${envIndicator}.ts`,
|
|
108
|
-
`.axiom${envIndicator}.mts`
|
|
109
|
-
],
|
|
110
|
-
{ cwd: input?.cwd }
|
|
153
|
+
const extensions = ["json", "js", "mjs", "ts", "mts"];
|
|
154
|
+
const pattern = `.axiom${envIndicator}.{${extensions.join(",")}}`;
|
|
155
|
+
debug(
|
|
156
|
+
`Searching for config files with pattern: ${pattern}`,
|
|
157
|
+
input?.cwd ? `from ${input.cwd}` : "from current directory"
|
|
111
158
|
);
|
|
112
|
-
|
|
159
|
+
const cwd = input?.cwd || process.cwd();
|
|
160
|
+
let currentDir = cwd;
|
|
161
|
+
let found;
|
|
162
|
+
while (true) {
|
|
163
|
+
const matches = (0, import_glob.globSync)(pattern, {
|
|
164
|
+
cwd: currentDir,
|
|
165
|
+
absolute: true,
|
|
166
|
+
nodir: true
|
|
167
|
+
});
|
|
168
|
+
if (matches.length > 0) {
|
|
169
|
+
found = matches[0];
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
const parent = require("path").dirname(currentDir);
|
|
173
|
+
if (parent === currentDir) {
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
currentDir = parent;
|
|
177
|
+
}
|
|
178
|
+
if (found === void 0) {
|
|
179
|
+
debug(`Config file not found! Searched for pattern: ${pattern}`);
|
|
113
180
|
throw new Error(
|
|
114
181
|
`Axiom config files not found: .axiom${envIndicator}.json, .axiom${envIndicator}.js .axiom${envIndicator}.ts`
|
|
115
182
|
);
|
|
116
|
-
|
|
183
|
+
}
|
|
184
|
+
debug(`Found config file: ${found}`);
|
|
185
|
+
return found;
|
|
117
186
|
};
|
|
118
187
|
// Annotate the CommonJS export names for ESM import in node:
|
|
119
188
|
0 && (module.exports = {
|
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,13 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
|
|
8
8
|
// src/index.ts
|
|
9
9
|
import { readFileSync } from "fs";
|
|
10
|
-
import {
|
|
10
|
+
import { globSync } from "glob";
|
|
11
|
+
var isDebugEnabled = () => process.env.AXIOM_DEBUG === "true" || process.env.AXIOM_DEBUG === "1";
|
|
12
|
+
var debug = (message, ...args) => {
|
|
13
|
+
if (isDebugEnabled()) {
|
|
14
|
+
console.error(`[axiom:config] ${message}`, ...args);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
11
17
|
var ConfigContainer = class {
|
|
12
18
|
name;
|
|
13
19
|
env;
|
|
@@ -30,31 +36,76 @@ var ConfigContainer = class {
|
|
|
30
36
|
}
|
|
31
37
|
};
|
|
32
38
|
var importConfigFromPath = async (path) => {
|
|
39
|
+
debug(`Attempting to import config from: ${path}`);
|
|
33
40
|
if (/\.json$/.test(path)) {
|
|
34
|
-
|
|
41
|
+
debug(`Loading JSON config file: ${path}`);
|
|
42
|
+
try {
|
|
43
|
+
const config = JSON.parse(readFileSync(path).toString());
|
|
44
|
+
debug(`Successfully loaded JSON config with name: ${config.name}`);
|
|
45
|
+
return config;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
debug(`Failed to parse JSON config: ${error}`);
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
35
50
|
}
|
|
36
51
|
if (/\.((m)?ts|mjs)$/.test(path)) {
|
|
37
|
-
|
|
38
|
-
|
|
52
|
+
debug(`Loading TypeScript/ESM config file: ${path}`);
|
|
53
|
+
try {
|
|
54
|
+
const loaded = await import(path);
|
|
55
|
+
const config = loaded.default || loaded;
|
|
56
|
+
debug(`Successfully loaded TS/ESM config with name: ${config.name}`);
|
|
57
|
+
return config;
|
|
58
|
+
} catch (error) {
|
|
59
|
+
debug(`Failed to import TS/ESM config: ${error}`);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
39
62
|
}
|
|
40
63
|
if (/\.(m)?js$/.test(path)) {
|
|
41
|
-
|
|
42
|
-
|
|
64
|
+
debug(`Loading JavaScript config file: ${path}`);
|
|
65
|
+
try {
|
|
66
|
+
const loaded = __require(path);
|
|
67
|
+
const config = loaded.default || loaded;
|
|
68
|
+
debug(`Successfully loaded JS config with name: ${config.name}`);
|
|
69
|
+
return config;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
debug(`Failed to require JS config: ${error}`);
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
43
74
|
}
|
|
75
|
+
debug(`Unsupported file type: ${path}`);
|
|
44
76
|
throw new Error(`Unsupported file type or path not found: ${path}`);
|
|
45
77
|
};
|
|
46
78
|
var loadConfig = async (input) => {
|
|
79
|
+
debug(
|
|
80
|
+
`Loading config with options:`,
|
|
81
|
+
JSON.stringify({
|
|
82
|
+
env: input?.env,
|
|
83
|
+
cwd: input?.cwd,
|
|
84
|
+
hasOverrides: !!input?.overrides
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
debug(`Looking for base config file...`);
|
|
47
88
|
const baseConfigFile = configPath({
|
|
48
89
|
...input,
|
|
49
90
|
env: void 0
|
|
50
91
|
});
|
|
92
|
+
debug(`Loading base config from: ${baseConfigFile}`);
|
|
51
93
|
const baseConfig = await importConfigFromPath(baseConfigFile);
|
|
94
|
+
debug(`Base config loaded: ${baseConfig.name} (env: ${baseConfig.env})`);
|
|
52
95
|
let overrides = input?.overrides || {};
|
|
53
96
|
if (input?.env) {
|
|
54
|
-
|
|
97
|
+
debug(`Looking for environment-specific config for: ${input.env}`);
|
|
98
|
+
const envConfigPath = configPath(input);
|
|
99
|
+
debug(`Loading env config from: ${envConfigPath}`);
|
|
100
|
+
const devConfig = await importConfigFromPath(envConfigPath);
|
|
101
|
+
debug(`Env config loaded: ${devConfig.name} (env: ${devConfig.env})`);
|
|
55
102
|
overrides = mergeDeep(devConfig, overrides);
|
|
103
|
+
debug(`Merged env config with overrides`);
|
|
56
104
|
}
|
|
57
105
|
const configObject = mergeDeep(baseConfig, overrides);
|
|
106
|
+
debug(
|
|
107
|
+
`Final config: ${configObject.name} (env: ${configObject.env}, isProd: ${configObject.env === (configObject.prodEnvName || "prod")})`
|
|
108
|
+
);
|
|
58
109
|
return new ConfigContainer(configObject);
|
|
59
110
|
};
|
|
60
111
|
function isObject(item) {
|
|
@@ -77,21 +128,39 @@ function mergeDeep(target, ...sources) {
|
|
|
77
128
|
}
|
|
78
129
|
var configPath = (input) => {
|
|
79
130
|
const envIndicator = input?.env ? `.${input.env}` : "";
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
`.axiom${envIndicator}.ts`,
|
|
86
|
-
`.axiom${envIndicator}.mts`
|
|
87
|
-
],
|
|
88
|
-
{ cwd: input?.cwd }
|
|
131
|
+
const extensions = ["json", "js", "mjs", "ts", "mts"];
|
|
132
|
+
const pattern = `.axiom${envIndicator}.{${extensions.join(",")}}`;
|
|
133
|
+
debug(
|
|
134
|
+
`Searching for config files with pattern: ${pattern}`,
|
|
135
|
+
input?.cwd ? `from ${input.cwd}` : "from current directory"
|
|
89
136
|
);
|
|
90
|
-
|
|
137
|
+
const cwd = input?.cwd || process.cwd();
|
|
138
|
+
let currentDir = cwd;
|
|
139
|
+
let found;
|
|
140
|
+
while (true) {
|
|
141
|
+
const matches = globSync(pattern, {
|
|
142
|
+
cwd: currentDir,
|
|
143
|
+
absolute: true,
|
|
144
|
+
nodir: true
|
|
145
|
+
});
|
|
146
|
+
if (matches.length > 0) {
|
|
147
|
+
found = matches[0];
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
const parent = __require("path").dirname(currentDir);
|
|
151
|
+
if (parent === currentDir) {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
currentDir = parent;
|
|
155
|
+
}
|
|
156
|
+
if (found === void 0) {
|
|
157
|
+
debug(`Config file not found! Searched for pattern: ${pattern}`);
|
|
91
158
|
throw new Error(
|
|
92
159
|
`Axiom config files not found: .axiom${envIndicator}.json, .axiom${envIndicator}.js .axiom${envIndicator}.ts`
|
|
93
160
|
);
|
|
94
|
-
|
|
161
|
+
}
|
|
162
|
+
debug(`Found config file: ${found}`);
|
|
163
|
+
return found;
|
|
95
164
|
};
|
|
96
165
|
export {
|
|
97
166
|
ConfigContainer,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dsmrt/axiom-config",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Config library for axiom cli",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"vitest": "^4.0.0"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"
|
|
38
|
+
"glob": "^10.5.0",
|
|
39
39
|
"yargs": "^17.7.2"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|