@infly/libs 2.2.7 → 2.2.8
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/cli.js +1 -0
- package/cli/run-command.js +81 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -131,6 +131,7 @@ async function main(argv = process.argv.slice(2)) {
|
|
|
131
131
|
preview: () => loadPreview().init(),
|
|
132
132
|
"--preview": () => loadPreview().init(),
|
|
133
133
|
beforeBuild: () => loadBuildDist().beforeBuild(),
|
|
134
|
+
run: () => require("../cli/run-command").runCommand(argv.slice(1)),
|
|
134
135
|
scriptOverride: () => {
|
|
135
136
|
const preview = loadPreview();
|
|
136
137
|
const buildDist = loadBuildDist();
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
const dotenv = require("dotenv");
|
|
6
|
+
const { defaultRunCommand } = require("./project-command");
|
|
7
|
+
|
|
8
|
+
const ENV_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
9
|
+
|
|
10
|
+
function parseRunArgs(argv) {
|
|
11
|
+
const env = {};
|
|
12
|
+
let cwd = ".";
|
|
13
|
+
let envFile;
|
|
14
|
+
let separatorIndex = -1;
|
|
15
|
+
|
|
16
|
+
for (let index = 0; index < argv.length; index++) {
|
|
17
|
+
const arg = argv[index];
|
|
18
|
+
if (arg === "--") {
|
|
19
|
+
separatorIndex = index;
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
if (arg === "--cwd" || arg === "--env-file") {
|
|
23
|
+
const value = argv[index + 1];
|
|
24
|
+
if (!value || value.startsWith("--")) {
|
|
25
|
+
throw new Error(`${arg} requires a value.`);
|
|
26
|
+
}
|
|
27
|
+
if (arg === "--cwd") cwd = value;
|
|
28
|
+
else envFile = value;
|
|
29
|
+
index++;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (arg !== "--set") {
|
|
33
|
+
throw new Error(`run 不支持的参数: ${arg}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const assignment = argv[index + 1];
|
|
37
|
+
if (!assignment) throw new Error("--set requires NAME=VALUE.");
|
|
38
|
+
const equalsIndex = assignment.indexOf("=");
|
|
39
|
+
const name = equalsIndex > 0 ? assignment.slice(0, equalsIndex) : "";
|
|
40
|
+
if (!ENV_NAME_PATTERN.test(name)) {
|
|
41
|
+
throw new Error(`Invalid environment assignment: ${assignment}`);
|
|
42
|
+
}
|
|
43
|
+
env[name] = assignment.slice(equalsIndex + 1);
|
|
44
|
+
index++;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (separatorIndex < 0 || !argv[separatorIndex + 1]) {
|
|
48
|
+
throw new Error("Usage: infly-libs run [--set NAME=VALUE] -- <command> [...args]");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
env,
|
|
53
|
+
cwd,
|
|
54
|
+
envFile,
|
|
55
|
+
command: argv[separatorIndex + 1],
|
|
56
|
+
args: argv.slice(separatorIndex + 2)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function runCommand(argv, dependencies = {}) {
|
|
61
|
+
const {
|
|
62
|
+
run = defaultRunCommand,
|
|
63
|
+
cwd: baseCwd = process.cwd(),
|
|
64
|
+
baseEnv = process.env
|
|
65
|
+
} = dependencies;
|
|
66
|
+
const parsed = parseRunArgs(argv);
|
|
67
|
+
const commandCwd = path.resolve(baseCwd, parsed.cwd);
|
|
68
|
+
let fileEnv = {};
|
|
69
|
+
if (parsed.envFile) {
|
|
70
|
+
const envPath = path.resolve(baseCwd, parsed.envFile);
|
|
71
|
+
if (!fs.existsSync(envPath)) throw new Error(`Environment file not found: ${envPath}`);
|
|
72
|
+
fileEnv = dotenv.parse(fs.readFileSync(envPath));
|
|
73
|
+
}
|
|
74
|
+
await run(parsed.command, parsed.args, {
|
|
75
|
+
cwd: commandCwd,
|
|
76
|
+
env: { ...fileEnv, ...baseEnv, ...parsed.env }
|
|
77
|
+
});
|
|
78
|
+
return parsed;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = { parseRunArgs, runCommand };
|