-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathcommand-helpers.ts
More file actions
306 lines (260 loc) · 9.56 KB
/
command-helpers.ts
File metadata and controls
306 lines (260 loc) · 9.56 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
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
import os from 'os'
import fs from 'fs'
import process from 'process'
import { format, inspect } from 'util'
import type { NetlifyAPI } from '@netlify/api'
import { getAPIToken } from '@netlify/dev-utils'
import { Chalk, type ChalkInstance as ChalkInstancePrimitiveType } from 'chalk'
import type { Option } from 'commander'
import WSL from 'is-wsl'
import terminalLink from 'terminal-link'
import { startSpinner } from '../lib/spinner.js'
import getCLIPackageJson from './get-cli-package-json.js'
import { reportError } from './telemetry/report-error.js'
import type { TokenLocation } from './types.js'
import type { CachedConfig } from '../lib/build.js'
/** The parsed process argv without the binary only arguments and flags */
const argv = process.argv.slice(2)
/**
* Chalk instance for CLI that can be initialized with no colors mode
* needed for json outputs where we don't want to have colors
* @param {boolean} noColors - disable chalk colors
* @return {ChalkInstancePrimitiveType} - default or custom chalk instance
*/
const safeChalk = function (noColors: boolean) {
if (noColors) {
const colorlessChalk = new Chalk({ level: 0 })
return colorlessChalk
}
return new Chalk()
}
export const chalk = safeChalk(argv.includes('--json'))
export type ChalkInstance = ChalkInstancePrimitiveType
/**
* Adds the filler to the start of the string
* @param {string} str
* @param {number} count
* @param {string} [filler]
* @returns {string}
*/
export const padLeft = (str: string, count: number, filler = ' ') => str.padStart(str.length + count, filler)
const platform = WSL ? 'wsl' : os.platform()
const arch = os.arch() === 'ia32' ? 'x86' : os.arch()
const { name, version: packageVersion } = await getCLIPackageJson()
export const version = packageVersion
export const USER_AGENT = `${name}/${version} ${platform}-${arch} node-${process.version}`
/** A list of base command flags that needs to be sorted down on documentation and on help pages */
const BASE_FLAGS = new Set(['--debug', '--http-proxy', '--http-proxy-certificate-filename'])
export const NETLIFY_CYAN = chalk.rgb(40, 180, 170)
export const NETLIFY_CYAN_HEX = '#28b5ac'
// TODO(serhalp) I *think* this "dev" naming is a vestige of the predecessor of the CLI? Rename to avoid
// confusion with `netlify dev` command?
export const NETLIFYDEVLOG = chalk.greenBright('⬥')
export const NETLIFYDEVWARN = chalk.yellowBright('⬥')
export const NETLIFYDEVERR = chalk.redBright('⬥')
export const BANG = process.platform === 'win32' ? '»' : '›'
/**
* Sorts two options so that the base flags are at the bottom of the list
* @param {import('commander').Option} optionA
* @param {import('commander').Option} optionB
* @returns {number}
* @example
* options.sort(sortOptions)
*/
export const sortOptions = (optionA: Option, optionB: Option) => {
// base flags should be always at the bottom
if ((optionA.long && BASE_FLAGS.has(optionA.long)) || (optionB.long && BASE_FLAGS.has(optionB.long))) {
return -1
}
return (optionA.long ?? '').localeCompare(optionB.long ?? '')
}
// Poll Token timeout 5 Minutes
const TOKEN_TIMEOUT = 3e5
export const pollForToken = async ({
api,
ticket,
}: {
api: NetlifyAPI
ticket: { id?: string; client_id?: string; authorized?: boolean; created_at?: string }
}) => {
const spinner = startSpinner({ text: 'Waiting for authorization...' })
try {
const accessToken = await api.getAccessToken(ticket, { timeout: TOKEN_TIMEOUT })
if (!accessToken) {
return logAndThrowError('Could not retrieve access token')
}
return accessToken
} catch (error_) {
if (error_ instanceof Error && error_.name === 'TimeoutError') {
return logAndThrowError(
`Timed out waiting for authorization. If you do not have a ${chalk.bold.greenBright(
'Netlify',
)} account, please create one at ${chalk.magenta(
'https://app.netlify.com/signup',
)}, then run ${chalk.cyanBright('netlify login')} again.`,
)
} else {
return logAndThrowError(error_)
}
} finally {
spinner.stop()
spinner.clear()
}
}
/**
* Get a netlify token
* @param {string} [tokenFromOptions] optional token from the provided --auth options
* @returns {Promise<[null|string, 'flag' | 'env' |'config' |'not found']>}
*/
export type TokenTuple = [string | null, TokenLocation]
export const getToken = async (tokenFromOptions?: string): Promise<TokenTuple> => {
// 1. First honor command flag --auth
if (tokenFromOptions) {
return [tokenFromOptions, 'flag']
}
// 2. then Check ENV var
const { NETLIFY_AUTH_TOKEN } = process.env
if (NETLIFY_AUTH_TOKEN && NETLIFY_AUTH_TOKEN !== 'null') {
return [NETLIFY_AUTH_TOKEN, 'env']
}
// 3. If no env var use global user setting
const tokenFromConfig = await getAPIToken()
if (tokenFromConfig) {
return [tokenFromConfig, 'config']
}
return [null, 'not found']
}
// 'api' command uses JSON output by default
// 'functions:invoke' need to return the data from the function as is
const isDefaultJson = () => argv[0] === 'functions:invoke' || (argv[0] === 'api' && !argv.includes('--list'))
/**
* logs a json message
*/
export const logJson = (message: unknown = '') => {
if (argv.includes('--json') || isDefaultJson()) {
process.stdout.write(JSON.stringify(message, null, 2))
}
}
export const log = (message = '', ...args: string[]) => {
// If --silent or --json flag passed disable logger
if (argv.includes('--json') || argv.includes('--silent') || isDefaultJson()) {
return
}
message = typeof message === 'string' ? message : inspect(message)
process.stdout.write(`${format(message, ...args)}\n`)
}
export const logPadded = (message = '', ...args: string[]) => {
log('')
log(message, ...args)
log('')
}
/**
* logs a warning message
*/
export const warn = (message = '') => {
const bang = chalk.yellow(BANG)
log(` ${bang} Warning: ${message}`)
}
const toError = (val: unknown): Error => {
if (val instanceof Error) return val
if (typeof val === 'string') return new Error(val)
const err = new Error(inspect(val))
err.stack = undefined
return err
}
export const logAndThrowError = (message: unknown): never => {
const err = toError(message)
void reportError(err, { severity: 'error' })
throw err
}
export const logError = (message: unknown): void => {
const err = toError(message)
const bang = chalk.red(BANG)
if (process.env.DEBUG) {
process.stderr.write(` ${bang} Warning: ${err.stack?.split('\n').join(`\n ${bang} `)}\n`)
} else {
process.stderr.write(` ${bang} ${chalk.red(`${err.name}:`)} ${err.message}\n`)
}
}
export const exit = (code = 0): never => {
process.exit(code)
}
/**
* When `build.publish` is not set by the user, the CLI behavior differs in
* several ways. It detects it by checking if `build.publish` is `undefined`.
* However, `@netlify/config` adds a default value to `build.publish`.
* This removes 'publish' and 'publishOrigin' in this case.
* TODO(serhalp): Come up with a better name (or kill it?). This sucks but it's descriptive.
*/
export type NormalizedCachedConfigConfig =
| CachedConfig['config']
| (Omit<CachedConfig['config'], 'build'> & {
build: Omit<CachedConfig['config']['build'], 'publish' | 'publishOrigin'>
})
export const normalizeConfig = (config: CachedConfig['config']): NormalizedCachedConfigConfig => {
// Unused var here is in order to omit 'publish' from build config
const { publish, publishOrigin, ...build } = config.build
return publishOrigin === 'default' ? { ...config, build } : config
}
export const getTerminalLink = (text: string, url: string): string =>
terminalLink(text, url, { fallback: () => `${text} (${url})` })
export const isNodeError = (err: unknown): err is NodeJS.ErrnoException => err instanceof Error
export const nonNullable = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined
export interface APIError extends Error {
status: number
message: string
}
export class GitHubAPIError extends Error {
status: string
constructor(status: string, message: string) {
super(message)
this.status = status
this.name = 'GitHubAPIError'
}
}
export interface GitHubRepoResponse {
status?: string
message?: string
id?: number
name?: string
clone_url?: string
full_name?: string
private?: boolean
default_branch?: string
errors?: string[]
is_template?: boolean
}
export const checkFileForLine = (filename: string, line: string) => {
let filecontent = ''
try {
filecontent = fs.readFileSync(filename, 'utf8')
} catch (error_) {
return logAndThrowError(error_)
}
return !!filecontent.match(line)
}
export const TABTAB_CONFIG_LINE = '[[ -f ~/.config/tabtab/__tabtab.zsh ]] && . ~/.config/tabtab/__tabtab.zsh || true'
export const AUTOLOAD_COMPINIT = 'autoload -U compinit; compinit'
function pkgFromUserAgent(userAgent: string | undefined): string | undefined {
if (!userAgent) return undefined
const pkgSpec = userAgent.split(' ')[0]
const [pkgManagerName] = pkgSpec.split('/')
return pkgManagerName
}
export const netlifyCommand = () => {
const { npm_command, npm_config_user_agent, npm_lifecycle_event } = process.env
// Captures both `npx netlify ...` and `npm exec netlify ...`
if (npm_lifecycle_event === 'npx') {
return `npx netlify`
}
// Captures `pnpm exec netlify ...`
if (pkgFromUserAgent(npm_config_user_agent) === 'pnpm' && npm_command === 'exec') {
return `pnpm exec netlify`
}
// Captures `pnpx netlify ...`
if (pkgFromUserAgent(npm_config_user_agent) === 'pnpm' && ['run-script', 'run'].includes(npm_command ?? '')) {
return `pnpx netlify`
}
// Default
return 'netlify'
}