-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.js
More file actions
54 lines (45 loc) · 1.77 KB
/
build.js
File metadata and controls
54 lines (45 loc) · 1.77 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
const path = require('path');
const fs = require('fs');
const SiteGenerator = require('../site-generator');
class BuildCommand {
constructor(configManager) {
this.configManager = configManager;
this.cwd = process.cwd();
}
async execute(options = {}) {
const startTime = Date.now();
try {
let config = this.configManager.getConfig();
// 检查是否存在自定义配置文件
const customConfigPath = path.join(this.cwd, 'pageforge.config.js');
if (fs.existsSync(customConfigPath)) {
const customConfig = require(customConfigPath);
config = {...config, ...customConfig};
}
// 合并命令行选项
config = {
...config,
...options
};
// 如果配置中有 versions 且当前不是在构建特定版本
// 则将输出路径修改为 current 目录
if (config.versions && !config.version) {
config.outputPath = path.join(config.outputPath, 'current');
config.version = 'current';
this.configManager.updateConfig(config);
}
else if (config.version) {
config.outputPath = path.join(config.outputPath, config.version);
this.configManager.updateConfig(config);
}
const generator = new SiteGenerator(config);
await generator.generate();
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
console.log(`🎉 项目编译完成,耗时: ${duration}s`);
}
catch (error) {
throw new Error(`Build failed: ${error.message}`);
}
}
}
module.exports = BuildCommand;