-
Notifications
You must be signed in to change notification settings - Fork 343
/
base-command.js
511 lines (447 loc) · 15.9 KB
/
base-command.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// @ts-check
const process = require('process')
const { format } = require('util')
const { Command, Option } = require('commander')
const debug = require('debug')
const merge = require('lodash/merge')
// TODO: use static `import` after migrating this repository to pure ES modules
const jsClient = import('netlify')
const netlifyConfigPromise = import('@netlify/config')
const { getAgent } = require('../lib/http-agent')
const {
NETLIFY_CYAN,
StateConfig,
USER_AGENT,
chalk,
error,
exit,
getGlobalConfig,
getToken,
identify,
log,
normalizeConfig,
openBrowser,
padLeft,
pollForToken,
sortOptions,
track,
} = require('../utils')
// Netlify CLI client id. Lives in bot@netlify.com
// TODO: setup client for multiple environments
const CLIENT_ID = 'd6f37de6614df7ae58664cfca524744d73807a377f5ee71f1a254f78412e3750'
const NANO_SECS_TO_MSECS = 1e6
// The fallback width for the help terminal
const FALLBACK_HELP_CMD_WIDTH = 80
const HELP_$ = NETLIFY_CYAN('$')
// indent on commands or description on the help page
const HELP_INDENT_WIDTH = 2
// separator width between term and description
const HELP_SEPARATOR_WIDTH = 5
/**
* Formats a help list correctly with the correct indent
* @param {string[]} textArray
* @returns
*/
const formatHelpList = (textArray) => textArray.join('\n').replace(/^/gm, ' '.repeat(HELP_INDENT_WIDTH))
/**
* Get the duration between a start time and the current time
* @param {bigint} startTime
* @returns
*/
const getDuration = function (startTime) {
const durationNs = process.hrtime.bigint() - startTime
return Math.round(Number(durationNs / BigInt(NANO_SECS_TO_MSECS)))
}
/**
* The netlify object inside each command with the state
* @typedef NetlifyOptions
* @type {object}
* @property {import('netlify').NetlifyAPI} api
* @property {*} repositoryRoot
* @property {object} site
* @property {*} site.root
* @property {*} site.configPath
* @property {*} site.id
* @property {*} siteInfo
* @property {*} config
* @property {*} cachedConfig
* @property {*} globalConfig
* @property {StateConfig} state,
*/
/** Base command class that provides tracking and config initialization */
class BaseCommand extends Command {
/** @type {NetlifyOptions} */
netlify
/** @type {{ startTime: bigint, payload?: any}} */
analytics = { startTime: process.hrtime.bigint() }
/**
* IMPORTANT this function will be called for each command!
* Don't do anything expensive in there.
* @param {string} name The command name
* @returns
*/
createCommand(name) {
return (
new BaseCommand(name)
// If --silent or --json flag passed disable logger
.addOption(new Option('--json', 'Output return values as JSON').hideHelp(true))
.addOption(new Option('--silent', 'Silence CLI output').hideHelp(true))
.addOption(new Option('--cwd <cwd>').hideHelp(true))
.addOption(new Option('-o, --offline').hideHelp(true))
.addOption(new Option('--auth <token>', 'Netlify auth token').hideHelp(true))
.option(
'--httpProxyCertificateFilename [file]',
'Certificate file to use when connecting using a proxy server',
process.env.NETLIFY_PROXY_CERTIFICATE_FILENAME,
)
.option(
'--httpProxy [address]',
'Proxy server address to route requests through.',
process.env.HTTP_PROXY || process.env.HTTPS_PROXY,
)
.option('--debug', 'Print debugging information')
.hook('preAction', async (_parentCommand, actionCommand) => {
debug(`${name}:preAction`)('start')
this.analytics = { startTime: process.hrtime.bigint() }
// @ts-ignore cannot type actionCommand as BaseCommand
await this.init(actionCommand)
debug(`${name}:preAction`)('end')
})
)
}
/** @private */
noBaseOptions = false
/** don't show help options on command overview (mostly used on top commands like `addons` where options only apply on children) */
noHelpOptions() {
this.noBaseOptions = true
return this
}
/** The examples list for the command (used inside doc generation and help page) */
examples = []
/**
* Set examples for the command
* @param {string[]} examples
*/
addExamples(examples) {
this.examples = examples
return this
}
/**
* Overrides the help output of commander with custom styling
* @returns {import('commander').Help}
*/
createHelp() {
const help = super.createHelp()
help.commandUsage = (command) => {
const term =
this.name() === 'netlify'
? `${HELP_$} ${command.name()} [COMMAND]`
: `${HELP_$} ${command.parent.name()} ${command.name()} ${command.usage()}`
return padLeft(term, HELP_INDENT_WIDTH)
}
const getCommands = (command) => {
const parentCommand = this.name() === 'netlify' ? command : command.parent
return parentCommand.commands.filter((cmd) => {
// eslint-disable-next-line no-underscore-dangle
if (cmd._hidden) return false
// the root command
if (this.name() === 'netlify') {
// don't include subcommands on the main page
return !cmd.name().includes(':')
}
return cmd.name().startsWith(`${command.name()}:`)
})
}
/**
* override the longestSubcommandTermLength
* @param {BaseCommand} command
* @returns {number}
*/
help.longestSubcommandTermLength = (command) =>
getCommands(command).reduce((max, cmd) => Math.max(max, cmd.name().length), 0)
/**
* override the longestOptionTermLength to react on hide options flag
* @param {BaseCommand} command
* @param {import('commander').Help} helper
* @returns {number}
*/
help.longestOptionTermLength = (command, helper) =>
(command.noBaseOptions === false &&
helper.visibleOptions(command).reduce((max, option) => Math.max(max, helper.optionTerm(option).length), 0)) ||
0
/**
* override the format help function to style it correctly
* @param {BaseCommand} command
* @param {import('commander').Help} helper
* @returns {string}
*/
help.formatHelp = (command, helper) => {
const parentCommand = this.name() === 'netlify' ? command : command.parent
const termWidth = helper.padWidth(command, helper)
const helpWidth = helper.helpWidth || FALLBACK_HELP_CMD_WIDTH
/**
* formats a term correctly
* @param {string} term
* @param {string} [description]
* @param {boolean} [isCommand]
* @returns {string}
*/
const formatItem = (term, description, isCommand = false) => {
const bang = isCommand ? `${HELP_$} ` : ''
if (description) {
const pad = termWidth + HELP_SEPARATOR_WIDTH
const fullText = `${bang}${term.padEnd(pad - (isCommand ? 2 : 0))}${chalk.grey(description)}`
return helper.wrap(fullText, helpWidth - HELP_INDENT_WIDTH, pad)
}
return `${bang}${term}`
}
/** @type {string[]} */
let output = []
// Description
const [topDescription, ...commandDescription] = (helper.commandDescription(command) || '').split('\n')
if (topDescription.length !== 0) {
output = [...output, topDescription, '']
}
// on the parent help command the version should be displayed
if (this.name() === 'netlify') {
output = [...output, chalk.bold('VERSION'), formatHelpList([formatItem(USER_AGENT)]), '']
}
// Usage
output = [...output, chalk.bold('USAGE'), helper.commandUsage(command), '']
// Arguments
const argumentList = helper
.visibleArguments(command)
.map((argument) => formatItem(helper.argumentTerm(argument), helper.argumentDescription(argument)))
if (argumentList.length !== 0) {
output = [...output, chalk.bold('ARGUMENTS'), formatHelpList(argumentList), '']
}
if (command.noBaseOptions === false) {
// Options
const optionList = helper
.visibleOptions(command)
.sort(sortOptions)
.map((option) => formatItem(helper.optionTerm(option), helper.optionDescription(option)))
if (optionList.length !== 0) {
output = [...output, chalk.bold('OPTIONS'), formatHelpList(optionList), '']
}
}
// Description
if (commandDescription.length !== 0) {
output = [...output, chalk.bold('DESCRIPTION'), formatHelpList(commandDescription), '']
}
// Aliases
// eslint-disable-next-line no-underscore-dangle
if (command._aliases.length !== 0) {
// eslint-disable-next-line no-underscore-dangle
const aliases = command._aliases.map((alias) => formatItem(`${parentCommand.name()} ${alias}`, null, true))
output = [...output, chalk.bold('ALIASES'), formatHelpList(aliases), '']
}
if (command.examples.length !== 0) {
output = [
...output,
chalk.bold('EXAMPLES'),
formatHelpList(command.examples.map((example) => `${HELP_$} ${example}`)),
'',
]
}
const commandList = getCommands(command).map((cmd) =>
formatItem(cmd.name(), helper.subcommandDescription(cmd).split('\n')[0], true),
)
if (commandList.length !== 0) {
output = [...output, chalk.bold('COMMANDS'), formatHelpList(commandList), '']
}
return [...output, ''].join('\n')
}
return help
}
/**
* Will be called on the end of an action to track the metrics
* @param {*} [error_]
*/
async onEnd(error_) {
const { payload, startTime } = this.analytics
const duration = getDuration(startTime)
const status = error_ === undefined ? 'success' : 'error'
const command = Array.isArray(this.args) ? this.args[0] : this.name()
debug(`${this.name()}:onEnd`)(`Command: ${command}. Status: ${status}. Duration: ${duration}ms`)
try {
await track('command', {
...payload,
command,
duration,
status,
})
} catch {}
if (error_ !== undefined) {
error(error_ instanceof Error ? error_ : format(error_), { exit: false })
exit(1)
}
}
async authenticate(tokenFromFlag) {
const [token] = await getToken(tokenFromFlag)
if (token) {
return token
}
return this.expensivelyAuthenticate()
}
async expensivelyAuthenticate() {
const webUI = process.env.NETLIFY_WEB_UI || 'https://app.netlify.com'
log(`Logging into your Netlify account...`)
// Create ticket for auth
// @ts-ignore Types from api are wrong and they don't recognize `createTicket`
const ticket = await this.netlify.api.createTicket({
clientId: CLIENT_ID,
})
// Open browser for authentication
const authLink = `${webUI}/authorize?response_type=ticket&ticket=${ticket.id}`
log(`Opening ${authLink}`)
await openBrowser({ url: authLink })
const accessToken = await pollForToken({
api: this.netlify.api,
ticket,
})
// @ts-ignore Types from api are wrong and they don't recognize `getCurrentUser`
const { email, full_name: name, id: userId } = await this.netlify.api.getCurrentUser()
const userData = merge(this.netlify.globalConfig.get(`users.${userId}`), {
id: userId,
name,
email,
auth: {
token: accessToken,
github: {
user: undefined,
token: undefined,
},
},
})
// Set current userId
this.netlify.globalConfig.set('userId', userId)
// Set user data
this.netlify.globalConfig.set(`users.${userId}`, userData)
await identify({
name,
email,
userId,
})
await track('user_login', {
email,
})
// Log success
log()
log(`${chalk.greenBright('You are now logged into your Netlify account!')}`)
log()
log(`Run ${chalk.cyanBright('netlify status')} for account details`)
log()
log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`)
log()
return accessToken
}
setAnalyticsPayload(payload) {
this.analytics = { ...this.analytics, payload }
}
/**
* Initializes the options and parses the configuration needs to be called on start of a command function
* @param {BaseCommand} actionCommand The command of the action that is run (`this.` gets the parent command)
* @private
*/
async init(actionCommand) {
debug(`${actionCommand.name()}:init`)('start')
const options = actionCommand.opts()
const cwd = options.cwd || process.cwd()
// Get site id & build state
const state = new StateConfig(cwd)
const [token] = await getToken(options.auth)
const apiUrlOpts = {
userAgent: USER_AGENT,
}
if (process.env.NETLIFY_API_URL) {
const apiUrl = new URL(process.env.NETLIFY_API_URL)
apiUrlOpts.scheme = apiUrl.protocol.slice(0, -1)
apiUrlOpts.host = apiUrl.host
apiUrlOpts.pathPrefix =
process.env.NETLIFY_API_URL === `${apiUrl.protocol}//${apiUrl.host}` ? '/api/v1' : apiUrl.pathname
}
const cachedConfig = await actionCommand.getConfig({ cwd, state, token, ...apiUrlOpts })
const { buildDir, config, configPath, repositoryRoot, siteInfo } = cachedConfig
const normalizedConfig = normalizeConfig(config)
const agent = await getAgent({
httpProxy: options.httpProxy,
certificateFile: options.httpProxyCertificateFilename,
})
const apiOpts = { ...apiUrlOpts, agent }
const globalConfig = await getGlobalConfig()
const { NetlifyAPI } = await jsClient
actionCommand.netlify = {
// api methods
api: new NetlifyAPI(token || '', apiOpts),
apiOpts,
repositoryRoot,
// current site context
site: {
root: buildDir,
configPath,
get id() {
return state.get('siteId')
},
set id(id) {
state.set('siteId', id)
},
},
// Site information retrieved using the API
siteInfo,
// Configuration from netlify.[toml/yml]
config: normalizedConfig,
// Used to avoid calling @netlify/config again
cachedConfig,
// global cli config
globalConfig,
// state of current site dir
state,
}
debug(`${this.name()}:init`)('end')
}
/**
* Find and resolve the Netlify configuration
* @param {*} config
* @returns {ReturnType<import('@netlify/config/src/main')>}
*/
async getConfig(config) {
const options = this.opts()
const { cwd, host, offline = options.offline, pathPrefix, scheme, state, token } = config
const { resolveConfig } = await netlifyConfigPromise
try {
return await resolveConfig({
config: options.config,
cwd,
context:
options.context ||
process.env.CONTEXT ||
// Dev commands have a default context of `dev`, otherwise we let netlify/config handle default behavior
(['dev', 'dev:exec', 'dev:trace'].includes(this.name()) ? 'dev' : undefined),
debug: this.opts().debug,
siteId: options.siteId || (typeof options.site === 'string' && options.site) || state.get('siteId'),
token,
mode: 'cli',
host,
pathPrefix,
scheme,
offline,
})
} catch (error_) {
const isUserError = error_.customErrorInfo !== undefined && error_.customErrorInfo.type === 'resolveConfig'
// If we're failing due to an error thrown by us, it might be because the token we're using is invalid.
// To account for that, we try to retrieve the config again, this time without a token, to avoid making
// any API calls.
//
// @todo Replace this with a mechanism for calling `resolveConfig` with more granularity (i.e. having
// the option to say that we don't need API data.)
if (isUserError && !offline && token) {
return this.getConfig({ cwd, offline: true, state, token })
}
const message = isUserError ? error_.message : error_.stack
console.error(message)
exit(1)
}
}
}
module.exports = { BaseCommand }