@spaceflow/core 0.3.0 → 0.4.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/dist/index.js +38 -52
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/shared/source-utils/index.ts +1 -0
- package/src/shared/spaceflow-dir/index.ts +35 -57
package/package.json
CHANGED
|
@@ -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/
|
|
48
|
-
* @
|
|
49
|
-
* @
|
|
41
|
+
* 获取 @spaceflow/core 的版本号
|
|
42
|
+
* 从 process.argv[1](cli 入口)向上找到 @spaceflow/cli 的 package.json
|
|
43
|
+
* 读取其中声明的 @spaceflow/core 依赖版本,保证 cli 和 core 版本一致
|
|
50
44
|
*/
|
|
51
|
-
export function
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
|
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
|
-
|
|
113
|
-
|
|
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":
|
|
96
|
+
"@spaceflow/core": coreVersion,
|
|
119
97
|
},
|
|
120
98
|
};
|
|
121
99
|
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
|