@spaceflow/core 0.3.0 → 0.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spaceflow/core",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Spaceflow 核心能力库",
5
5
  "license": "MIT",
6
6
  "author": "Lydanne",
@@ -195,15 +195,53 @@ export function getDependencies(cwd?: string): Record<string, string> {
195
195
  return (config.dependencies as Record<string, string>) || {};
196
196
  }
197
197
 
198
+ /**
199
+ * 找到包含指定字段的最高优先级配置文件路径
200
+ * 如果没有找到,返回项目级 .spaceflowrc 路径(默认写入位置)
201
+ * @param field 要查找的字段名
202
+ * @param cwd 工作目录
203
+ */
204
+ export function findConfigFileWithField(field: string, cwd?: string): string {
205
+ const workDir = cwd || process.cwd();
206
+ // 按优先级从高到低查找,找到第一个包含该字段的文件
207
+ const candidates = [
208
+ join(workDir, RC_FILE_NAME),
209
+ join(workDir, ".spaceflow", CONFIG_FILE_NAME),
210
+ join(homedir(), RC_FILE_NAME),
211
+ join(homedir(), ".spaceflow", CONFIG_FILE_NAME),
212
+ ];
213
+
214
+ for (const filePath of candidates) {
215
+ if (existsSync(filePath)) {
216
+ try {
217
+ const content = readFileSync(filePath, "utf-8");
218
+ const config = JSON.parse(content);
219
+ if (config[field] !== undefined) {
220
+ return filePath;
221
+ }
222
+ } catch {
223
+ // 解析失败,跳过
224
+ }
225
+ }
226
+ }
227
+
228
+ // 默认写入项目级 .spaceflowrc
229
+ return join(workDir, RC_FILE_NAME);
230
+ }
231
+
198
232
  /**
199
233
  * 更新单个 dependency
234
+ * 找到 dependencies 所在的配置文件并原地更新,默认写入 .spaceflowrc
200
235
  * @param name 依赖名称
201
236
  * @param source 依赖来源
202
237
  * @param cwd 工作目录,默认为 process.cwd()
203
238
  * @returns 是否有更新(false 表示已存在相同配置)
204
239
  */
205
240
  export function updateDependency(name: string, source: string, cwd?: string): boolean {
206
- const config = readConfigSync(cwd) as Record<string, unknown>;
241
+ const targetFile = findConfigFileWithField("dependencies", cwd);
242
+ const config = existsSync(targetFile)
243
+ ? (JSON.parse(readFileSync(targetFile, "utf-8")) as Record<string, unknown>)
244
+ : ({} as Record<string, unknown>);
207
245
 
208
246
  if (!config.dependencies) {
209
247
  config.dependencies = {};
@@ -217,18 +255,29 @@ export function updateDependency(name: string, source: string, cwd?: string): bo
217
255
  }
218
256
 
219
257
  dependencies[name] = source;
220
- writeConfigSync(config, cwd);
258
+ writeFileSync(targetFile, stringify(config, { indent: 2 }) + "\n");
221
259
  return true;
222
260
  }
223
261
 
224
262
  /**
225
263
  * 删除单个 dependency
264
+ * 找到 dependencies 所在的配置文件并原地更新
226
265
  * @param name 依赖名称
227
266
  * @param cwd 工作目录,默认为 process.cwd()
228
267
  * @returns 是否有删除(false 表示不存在)
229
268
  */
