/
runner.ts
181 lines (157 loc) 路 4.69 KB
/
runner.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2020-present the denosaurs team. All rights reserved. MIT license.
import {
buildFlags,
Script,
ScriptObject,
ScriptOptions,
Scripts,
} from "./scripts.ts";
import { merge } from "./merge.ts";
/** `Runner` configuration.
* This configuration is, in contrast to other, extended
* by `Denon` config as scripts has to be a top level
* parameter. */
export interface RunnerConfig extends ScriptOptions {
scripts: Scripts;
}
const reDenoAction = new RegExp(/^(deno +\w+) *(.*)$/);
const reCompact = new RegExp(
/^'(?:\\'|.)*?\.(ts|js)'|^"(?:\\"|.)*?\.(ts|js)"|^(?:\\ |\S)+\.(ts|js)$/,
);
const reCliCompact = new RegExp(/^(run|test|fmt|lint) *(.*)$/);
/** Handle all the things related to process management.
* Scripts are built into executable commands that are
* executed by `Deno.run()` and managed in an `Executable`
* object to make available process events. */
export class Runner {
#config: RunnerConfig;
#args: string[];
constructor(config: RunnerConfig, args: string[] = []) {
this.#config = config;
this.#args = args;
}
private cmd(cmd: string[], options: ScriptOptions): Command {
const command = {
cmd,
options,
exe: (): Deno.Process => {
return this.execute(command);
},
};
return command;
}
private buildCliCommand(args: string[], global: ScriptOptions): Command {
const cmd = args.join(" ");
let out: string[];
if (reCompact.test(cmd)) {
out = ["deno", "run"];
out = out.concat(stdCmd(cmd));
} else if (reCliCompact.test(cmd)) {
out = ["deno"];
out = out.concat(stdCmd(cmd));
} else {
out = stdCmd(cmd);
}
return this.cmd(out, global); // single command
}
private buildCommands(
script: string | ScriptObject,
global: ScriptOptions,
args: string[],
): Command[] {
if (typeof script === "object") {
let options = Object.assign({}, merge(global, script));
return this.buildStringCommands(script.cmd, options, args);
}
return this.buildStringCommands(script, global, args);
}
public buildStringCommands(
script: string,
global: ScriptOptions,
args: string[],
): Command[] {
if (script.includes("&&")) {
let commands: Command[] = [];
script.split("&&").map((s) => {
commands.push(this.buildCommand(s, global, args));
});
return commands;
}
return [this.buildCommand(script, global, args)];
}
private buildCommand(
cmd: string,
options: ScriptOptions,
cli: string[],
): Command {
let out: string[] = [];
cmd = stdCmd(cmd).join(" ");
let denoAction = reDenoAction.exec(cmd);
if (denoAction && denoAction.length === 3) {
const action = denoAction[1];
const args = denoAction[2];
out = out.concat(stdCmd(action));
out = out.concat(buildFlags(options));
if (args) out = out.concat(stdCmd(args));
} else if (reCompact.test(cmd)) {
out = ["deno", "run"];
out = out.concat(buildFlags(options));
out = out.concat(stdCmd(cmd));
} else {
out = stdCmd(cmd);
}
if (cli) out = out.concat(cli);
return this.cmd(out, options); // single command
}
/** Build the script, in whatever form it is declared in,
* to be compatible with `Deno.run()`.
* This function add flags, arguments and actions. */
build(script: string): Command[] {
// global options
const g = Object.assign(
{
watch: true, // this is a file watcher after all :)
},
this.#config,
);
delete g.scripts;
const s: Script = this.#config.scripts[script];
if (!s) {
if (this.#args.length > 0) {
return [this.buildCliCommand(this.#args, g)];
} else {
throw new Error("Script does not exist and CLI args are not provided.");
}
}
let args = this.#args.slice(1);
let commands: Command[] = [];
if (Array.isArray(s)) {
s.forEach((ss) => {
commands = commands.concat(this.buildCommands(ss, g, args));
});
} else {
commands = commands.concat(this.buildCommands(s, g, args));
}
return commands;
}
/** Create an `Execution` object to handle the lifetime
* of the process that is executed. */
execute(command: Command): Deno.Process {
const options = {
cmd: command.cmd,
env: command.options.env ?? {},
stdin: command.options.stdin ?? "inherit",
stdout: command.options.stdout ?? "inherit",
stderr: command.options.stderr ?? "inherit",
};
return Deno.run(options);
}
}
function stdCmd(cmd: string): string[] {
return cmd.trim().replace(/\s\s+/g, " ").split(" ");
}
export interface Command {
cmd: string[];
options: ScriptOptions;
exe: () => Deno.Process;
}