-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
scripts.js
50 lines (39 loc) 路 1.11 KB
/
scripts.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
import { brightGreen } from "./deps/colors.js";
export default class Scripts {
scripts = new Map();
constructor(site) {
this.site = site;
}
set(name, ...scripts) {
this.scripts.set(name, scripts);
}
async run(...names) {
for (const name of names) {
const success = await this.#runScript(name);
if (!success) {
return false;
}
}
return true;
}
async #runScript(name) {
if (this.scripts.has(name)) {
name = this.scripts.get(name);
return this.run(...name);
}
if (Array.isArray(name)) {
const results = await Promise.all(name.map((n) => this.#runScript(n)));
return (results.every((success) => success)) ? 0 : 1;
}
return this.#runCommand(name);
}
async #runCommand(command) {
console.log(`鈿★笍 ${brightGreen(command)}`);
const cmd = Array.from(command.matchAll(/('([^']*)'|"([^"]*)"|[\S]+)/g))
.map((piece) => piece[2] || piece[1]);
const process = Deno.run({ cmd, cwd: this.site.options.cwd });
const status = await process.status();
process.close();
return status.success;
}
}