@spaceflow/core 0.4.0 → 0.6.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.4.0",
3
+ "version": "0.6.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
 
@@ -37,6 +37,8 @@ export function initI18n(lang?: string): void {
37
37
  },
38
38
  returnNull: false,
39
39
  returnEmptyString: false,
40
+ // 确保 init 同步完成(默认 initImmediate: true 会将加载推到 setTimeout)
41
+ initImmediate: false,
40
42
  // i18next v25.8+ 会在 init 时输出 locize.com 推广日志
41
43
  showSupportNotice: false,
42
44
  });