-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcompile_project.js
executable file
·109 lines (95 loc) · 2.42 KB
/
compile_project.js
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
const EventEmitter = require('events');
const fs = require("fs");
const path = require("path");
const spawn = require("child_process").spawn;
const jsonc = require("jsonc-parser");
const
{
printQuotation,
printErr,
printOk,
printWarning,
tr
} = require("./print.js");
/**
* 编译LayaAir项目。
* 利用位于项目目录中的 .laya/tasks.json 中提供的参数调用编译器。
* 目前并非支持完整的 tasks.json
*/
class LayaProjectCompiler extends EventEmitter
{
constructor(project_dir)
{
super();
if (project_dir)
this.compile(project_dir);
}
compile(project_dir)
{
this.project_dir = project_dir;
let tasksFile = path.resolve(".laya", "tasks.json");
if (!fs.existsSync(tasksFile))
{
console.log(tr("no tasks.json, skip compile.").green);
this.emit("success");
return;
}
// 读取和解析 tasks.json
let tasksFileInString = fs.readFileSync(tasksFile).toString();
let errors = [];
let tasks = jsonc.parse(tasksFileInString, errors);
if (errors.length)
{
errors.forEach((e) => console.error(e));
}
else
{
// windows调用tsc必须是tsc.cmd
if (fs.existsSync(path.join(project_dir, "tsconfig.json")) &&
process.platform == "win32")
{
tasks.command = tasks.command.replace(/\btsc\b/, "tsc.cmd");
}
// 替换${workspaceRoot}变量为其值
let command = tasks.command.replace("${workspaceRoot}", process.cwd());
let args = tasks.args.map((arg) => arg = arg.replace("${workspaceRoot}", process.cwd()));
let fullCommand = tasks.command;
// 打印命令
tasks.args.forEach((arg) => fullCommand += " " + arg);
console.log(fullCommand.yellow);
let sp = spawn(command, args);
sp.stdout.on("data", this.emitStdout.bind(this));
sp.stderr.on("data", this.emitStderr.bind(this));
sp.on("close", (data) =>
{
if (data == 0)
{
if (fs.existsSync(path.join(project_dir, "tsconfig.json")))
this.tsSortScript();
printOk(tr("Compile completed."));
this.emit("success");
}
else
{
this.emit("failed");
}
});
}
}
emitStdout(data)
{
this.emit("stdout", data);
}
emitStderr(data)
{
this.emit("stderr", data);
}
tsSortScript()
{
require("./tools/tsSort.js").htmlHandlerScript(
{
workspacePath: this.project_dir
});
}
}
exports.LayaProjectCompiler = LayaProjectCompiler;