-
Notifications
You must be signed in to change notification settings - Fork 366
/
main.ts
234 lines (204 loc) · 7.95 KB
/
main.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
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
import process from 'process'
import { Option } from 'commander'
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'envi... Remove this comment to see the full error message
import envinfo from 'envinfo'
import { closest } from 'fastest-levenshtein'
import inquirer from 'inquirer'
import { BANG, chalk, error, exit, log, NETLIFY_CYAN, USER_AGENT, warn } from '../utils/command-helpers.js'
import execa from '../utils/execa.js'
import getGlobalConfig from '../utils/get-global-config.js'
import getPackageJson from '../utils/get-package-json.js'
import { track, reportError } from '../utils/telemetry/index.js'
import { createAddonsCommand } from './addons/index.js'
import { createApiCommand } from './api/index.js'
import BaseCommand from './base-command.js'
import { createBlobsCommand } from './blobs/blobs.js'
import { createBuildCommand } from './build/index.js'
import { createCompletionCommand } from './completion/index.js'
import { createDeployCommand } from './deploy/index.js'
import { createDevCommand } from './dev/index.js'
import { createEnvCommand } from './env/index.js'
import { createFunctionsCommand } from './functions/index.js'
import { createInitCommand } from './init/index.js'
import { createIntegrationCommand } from './integration/index.js'
import { createLinkCommand } from './link/index.js'
import { createLmCommand } from './lm/index.js'
import { createLoginCommand } from './login/index.js'
import { createLogoutCommand } from './logout/index.js'
import { createLogsCommand } from './logs/index.js'
import { createOpenCommand } from './open/index.js'
import { createRecipesCommand } from './recipes/index.js'
import { createServeCommand } from './serve/index.js'
import { createSitesCommand } from './sites/index.js'
import { createStatusCommand } from './status/index.js'
import { createSwitchCommand } from './switch/index.js'
import { createUnlinkCommand } from './unlink/index.js'
import { createWatchCommand } from './watch/index.js'
const SUGGESTION_TIMEOUT = 1e4
process.on('uncaughtException', async (err) => {
console.log('')
error(
`${chalk.red(
'Netlify CLI has terminated unexpectedly',
)}\nThis is a problem with the Netlify CLI, not with your application.\nIf you recently updated the CLI, consider reverting to an older version by running:\n\n${chalk.bold(
'npm install -g netlify-cli@VERSION',
)}\n\nYou can use any version from ${chalk.underline(
'https://ntl.fyi/cli-versions',
)}.\n\nPlease report this problem at ${chalk.underline(
'https://ntl.fyi/cli-error',
)} including the error details below.\n`,
{ exit: false },
)
const systemInfo = await getSystemInfo()
console.log(chalk.dim(err.stack || err))
console.log(chalk.dim(systemInfo))
reportError(err, { severity: 'error' })
process.exit(1)
})
const getSystemInfo = () =>
envinfo.run({
System: ['OS', 'CPU'],
Binaries: ['Node', 'Yarn', 'npm'],
Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'],
npmGlobalPackages: ['netlify-cli'],
})
const getVersionPage = async () => {
const data = await getSystemInfo()
return `
────────────────────┐
Environment Info │
────────────────────┘
${data}
${USER_AGENT}
`
}
/**
* The main CLI command without any command (root action)
* @param {import('commander').OptionValues} options
* @param {import('./base-command.js').default} command
*/
// @ts-expect-error TS(7006) FIXME: Parameter 'options' implicitly has an 'any' type.
const mainCommand = async function (options, command) {
const globalConfig = await getGlobalConfig()
if (options.telemetryDisable) {
globalConfig.set('telemetryDisabled', true)
console.log('Netlify telemetry has been disabled')
console.log('You can re-enable it anytime with the --telemetry-enable flag')
exit()
}
if (options.telemetryEnable) {
globalConfig.set('telemetryDisabled', false)
console.log('Netlify telemetry has been enabled')
console.log('You can disable it anytime with the --telemetry-disable flag')
await track('user_telemetryEnabled')
exit()
}
if (command.args[0] === 'version' || options.version) {
if (options.verbose) {
const versionPage = await getVersionPage()
log(versionPage)
}
log(USER_AGENT)
exit()
}
// if no command show the header and the help
if (command.args.length === 0) {
const pkg = await getPackageJson()
const title = `${chalk.bgBlack.cyan('⬥ Netlify CLI')}`
const docsMsg = `${chalk.greenBright('Read the docs:')} https://ntl.fyi/get-started-with-netlify-cli`
const supportMsg = `${chalk.magentaBright('Support and bugs:')} ${pkg.bugs.url}`
console.log()
console.log(title)
console.log(docsMsg)
console.log(supportMsg)
console.log()
command.help()
}
if (command.args[0] === 'help') {
if (command.args[1]) {
// @ts-expect-error TS(7006) FIXME: Parameter 'cmd' implicitly has an 'any' type.
const subCommand = command.commands.find((cmd) => cmd.name() === command.args[1])
if (!subCommand) {
error(`command ${command.args[1]} not found`)
}
subCommand.help()
}
command.help()
}
warn(`${chalk.yellow(command.args[0])} is not a ${command.name()} command.`)
// @ts-expect-error TS(7006) FIXME: Parameter 'cmd' implicitly has an 'any' type.
const allCommands = command.commands.map((cmd) => cmd.name())
const suggestion = closest(command.args[0], allCommands)
const applySuggestion = await new Promise((resolve) => {
const prompt = inquirer.prompt({
type: 'confirm',
name: 'suggestion',
message: `Did you mean ${chalk.blue(suggestion)}`,
default: false,
})
setTimeout(() => {
// @ts-expect-error TS(2445) FIXME: Property 'close' is protected and only accessible ... Remove this comment to see the full error message
prompt.ui.close()
resolve(false)
}, SUGGESTION_TIMEOUT)
// eslint-disable-next-line promise/catch-or-return
prompt.then((value) => resolve(value.suggestion))
})
// create new log line
log()
if (!applySuggestion) {
error(`Run ${NETLIFY_CYAN(`${command.name()} help`)} for a list of available commands.`)
}
await execa(process.argv[0], [process.argv[1], suggestion], { stdio: 'inherit' })
}
/**
* Creates the `netlify-cli` command
* Promise is needed as the envinfo is a promise
* @returns {import('./base-command.js').default}
*/
export const createMainCommand = () => {
const program = new BaseCommand('netlify')
// register all the commands
createAddonsCommand(program)
createApiCommand(program)
createBlobsCommand(program)
createBuildCommand(program)
createCompletionCommand(program)
createDeployCommand(program)
createDevCommand(program)
createEnvCommand(program)
createFunctionsCommand(program)
createRecipesCommand(program)
createInitCommand(program)
createIntegrationCommand(program)
createLinkCommand(program)
createLmCommand(program)
createLoginCommand(program)
createLogoutCommand(program)
createOpenCommand(program)
createServeCommand(program)
createSitesCommand(program)
createStatusCommand(program)
createSwitchCommand(program)
createUnlinkCommand(program)
createWatchCommand(program)
createLogsCommand(program)
program
.version(USER_AGENT, '-V')
.showSuggestionAfterError(true)
.option('--telemetry-disable', 'Disable telemetry')
.option('--telemetry-enable', 'Enables telemetry')
// needed for custom version output as we display further environment information
// commanders version output is set to uppercase -V
.addOption(new Option('-v, --version').hideHelp())
.addOption(new Option('--verbose').hideHelp())
.noHelpOptions()
.configureOutput({
outputError: (message, write) => {
write(` ${chalk.red(BANG)} Error: ${message.replace(/^error:\s/g, '')}`)
write(` ${chalk.red(BANG)} See more help with --help\n`)
},
})
.action(mainCommand)
return program
}