create.mjs
4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* 项目生成器:从当前基座复制出一个全新的、干净的项目基座。
* `venus create <name> --dir <parent>` -> <parent>/<name>/
*
* 生成结果:
* - 完整的 .kernel/(内核,带新生成的 manifest)
* - cli/(工具链)
* - 根配置(package.json 包名换成 <name>;pnpm-workspace/turbo/tsconfig 原样)
* - apps/example:一个最小占位子应用(派生应用可删可改)
* - README / .gitignore
* 不含:node_modules、dist、.git、备份、生成物。
*/
import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import { ROOT, KERNEL_DIR, sha256, writeManifestAt } from './kernel.mjs';
const SKIP_DIRS = new Set([
'node_modules', '.git', 'dist', '.turbo', '.tmp', 'coverage',
'miniprogram_dist', '.kernel-backups',
]);
function copyDir(src, dst, transform) {
fs.mkdirSync(dst, { recursive: true });
for (const ent of fs.readdirSync(src, { withFileTypes: true })) {
if (ent.isDirectory()) {
if (SKIP_DIRS.has(ent.name)) continue;
copyDir(path.join(src, ent.name), path.join(dst, ent.name), transform);
} else {
const s = path.join(src, ent.name);
const d = path.join(dst, ent.name);
if (transform) {
const content = fs.readFileSync(s, 'utf8');
const out = transform(d, content);
fs.writeFileSync(d, out ?? content);
} else {
fs.copyFileSync(s, d);
}
}
}
}
/** 生成一个最小占位子应用(如果源基座没有 example 时用) */
function ensureExample(targetApps) {
const exampleDir = path.join(targetApps, 'example');
if (fs.existsSync(exampleDir)) return;
fs.mkdirSync(path.join(exampleDir, 'src'), { recursive: true });
fs.writeFileSync(
path.join(exampleDir, 'package.json'),
JSON.stringify(
{
name: '@app/example',
version: '0.0.0',
private: true,
type: 'module',
scripts: {
dev: 'vite',
build: 'vite build',
lint: 'eslint . --max-warnings=0',
typecheck: 'tsc --noEmit',
},
venus: { route: 'example', entry: 'http://localhost:5174/' },
},
null,
2,
) + '\n',
);
}
/**
* @param {string} name 项目名(小写 kebab)
* @param {string} parentDir 父目录
*/
export function createProject(name, parentDir) {
if (!/^[a-z0-9][a-z0-9-]*$/.test(name)) {
return { ok: false, message: `项目名不合法:${name}(只允许小写字母、数字、连字符,且以字母或数字开头)` };
}
const target = path.resolve(parentDir, name);
if (fs.existsSync(target)) {
return { ok: false, message: `目标目录已存在:${target}` };
}
try {
fs.mkdirSync(target, { recursive: true });
// 1. 复制内核(transform 不动内容,保持哈希一致)
copyDir(KERNEL_DIR, path.join(target, '.kernel'));
// 2. 复制 CLI 工具链
copyDir(path.join(ROOT, 'cli'), path.join(target, 'cli'));
// 3. 复制根配置,替换包名
const rootFiles = [
'package.json', 'pnpm-workspace.yaml', 'tsconfig.base.json', 'turbo.json',
'eslint.config.js', '.env.example', '.editorconfig', '.prettierrc.json',
'.prettierignore', '.npmrc', '.gitignore',
];
for (const f of rootFiles) {
const s = path.join(ROOT, f);
if (!fs.existsSync(s)) continue;
const d = path.join(target, f);
let content = fs.readFileSync(s, 'utf8');
if (f === 'package.json') {
content = content.replace(/"name":\s*"@venus\/base"/, `"name": "${name}"`);
}
fs.writeFileSync(d, content);
}
// 4. 复制业务区(只保留 example 作为起点)
const appsSrc = path.join(ROOT, 'apps');
const appsDst = path.join(target, 'apps');
if (fs.existsSync(appsSrc)) {
// 只复制 example 目录,避免把派生应用的真实业务带进新项目
const ex = path.join(appsSrc, 'example');
if (fs.existsSync(ex)) copyDir(ex, path.join(appsDst, 'example'));
}
ensureExample(appsDst);
// 5. 生成新项目的内核清单(在新目录就地生成)
writeManifestAt(path.join(target, '.kernel'));
return {
ok: true,
target,
message: `已生成项目基座 ${name} 于 ${target}\n` +
` 下一步:cd ${name} && pnpm install && pnpm dev`,
};
} catch (err) {
try { fs.rmSync(target, { recursive: true, force: true }); } catch { /* ignore */ }
return { ok: false, message: `生成失败:${err.message}` };
}
}