-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
148 lines (124 loc) · 3.9 KB
/
helpers.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
import version from "./version.ts"
import { join } from "std/path/mod.ts"
import { Context, Script, Config, CmdArg } from "./interfaces.ts"
import { bold, green } from "https://deno.land/std@0.192.0/fmt/colors.ts"
export function cmdBuild (parts: TemplateStringsArray, ...args: CmdArg[]) {
return (context: Context) => parts.reduce((cmd, part, i) => {
cmd = cmd.concat(part)
const arg = args[i]
if (arg) {
if (typeof arg === "function") {
cmd = cmd.concat(arg(context))
} else {
cmd = cmd.concat(`${arg}`)
}
}
return cmd
}, "")
}
export function showShortCliHelp() {
showCliHeader()
console.log()
console.log("Show help:")
console.log(" launch -h")
console.log()
}
export function showCliHelp(config: Config) {
showCliHeader()
console.log()
console.log("Usage:")
console.log(" launch <command> [args...]")
console.log(" launch -h")
console.log(" launch -v")
console.log()
console.log("Options:")
console.log(" -h, --help Show this help")
console.log(" -v, --version Show version number")
console.log()
console.log("Config:")
console.log(` You can configure launch cli by editing ${bold('config')} constant in`, scriptsFile(config))
console.log(" See", bold(`https://deno.land/x/launch@${version}#config`), "for more info")
console.log()
}
export function showCliHeader() {
console.log(bold("Launch CLI"), green(`v${version}`))
}
export function showAllCommands(config: Config, commands: Record<string, Pick<Script, "desc">>) {
const commandNames = Object.keys(commands)
const maxLen = Math.max(...commandNames.map(cmd => cmd.length))
console.log(`Available scripts in ${scriptsFile(config)}\n`)
if (commandNames.length === 0) {
console.log(" <empty list>")
}
commandNames.forEach((cmd) => {
const script = commands[cmd]
if (script.desc) {
console.log(` ${cmd.padEnd(maxLen)} ${script.desc}`)
} else {
console.log(` ${cmd}`)
}
})
}
export const scriptsFile = (config: Config) => join(config.scriptsPath, config.scriptsFile)
export function showTheMostSimilarCommand(config: Config, commands: Record<string, unknown>, cmd: string) {
const cmdKeys = Object.keys(commands)
const like = cmd.toLowerCase().split("")
const PENALTY = -1000
const matches = cmdKeys.map((name) => {
const nameLike = name.toLowerCase()
return {
name, match: like.map((letter) => nameLike.indexOf(letter))
}
}).map(({name, match}) => ({
name,
match: match.map((value, i) => {
if (value === -1) return PENALTY
return Math.abs(value - i) * -1
}).reduce((a, b) => a + b)
}))
const mostSimilar = matches.reduce((match1, match2) => {
return match1.match >= match2.match ? match1 : match2
})
const minMatch = like.length * PENALTY
if (!mostSimilar || mostSimilar.match <= minMatch) {
return
}
console.log("\nThe most similar command is")
console.log(` ${mostSimilar.name}`)
const acc: Array<{ name: string, match: number }> = []
const others = matches.reduce((acc, match) => {
if (match.name == mostSimilar.name) return acc
if (match.match > minMatch) {
acc.push(match)
}
return acc
}, acc)
if (others.length === 0) {
return
}
others.sort((a, b) => b.match - a.match)
console.log("\nOther similar commands")
others.slice(0, config.othersCommandsToShow).forEach((match) => {
console.log(` ${match.name}`)
})
}
export function buildCommand (config: Config, context: Context, commands: CmdArg[]) {
return commands.map((cmd) => {
if (typeof cmd === "function") {
return cmd(context)
}
return cmd
}).join(config.commandSeparator)
}
export const exists = async (filename: string): Promise<boolean> => {
try {
await Deno.stat(filename);
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return false;
} else {
throw error;
}
}
};