230
269
  export function removeDependency(name: string, cwd?: string): boolean {
231
- const config = readConfigSync(cwd) as Record<string, unknown>;
270
+ const targetFile = findConfigFileWithField("dependencies", cwd);
271
+ if (!existsSync(targetFile)) {
272
+ return false;
273
+ }
274
+
275
+ let config: Record<string, unknown>;
276
+ try {
277
+ config = JSON.parse(readFileSync(targetFile, "utf-8"));
278
+ } catch {
279
+ return false;
280
+ }
232
281
 
233
282
  if (!config.dependencies) {
234
283
  return false;
@@ -241,7 +290,7 @@ export function removeDependency(name: string, cwd?: string): boolean {
241
290
  }
242
291
 
243
292
  delete dependencies[name];
244
- writeConfigSync(config, cwd);
293
+ writeFileSync(targetFile, stringify(config, { indent: 2 }) + "\n");
245
294
  return true;
246
295
  }
247
296
 
@@ -22,6 +22,7 @@ export function isGitUrl(source: string): boolean {
22
22
  */
23
23
  export function isLocalPath(source: string): boolean {
24
24
  return (
25
+ source.startsWith("workspace:") ||
25
26
  source.startsWith("link:") ||
26
27
  source.startsWith("./") ||
27
28
  source.startsWith("../") ||
@@ -2,7 +2,6 @@ import { existsSync, readFileSync, mkdirSync, writeFileSync } from "fs";
2
2
  import { join } from "path";
3
3
  import { homedir } from "os";
4
4
  import { SPACEFLOW_DIR, PACKAGE_JSON } from "../../extension-system/extension.interface";
5
- import { isPnpmWorkspace } from "../package-manager";
6
5
 
7
6
  /**
8
7
  * 获取 .spaceflow 目录路径
@@ -36,86 +35,65 @@ config-schema.json
36
35
  writeFileSync(gitignorePath, gitignoreContent);
37
36
  }
38
37
 
39
- // 创建空的 pnpm-workspace.yaml,防止被父级 workspace 接管
40
- const workspaceYamlPath = join(spaceflowDir, "pnpm-workspace.yaml");
41
- if (!existsSync(workspaceYamlPath)) {
42
- writeFileSync(workspaceYamlPath, "packages: []\n");
43
- }
44
38
  }
45
39
 
46
40
  /**
47
- * 获取 @spaceflow/cli 的路径
48
- * @param isGlobal 是否为全局安装
49
- * @param cwd 工作目录,默认为 process.cwd()
41
+ * 获取 @spaceflow/core 的版本号
42
+ * 从 process.argv[1](cli 入口)向上找到 @spaceflow/cli package.json
43
+ * 读取其中声明的 @spaceflow/core 依赖版本,保证 cli 和 core 版本一致
50
44
  */
51
- export function getSpaceflowCliPath(isGlobal: boolean = false, cwd?: string): string {
52
- const workDir = cwd || process.cwd();
53
-
54
- if (isGlobal) {
55
- // 全局安装:尝试找到 @spaceflow/cli 的实际路径进行 link
56
- // 优先从当前项目的 cli 目录 link
57
- const cliPath = join(workDir, "packages", "cli");
58
- if (existsSync(join(cliPath, PACKAGE_JSON))) {
45
+ export function getSpaceflowCoreVersion(): string {
46
+ const cliEntryPath = process.argv[1];
47
+ if (cliEntryPath) {
48
+ // cli 入口: .../node_modules/@spaceflow/cli/dist/cli.js → 往上两级是包根目录
49
+ const cliDir = join(cliEntryPath, "..", "..");
50
+ const cliPkgPath = join(cliDir, PACKAGE_JSON);
51
+ if (existsSync(cliPkgPath)) {
59
52
  try {
60
- const content = readFileSync(join(cliPath, PACKAGE_JSON), "utf-8");
61
- const pkg = JSON.parse(content);
62
- if (pkg.name === "@spaceflow/cli") {
63
- return `link:${cliPath}`;
53
+ const cliPkg = JSON.parse(readFileSync(cliPkgPath, "utf-8"));
54
+ if (cliPkg.name === "@spaceflow/cli") {
55
+ const coreVersion = cliPkg.dependencies?.["@spaceflow/core"];
56
+ if (coreVersion) {
57
+ return coreVersion;
58
+ }
64
59
  }
65
60
  } catch {
66
61
  // ignore
67
62
  }
68
63
  }
69
- // 回退到 latest
70
- return "latest";
71
- }
72
-
73
- // 本地安装:检查是否在 monorepo 中
74
- if (isPnpmWorkspace(workDir)) {
75
- return "workspace:*";
76
64
  }
77
-
78
- // 尝试从项目 package.json 获取版本
79
- const projectPkgPath = join(workDir, PACKAGE_JSON);
80
- if (existsSync(projectPkgPath)) {
81
- try {
82
- const content = readFileSync(projectPkgPath, "utf-8");
83
- const pkg = JSON.parse(content);
84
- const version =
85
- pkg.dependencies?.["@spaceflow/core"] || pkg.devDependencies?.["@spaceflow/core"];
86
- if (version) return version;
87
- } catch {
88
- // ignore
89
- }
90
- }
91
-
92
65
  return "latest";
93
66
  }
94
67
 
95
68
  /**
96
- * 确保 .spaceflow/package.json 存在
97
- * 包含 @spaceflow/cli 作为依赖
69
+ * 确保 .spaceflow 目录及 package.json 存在,并保持 @spaceflow/core 版本与 cli 一致
98
70
  * @param spaceflowDir .spaceflow 目录路径
99
- * @param isGlobal 是否为全局安装
100
- * @param cwd 工作目录,默认为 process.cwd()
101
71
  */
102
- export function ensureSpaceflowPackageJson(
103
- spaceflowDir: string,
104
- isGlobal: boolean = false,
105
- cwd?: string,
106
- ): void {
107
- // 确保目录存在
72
+ export function ensureSpaceflowPackageJson(spaceflowDir: string): void {
108
73
  ensureSpaceflowDir(spaceflowDir);
109
74
 
110
- // 确保 package.json 存在
111
75
  const packageJsonPath = join(spaceflowDir, PACKAGE_JSON);
112
- if (!existsSync(packageJsonPath)) {
113
- const cliPath = getSpaceflowCliPath(isGlobal, cwd);
76
+ const coreVersion = getSpaceflowCoreVersion();
77
+
78
+ if (existsSync(packageJsonPath)) {
79
+ // 已存在:检查并更新 @spaceflow/core 版本
80
+ try {
81
+ const pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
82
+ if (pkg.dependencies?.["@spaceflow/core"] !== coreVersion) {
83
+ pkg.dependencies = pkg.dependencies || {};
84
+ pkg.dependencies["@spaceflow/core"] = coreVersion;
85
+ writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2) + "\n");
86
+ }
87
+ } catch {
88
+ // ignore
89
+ }
90
+ } else {
91
+ // 不存在:创建
114
92
  const packageJson = {
115
93
  name: "spaceflow",
116
94
  private: true,
117
95
  dependencies: {
118
- "@spaceflow/core": cliPath,
96
+ "@spaceflow/core": coreVersion,
119
97
  },
120
98
  };
121
99
  writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");