oox 0.3.0-beta11 → 0.3.0-beta13

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/bin/configurer.js CHANGED
@@ -1,64 +1,64 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.configure = void 0;
4
- const fs = require("node:fs");
5
- const path = require("node:path");
6
- const argv = require("./argv");
7
- /**
8
- * x.y=1 => { x: { y: 1 } }
9
- */
10
- function mergeFlatEnv(env) {
11
- for (const key of Object.keys(env)) {
12
- // x.y.z
13
- if (key.includes('.') && !key.startsWith('.') && !key.endsWith('.')) {
14
- const subKeys = key.split('.');
15
- const valueKey = subKeys.pop();
16
- let tmpEnv = env;
17
- for (const subKey of subKeys) {
18
- if (subKey in tmpEnv) { }
19
- else {
20
- tmpEnv[subKey] = {};
21
- }
22
- tmpEnv = tmpEnv[subKey];
23
- }
24
- tmpEnv[valueKey] = env[key];
25
- }
26
- }
27
- }
28
- async function readEnvFile(filePath) {
29
- let env = {};
30
- if (filePath && fs.existsSync(filePath)) {
31
- if (filePath.endsWith('.json')) {
32
- const raw = fs.readFileSync(filePath, 'utf-8');
33
- env = JSON.parse(raw);
34
- }
35
- else {
36
- const finalPath = path.resolve(filePath).replace(/\\/g, '/');
37
- env = await eval(`import('file://${finalPath}')`);
38
- }
39
- }
40
- else {
41
- throw new Error('Env file not found: ' + filePath);
42
- }
43
- return env.default || env;
44
- }
45
- async function configure(mergeEnv) {
46
- const env = Object.create(null);
47
- const defaultEnvPath = argv.getEnvArg('default-env');
48
- const targetEnvPath = argv.getEnvArg('env');
49
- const defaultEnv = defaultEnvPath ? await readEnvFile(defaultEnvPath) : {};
50
- const targetEnv = targetEnvPath ? await readEnvFile(targetEnvPath) : {};
51
- Object.assign(env, defaultEnv, targetEnv, argv.getAllEnvArgs());
52
- mergeFlatEnv(env);
53
- if ('string' === typeof env.ignore)
54
- env.ignore = env.ignore.split(',');
55
- if ('string' === typeof env.registry)
56
- env.registry = env.registry.split(',');
57
- if ('string' === typeof env.origin && env.origin.includes(',')) {
58
- env.origin = env.origin.split(',');
59
- }
60
- if (mergeEnv)
61
- Object.assign(env, mergeEnv);
62
- return env;
63
- }
64
- exports.configure = configure;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.configure = void 0;
4
+ const fs = require("node:fs");
5
+ const path = require("node:path");
6
+ const argv = require("./argv");
7
+ /**
8
+ * x.y=1 => { x: { y: 1 } }
9
+ */
10
+ function mergeFlatEnv(env) {
11
+ for (const key of Object.keys(env)) {
12
+ // x.y.z
13
+ if (key.includes('.') && !key.startsWith('.') && !key.endsWith('.')) {
14
+ const subKeys = key.split('.');
15
+ const valueKey = subKeys.pop();
16
+ let tmpEnv = env;
17
+ for (const subKey of subKeys) {
18
+ if (subKey in tmpEnv) { }
19
+ else {
20
+ tmpEnv[subKey] = {};
21
+ }
22
+ tmpEnv = tmpEnv[subKey];
23
+ }
24
+ tmpEnv[valueKey] = env[key];
25
+ }
26
+ }
27
+ }
28
+ async function readEnvFile(filePath) {
29
+ let env = {};
30
+ if (filePath && fs.existsSync(filePath)) {
31
+ if (filePath.endsWith('.json')) {
32
+ const raw = fs.readFileSync(filePath, 'utf-8');
33
+ env = JSON.parse(raw);
34
+ }
35
+ else {
36
+ const finalPath = path.resolve(filePath).replace(/\\/g, '/');
37
+ env = await eval(`import('file://${finalPath}')`);
38
+ }
39
+ }
40
+ else {
41
+ throw new Error('Env file not found: ' + filePath);
42
+ }
43
+ return env.default || env;
44
+ }
45
+ async function configure(mergeEnv) {
46
+ const env = Object.create(null);
47
+ const defaultEnvPath = argv.getEnvArg('default-env');
48
+ const targetEnvPath = argv.getEnvArg('env');
49
+ const defaultEnv = defaultEnvPath ? await readEnvFile(defaultEnvPath) : {};
50
+ const targetEnv = targetEnvPath ? await readEnvFile(targetEnvPath) : {};
51
+ Object.assign(env, defaultEnv, targetEnv, argv.getAllEnvArgs());
52
+ mergeFlatEnv(env);
53
+ if ('string' === typeof env.ignore)
54
+ env.ignore = env.ignore.split(',');
55
+ if ('string' === typeof env.registry)
56
+ env.registry = env.registry.split(',');
57
+ if ('string' === typeof env.origin && env.origin.includes(',')) {
58
+ env.origin = env.origin.split(',');
59
+ }
60
+ if (mergeEnv)
61
+ Object.assign(env, mergeEnv);
62
+ return env;
63
+ }
64
+ exports.configure = configure;