-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.ts
66 lines (60 loc) · 1.96 KB
/
test.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
import { build, stop } from "esbuild";
import { denoPlugins } from "@luca/esbuild-deno-loader";
import { basename } from "@std/path";
import { extname } from "@std/path";
async function esbuild(inPath: string, outPath: string): Promise<void> {
const { errors, warnings } = await build({
plugins: denoPlugins(),
entryPoints: [inPath],
outfile: outPath,
format: "esm",
bundle: true,
minify: true,
sourcemap: "inline",
});
errors.forEach((error) => console.error(error));
warnings.forEach((warning) => console.warn(warning));
}
async function createScript(path: string): Promise<void> {
if (extname(path) !== ".ts") {
throw new TypeError(
`Expected a ".ts" type extension. Got ${extname(path)} in ${path}`,
);
}
const name = basename(path).slice(0, -3);
const promise = esbuild(path, `tests/${name}.min.js`);
const file = await Deno.create(`tests/${name}.user.js`);
await file.write(new TextEncoder().encode(
await async function (): Promise<string> {
const text = await Deno.readTextFile(path);
const a = text.indexOf("// ==UserScript==");
if (a == -1) {
throw new SyntaxError(
`Failed to locate "// ==UserScript==" in ${path}`,
);
}
const b = text.indexOf("// ==/UserScript==", a) +
"// ==/UserScript==".length;
if (b == -1) {
throw new SyntaxError(
`Failed to locate "// ==/UserScript==" in ${path}`,
);
}
return text.slice(a, b) + "\n'use strict';\n";
}(),
));
await promise;
await (await Deno.open(`tests/${name}.min.js`))
.readable
.pipeTo(file.writable);
await Deno.remove(`tests/${name}.min.js`);
}
await Deno.remove("tests/", { recursive: true }).catch(() => {});
await Deno.mkdir("./tests/", { recursive: true });
await Promise.allSettled(Deno.args.map((arg) => createScript(arg)));
stop();
console.log(
`${
performance.now().toLocaleString("en-US", { maximumFractionDigits: 2 })
}ms`,
);