@infly/libs 2.2.7 → 2.2.9

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 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();
@@ -36,6 +36,7 @@ const {
36
36
  pullSingleDirFromBranch
37
37
  } = require("../../script/git-automation");
38
38
  const { resolveSiblingAppDeps } = require("../build-utils/deps");
39
+ const { resolveGitWorktreeRoot } = require("../../script/git-automation/git-utils");
39
40
 
40
41
  const scriptEvent = process.env.npm_lifecycle_event;
41
42
  const isBuilZipScript = process.env.npm_lifecycle_event;
@@ -46,6 +47,9 @@ const npmPackageConfigKey = "infly";
46
47
  const currentBranch = getCurrentBranch();
47
48
 
48
49
  const projectRoot = process.cwd();
50
+ // 构建配置可以位于主仓中的子目录(例如合并后的 level/)。配置、产物仍以
51
+ // projectRoot 为基准,只有 Git 检查与分支更新定位到实际工作树根目录。
52
+ const projectGitRoot = resolveGitWorktreeRoot(projectRoot);
49
53
 
50
54
  // 依赖包的配置
51
55
  const originConfigPath = currentProjectFileResolve("template-version-config.json"); // 模板项目文件
@@ -813,14 +817,14 @@ async function beforeBuild() {
813
817
  return;
814
818
  }
815
819
 
816
- const currentProjectStatus = checkSingleDirGitStatus(projectRoot, projectName);
820
+ const currentProjectStatus = checkSingleDirGitStatus(projectGitRoot, projectName);
817
821
 
818
822
  if (currentProjectStatus.hasUncommitted) {
819
823
  console.log(global.logColor.error, `\n❌ 当前项目存在未提交的更改,请先提交或暂存后再继续构建`);
820
824
  process.exit(1);
821
825
  }
822
826
 
823
- const currentProjectUpdateResult = pullSingleDirFromBranch(projectRoot, projectName, targetBranch);
827
+ const currentProjectUpdateResult = pullSingleDirFromBranch(projectGitRoot, projectName, targetBranch);
824
828
 
825
829
  if (!currentProjectUpdateResult.success) {
826
830
  console.log(global.logColor.error, `\n❌ 当前项目切换到 ${targetBranch} 分支失败,请检查`);
@@ -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 };
@@ -66,7 +66,9 @@ async function selectOptions(message, choices, dependencies = {}) {
66
66
  name: "selections",
67
67
  message,
68
68
  indicator: { on: "[√]", off: "[ ]" },
69
- hint: "↑/↓ 移动,空格勾选/取消,回车确认,Ctrl+C 退出(至少选择一项)",
69
+ // Enquirer 不会正确清除 Git Bash 中发生自动换行的提示行,提示过长时
70
+ // 每次移动光标都会残留一帧。保持简短,最少选择一项由 min 校验提示。
71
+ hint: "空格选择,回车确认",
70
72
  stdin,
71
73
  stdout,
72
74
  min: 1,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infly/libs",
3
- "version": "2.2.7",
3
+ "version": "2.2.9",
4
4
  "description": "不受前端框架限制的独立工具库",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -64,6 +64,7 @@
64
64
  "dotenv": "16.6.1",
65
65
  "enquirer": "^2.4.1",
66
66
  "path-browserify": "^1.0.1",
67
+ "sass": "^1.69.5",
67
68
  "serve-static": "^1.15.0",
68
69
  "yaml": "^2.3.4"
69
70
  },
@@ -45,7 +45,7 @@ function findGitRepo(cwd) {
45
45
  }
46
46
  }
47
47
 
48
- function getGitCommonDir(cwd) {
48
+ function getGitCommonDir(cwd) {
49
49
  const repo = findGitRepo(cwd);
50
50
 
51
51
  if (repo) {
@@ -58,10 +58,16 @@ function getGitCommonDir(cwd) {
58
58
  } catch {
59
59
  return path.resolve(cwd || process.cwd());
60
60
  }
61
- }
62
-
63
- module.exports = {
64
- gitOutput,
65
- findGitRepo,
66
- getGitCommonDir
67
- };
61
+ }
62
+
63
+ function resolveGitWorktreeRoot(cwd) {
64
+ const resolvedCwd = path.resolve(cwd || process.cwd());
65
+ return findGitRepo(resolvedCwd)?.root || resolvedCwd;
66
+ }
67
+
68
+ module.exports = {
69
+ gitOutput,
70
+ findGitRepo,
71
+ getGitCommonDir,
72
+ resolveGitWorktreeRoot
73
+ };