-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdev-tool.ts
executable file
·304 lines (285 loc) · 8.24 KB
/
dev-tool.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { exists, expandGlob } from "fs/mod.ts";
import "./docshim.ts";
interface DevEnv {
// readable metadata
revision: string;
// workspace information
commit: string;
tag?: string;
dirty: boolean;
deno: { deno: string; v8: string; typescript: string };
// make indexable
// deno-lint-ignore no-explicit-any
[k: string]: any;
}
async function getDevEnv(): Promise<DevEnv> {
const revparse = new Deno.Command("git", {
args: ["rev-parse", "--short", "HEAD"],
stdout: "piped",
});
const commit = new TextDecoder().decode((await revparse.output()).stdout)
.trim();
const namerev = new Deno.Command("git", {
args: ["name-rev", "--tags", "--name-only", commit],
stdout: "piped",
});
const tag = new TextDecoder().decode((await namerev.output()).stdout).trim();
const diff = new Deno.Command("git", {
args: ["diff", "--quiet"],
stdout: "piped",
});
const { code: diffStatus } = await diff.output();
const env = {
commit,
tag: tag !== "undefined" ? tag : undefined,
dirty: !!diffStatus,
deno: Deno.version,
};
return {
...env,
revision: `${env.tag || env.commit}${env.dirty ? "-dirty" : ""}`,
};
}
const denoImportMapFlag = "--import-map=./import-map.json";
interface DevScripts {
[k: string]: (args: string[], env: DevEnv) => Promise<void> | void;
}
const helpCommand = "help";
const devScripts: DevScripts = {
[helpCommand]: () => {
console.log("READABLE DEV TOOL");
console.log("=================");
console.log(`Available commands:\n`);
for (const command of Object.keys(devScripts)) {
console.log(` ${command}`);
}
console.log();
console.log(
"For more help, refer to https://github.com/bobheadxi/readable/blob/main/CONTRIBUTING.md",
);
},
env: (args, env) => {
if (args.length === 0) {
console.log(env);
} else {
console.log(env[args[0]]);
}
},
/**
* Checks to run before commit.
*
* @param args directories
*/
precommit: async (args, env) => {
for (const command of ["fmt", "test"]) {
console.log(`>>> ${command}`);
await devScripts[command](args, env);
}
},
/**
* Run formatter.
*
* @param args 'check' as first to check, rest are directories
*/
fmt: async (args, env) => {
const check = args ? args[0] === "check" : false;
const watch = args ? args[0] === "watch" : false;
const dir = check || watch ? args[1] : args[0];
const denoArgs = ["fmt"];
if (check) {
denoArgs.push("--check");
} else {
denoArgs.push("--quiet");
}
if (watch) {
denoArgs.push("--watch");
}
// 'deno fmt' does not have glob support yet: https://github.com/denoland/deno/issues/6365
// so we implement our own. this is required because 'deno fmt' now formats markdown,
// which I don't want.
const globPattern = `${dir ? `${dir}/` : ""}**/**.ts`;
console.log(`${check ? "Checking" : "Formatting"} '${globPattern}'`);
for await (const f of expandGlob(globPattern)) {
if (!f.name.endsWith(".ts")) continue;
const p = new Deno.Command("deno", { args: [...denoArgs, f.path] });
const { code } = await p.output();
if (code) {
throw new Error(`fmt exited with status ${code} on file ${f.path}`);
}
}
// Dogfood readable formatting
await devScripts["readable"]([check ? "check" : "fmt", "**/*.md"], env);
},
/**
* Run tests with coverage.
*
* @param args directories
*/
test: async (args) => {
// Test coverage https://deno.land/manual/testing#test-coverage
const coverageDir = "cov_profile";
if (await exists(coverageDir)) {
await Deno.remove(coverageDir, { recursive: true });
}
const test = new Deno.Command("deno", {
args: [
"test",
denoImportMapFlag,
"--no-check",
`--coverage=${coverageDir}`,
...args,
],
stdout: "inherit",
stderr: "inherit",
});
const { code: testCode } = await test.output();
if (testCode) {
throw new Error(`test exited with status ${testCode}`);
}
console.log("all tests ok!");
const coverageSummary = `${coverageDir}.lcov`;
if (await exists(coverageSummary)) {
await Deno.remove(coverageSummary);
}
const renderCoverage = new Deno.Command("deno", {
args: [
"coverage",
coverageDir,
"--lcov",
`--output=${coverageSummary}`,
],
});
const { code: coverageCode } = await renderCoverage.output();
if (coverageCode != 0) {
console.warn(`coverage render exited with status ${coverageCode}`);
}
},
/**
* Run readable.
*
* @param args 'install' as first to install, otherwise 'readable' arguments
*/
readable: async (args) => {
const install = args ? args[0] === "install" : false;
const target = ["./readable.ts"];
if (!install) {
target.push(...args);
}
const denoArgs = [
install ? "install" : "run",
denoImportMapFlag,
"--allow-read",
"--allow-write",
];
if (install) {
denoArgs.push("--force"); // force install
}
const p = new Deno.Command("deno", { args: [...denoArgs, ...target] });
const { code } = await p.output();
if (code) {
console.error(`readable exited with status ${code}`);
Deno.exit(code);
}
},
upgrade: async (args) => {
if (args.length !== 3) {
throw new Error(`requires arguments [target] [previous] [next]`);
}
const [target, previous, next] = args;
console.log(`Upgrading ${target} from ${previous} to ${next}`);
switch (target) {
case "deno": {
const files = [
"./.github/workflows/pipeline.yml",
"./.github/workflows/publish-latest.yml",
"./.github/workflows/publish-release.yml",
"./Dockerfile",
];
for (const f of files) {
const content = await Deno.readTextFile(f);
await Deno.writeTextFile(f, content.replaceAll(previous, next));
}
break;
}
case "deno-std": {
const content = await Deno.readTextFile("import-map.json");
await Deno.writeTextFile(
"import-map.json",
content.replaceAll(previous, next),
);
break;
}
default:
throw new Error(`unknown target ${target}`);
}
},
build: async (args, env) => {
if (args.length !== 1) {
throw new Error(`requires argument [target]`);
}
const [target] = args;
console.log(`Building ${target} @ ${env.revision}`);
if (env.dirty) {
console.warn("Warning: building dirty commit");
}
switch (target) {
case "docker": {
const p = new Deno.Command("docker", {
args: [
"build",
"-t",
`bobheadxi/readable:${env.revision}`,
".",
],
});
const { code } = await p.output();
if (code) {
throw new Error(`command failed with status ${code}`);
}
break;
}
default:
throw new Error(`unknown target ${target}`);
}
},
release: async (args, env) => {
const version = args[0];
if (!version) {
throw new Error(`version required`);
}
await Deno.writeTextFile(
"./version.ts",
`// Generated by './dev release ${version}'
export const READABLE_VERSION = "${version}";
export const READABLE_COMMIT = "${env.commit}";
`,
);
const commit = new Deno.Command("git", {
args: ["commit", "-a", "-m", `all: release readable@${version}`],
});
const { code: commitStatus } = await commit.output();
if (commitStatus) {
throw new Error("failed to commit");
}
const tag = new Deno.Command("git", {
args: ["git", "tag", version, "-m", `readable@${version}`],
});
const { code: tagStatus } = await tag.output();
if (tagStatus) {
throw new Error("failed to tag");
}
},
};
if (import.meta.main) {
const env = await getDevEnv();
const command = Deno.args[0] || helpCommand;
const scriptArgs = [...Deno.args].splice(1);
const runScript = devScripts[command];
if (!runScript) {
await devScripts[helpCommand](scriptArgs, env);
console.error(`Command '${command}' not found`);
Deno.exit(1);
}
await runScript(scriptArgs, env);
}
export default devScripts;