-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.ts
68 lines (55 loc) · 1.59 KB
/
core.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
import version from "./version.ts"
import { Config, Scripts } from "./interfaces.ts"
import { SEP, resolve } from "std/path/mod.ts"
import { spawn } from "node:child_process"
import { buildCommand, showAllCommands, showCliHelp, showShortCliHelp, showTheMostSimilarCommand } from "./helpers.ts"
export function launch ({
config, scripts
}: {
config: Config, scripts: Scripts
}) {
const [command] = Deno.args
if (!command) {
showShortCliHelp()
showAllCommands(config, scripts)
return
}
if (["-v", "--version"].includes(command)) {
console.log(version)
return
}
if (["-h", "--help"].includes(command)) {
showCliHelp(config)
showAllCommands(config, scripts)
return
}
const scheme = scripts[command]
if (!scheme) {
console.log("Unknown command")
showTheMostSimilarCommand(config, scripts, command)
return
}
const replacer = (_: string, group: string) => Deno.env.get(group) + SEP;
scheme.dir = scheme.dir || Deno.cwd()
scheme.dir = scheme.dir + SEP
scheme.dir = scheme.dir.replace(config.envRegexp, replacer)
scheme.dir = resolve(scheme.dir)
let cmdToBuild = scheme.cmd
let finalArgs = Deno.args.slice(1)
if (typeof cmdToBuild === "function") {
const result = cmdToBuild({ args: [...finalArgs] })
if (Array.isArray(result)) {
cmdToBuild = result
} else {
finalArgs = result.args
cmdToBuild = result.cmd
}
}
const cmd = buildCommand(config, { path: scheme.dir }, cmdToBuild)
.concat(" ")
.concat(finalArgs.join(" "))
spawn(
cmd,
{ stdio: 'inherit', shell: true, cwd: scheme.dir }
)
}