-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
scripts.js
77 lines (61 loc) 路 1.71 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
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
import { brightGreen, gray } from "./deps/colors.js";
export default class Scripts {
scripts = new Map();
constructor(site) {
this.site = site;
}
set(name, ...commands) {
this.scripts.set(name, commands);
}
async run(options = {}, ...names) {
options = { cwd: this.site.options.cwd, ...options };
for (const name of names) {
const success = await this.#runScript(options, name);
if (!success) {
return false;
}
}
return true;
}
async #runScript(options, name) {
if (this.scripts.has(name)) {
if (this.site.options.verbose > 0) {
console.log(`鈿★笍 ${brightGreen(name)}`);
}
name = this.scripts.get(name);
return this.run(options, ...name);
}
if (Array.isArray(name)) {
const results = await Promise.all(
name.map((n) => this.#runScript(options, n)),
);
return results.every((success) => success);
}
if (typeof name === "function") {
return this.#runFunction(name);
}
return this.#runCommand(options, name);
}
async #runFunction(fn) {
if (fn.name && this.site.options.verbose > 0) {
console.log(gray(`鈿★笍 ${fn.name}()`));
}
const result = await fn(this.site);
return result !== false;
}
async #runCommand(options, command) {
if (this.site.options.verbose > 0) {
console.log(gray(`鈿★笍 ${command}`));
}
const cmd = shArgs(command);
const process = Deno.run({ cmd, ...options });
const status = await process.status();
process.close();
return status.success;
}
}
function shArgs(command) {
return Deno.build.os === "windows"
? ["PowerShell.exe", "-Command", command]
: ["/bin/bash", "-c", command];
}