forked from BlackAsLight/DocScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
63 lines (57 loc) · 1.79 KB
/
test.ts
File metadata and controls
63 lines (57 loc) · 1.79 KB
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
import { TextLineStream } from "@std/streams/mod.ts"
// @deno-types="@esbuild/mod.d.ts"
import { build, stop } from "@esbuild/mod.js"
import { denoPlugins } from "@esbuild_deno_loader/mod.ts"
async function esbuild(inPath: string, outPath: string) {
const { errors, warnings } = await build({
plugins: denoPlugins(),
entryPoints: [ inPath ],
outfile: outPath,
format: 'esm',
bundle: true,
keepNames: true,
jsxFactory: 'x',
jsxFragment: 'y'
})
errors.forEach(error => console.error(error))
warnings.forEach(warning => console.warn(warning))
}
async function createScript(path: string) {
const lines: string[] = []
{
let copyLine = false
for await (const line of (await Deno.open(path)).readable.pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream())) {
if (!copyLine)
if (line.trim() === '// ==UserScript==')
copyLine = true
else
continue
lines.push(line.trim())
if (line.trim() === '// ==/UserScript==')
break
}
}
if (!lines.length)
return
lines.push('\'use strict\';\n')
const name = path.slice(path.lastIndexOf('/') + 1, path.lastIndexOf('.'))
const file = await Deno.create(`./tests/${name}.user.js`)
await file.write(Uint8Array.from(lines.join('\n').split('').map(char => char.charCodeAt(0))))
await esbuild(path, `./tests/${name}.min.js`)
await file.write(await Deno.readFile(`./tests/${name}.min.js`))
file.close()
await Deno.remove(`./tests/${name}.min.js`)
}
/* Create ./tests/
-------------------------*/
try {
await Deno.remove('./tests/', { recursive: true })
}
// deno-lint-ignore no-empty
catch { }
finally {
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`)