Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
219 lines (182 loc) · 6.41 KB
/
Copy pathcli.ts
File metadata and controls
219 lines (182 loc) · 6.41 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import chalk from 'chalk'
import { log } from '../nexus-logger'
import { HelpError, unknownCommand } from './help'
import { arg, format, isError } from './helpers'
import {
Command,
CommandNamespace,
CommandNode,
CommandRef,
CommandsLayout,
CommandsLayoutEntry,
ConcreteCommand,
} from './types'
import { Version } from './version'
function createCommandRef(value: string, parent: CommandNamespace): CommandRef {
const referencedCommand = parent.value[value] as undefined | CommandNode
if (referencedCommand === undefined) {
throw new Error(`Unable to deref "${value}" on ${parent}`)
}
if (referencedCommand.type !== 'concrete_command') {
throw new Error(
`References must point to a concrete command. The ref "${value}" actually pointed to ${referencedCommand}.`
)
}
return {
type: 'command_reference',
value: {
original: value,
commandPointer: referencedCommand,
},
parent,
}
}
function createConcreteCommand(value: Command, parent: CommandNamespace): ConcreteCommand {
return { type: 'concrete_command', value, parent }
}
function createCommandNamespace(
value: Record<string, CommandNode>,
parent: null | CommandNamespace
): CommandNamespace {
return { type: 'command_namespace', value, parent }
}
function buildCommandsTree(cmds: CommandsLayout): CommandNode {
return buildCommandsSubTree(cmds, null)
}
function buildCommandsSubTree(cmds: CommandsLayout, parent: null | CommandNamespace): CommandNode {
const branch = createCommandNamespace({}, parent)
for (const [name, cmd] of Object.entries(cmds)) {
branch.value[name] = buildCommandsEntry(cmd, branch)
}
return branch
}
function buildCommandsEntry(cmd: CommandsLayoutEntry, parent: CommandNamespace): CommandNode {
if (typeof cmd === 'string') {
// TODO need concept of createUnresolvedCommandRef because de-referring
// needs to come after the tree of concrete command values has been built.
// Easy workaround for now is to keep all reference nodes at the bottom of
// their respective namespace...!
return createCommandRef(cmd, parent)
} else if (typeof cmd.parse === 'function') {
return createConcreteCommand(cmd as Command, parent)
} else if (typeof cmd === 'object') {
return buildCommandsSubTree(cmd as CommandsLayout, parent)
} else {
throw new Error(`Could not process given commands layout entry: ${cmd}`)
}
}
/**
* Helper function for looking up a command by name on a command namespace that
* correctly types the possibility of lookup failing.
*/
function lookupCommand(name: string, namespace: CommandNamespace): undefined | CommandNode {
return namespace.value[name]
}
function isFlag(arg: string | undefined) {
return arg && (arg.startsWith('-') || arg.startsWith('--'))
}
/**
* CLI command
*/
export class CLI implements Command {
constructor(private readonly cmds: CommandsLayout) {}
// TODO setup stop at positional option, have each sub-command parse in turn
// https://github.com/zeit/arg#stopatpositional
async parse(argv: string[]) {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--version': Boolean,
'-v': '--version',
})
if (isError(args)) {
return this.help(args.message)
}
if (args['--version']) {
return Version.new().parse(argv)
}
if (args['--help']) {
return this.help()
}
// parse the invocation path
let targettedCommand = buildCommandsTree(this.cmds)
while (true) {
if (targettedCommand.type !== 'command_namespace') break
const nextArg = args._.shift()
if (nextArg === undefined) break
// If it's a flag, enqueue back the flag and break
if (isFlag(nextArg)) {
args._ = [nextArg, ...args._]
break
}
const nextCommandNode = lookupCommand(nextArg, targettedCommand)
if (nextCommandNode === undefined) {
return unknownCommand(CLI.help, nextArg)
}
targettedCommand = nextCommandNode
}
// Check if
for (const nextArg of args._) {
if (targettedCommand.type !== 'command_namespace') break
if (nextArg === undefined) break
if (lookupCommand(nextArg, targettedCommand)) {
log.fatal('Flags always needs to be in the last position')
process.exit(1)
}
}
// Resolve the runner
let run: null | Function = null
switch (targettedCommand.type) {
case 'concrete_command':
run = targettedCommand.value.parse.bind(targettedCommand.value)
break
case 'command_reference':
run = targettedCommand.value.commandPointer.value.parse.bind(
targettedCommand.value.commandPointer.value
)
break
case 'command_namespace':
const nsDefault = lookupCommand('__default', targettedCommand)
// When no sub-command given display help or the default sub-command if
// registered
if (nsDefault === undefined) {
// TODO should return command help, rather than assuming root help
return this.help()
} else if (nsDefault.type === 'concrete_command') {
run = nsDefault.value.parse.bind(nsDefault.value)
} else if (nsDefault.type === 'command_reference') {
run = nsDefault.value.commandPointer.value.parse.bind(nsDefault.value.commandPointer.value)
} else {
throw new Error(
`Attempt to run namespace default failed because was not a command or reference to a command. Was: ${nsDefault}`
)
}
}
return run(args._).catch((e: Error) => e) // treat error like Either type
}
// help function
private help(error?: string): string | HelpError {
if (error) {
return new HelpError(`\n${chalk.bold.red(`!`)} ${error}\n${CLI.help}`)
}
return CLI.help
}
// TODO generate this from cli tree
// static help template
private static help = format(`
Code-First Type-Safe GraphQL Framework - https://nexusjs.org
${chalk.bold('Usage')}
${chalk.dim(`$`)} nexus [command]
${chalk.bold('Commands')}
create Setup a ready-to-use nexus
dev Develop your application in watch mode
build Build a production-ready server
${chalk.bold('Examples')}
Initialize files for a new nexus project
${chalk.dim(`$`)} nexus create
Start developing and watch your changes locally
${chalk.dim(`$`)} nexus dev
Build a production-ready server
${chalk.dim(`$`)} nexus build
`)
}