-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
73 lines (59 loc) · 1.94 KB
/
build.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
const flags = [
{ name: "--import-map", deno: true, req: true },
{ name: "--dist-dir" , deno: false, req: true },
{ name: "--watch" , deno: true, req: false },
{ name: "-w" , deno: false, req: false }
]
const buildFiles = [
"build/cacheDb.ts",
"build/cacheVersion.ts",
"build/processCss.ts"
]
function validateArgs(args: string[]): void {
const reqFlags = flags.filter( ({ req }) => req )
const hasReqFlags: boolean = reqFlags
.every( ({ name }) => args.some( arg => arg.startsWith(name)))
if (!hasReqFlags) {
const missingArgs: string = reqFlags
.filter( ({ name }) => !args.includes(name)).join(" ")
throw(`error: mising arguments: ${missingArgs}`)
}
const unknownArgs: string[] = args.filter( arg => {
return !flags.some( ({ name }) => arg.startsWith(name))
})
if (unknownArgs.length) {
throw(`error: unknown arguments: ${unknownArgs.join(" ")}`)
}
}
async function subProcess(file: string, denoArgs: string[], custArgs: string[]): Promise<void> {
const process = Deno.run({
cmd: [
"deno",
"run",
"-A",
"--unstable", // processCss needs deno.futime
...denoArgs,
file,
...custArgs
],
stdout: "piped",
stderr: "piped",
})
const { code } = await process.status()
const rawOutput = await process.output()
const rawError = await process.stderrOutput()
if (code === 0) {
await Deno.stdout.write(rawOutput)
} else {
const errorString = new TextDecoder().decode(rawError)
console.error(errorString)
}
}
console.log("starting build process...")
await validateArgs(Deno.args)
const denoArgs = Deno.args
.filter( arg => flags.some( ({ deno, name }) => deno === true && arg.startsWith(name)))
const custArgs = Deno.args
.filter( arg => flags.some( ({ deno, name }) => deno === false && arg.startsWith(name)))
await Promise.all(buildFiles.map( file => subProcess(file, denoArgs, custArgs)))
console.log("build complete")