-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.config.js
More file actions
80 lines (73 loc) · 1.95 KB
/
Copy pathesbuild.config.js
File metadata and controls
80 lines (73 loc) · 1.95 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
import { resolve } from 'node:path';
import { copyFile, rm, writeFile } from 'node:fs/promises';
import esbuild from 'esbuild';
import { rootPath } from '#common/utils/file/path.js';
import { getPackageJson } from '#common/utils/file/getPackageJson.js';
import { logger } from '#common/utils/x/logger.js';
const packageJson = getPackageJson();
const outputDir = 'dist';
const esbuildConfig = {
entryPoints: ['bin/x.js', 'bin/xa.js', 'bin/xi.js', 'bin/xu.js', 'bin/xr.js'],
outdir: `${outputDir}/bin`,
platform: 'node',
target: ['node20'],
format: 'esm',
bundle: true,
minify: true,
packages: 'external',
define: {
'process.env.VERSION': JSON.stringify(packageJson.version)
}
};
/**
* 清理文件
*/
async function clean() {
await rm(resolve(rootPath, outputDir), { recursive: true, force: true });
}
/**
* 构建完成后的操作
* @returns {Promise<void>}
*/
async function afterBuild() {
await makePackageJson();
await copyDocs();
}
/**
* 创建package.json
* @returns {Promise<void>}
*/
async function makePackageJson() {
const publishExcludeFields = ['scripts', 'devDependencies', 'imports', 'main', 'config', 'packageManager'];
const newPackageJson = packageJson;
publishExcludeFields?.forEach((key) => {
delete newPackageJson[key];
});
await writeFile(resolve(rootPath, `${outputDir}/package.json`), JSON.stringify(newPackageJson));
}
/**
* 复制文档和配置模板
*/
async function copyDocs() {
const docs = ['README.md', 'opencode.example.json'];
await Promise.allSettled(
docs.map((doc) => {
copyFile(resolve(rootPath, doc), resolve(rootPath, `${outputDir}/${doc}`));
})
);
}
/**
* 主进程
*/
async function main() {
try {
await clean();
await esbuild.build(esbuildConfig);
await afterBuild();
process.exit(0);
} catch (e) {
logger.info(e);
process.exit(1);
}
}
await main();