Skip to content

Commit 3513229

Browse files
committed
fix(deps): replace chalk with tiny ansis
Oops and I got carried away and fixed a lot of types along the way.
1 parent b7c0f0f commit 3513229

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+415
-423
lines changed

eslint.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export default tseslint.config(
7979
},
8080
},
8181
{
82+
files: ['{src,tests}/**/*'],
8283
rules: {
8384
'no-restricted-imports': [
8485
'error',
@@ -96,9 +97,9 @@ export default tseslint.config(
9697
},
9798

9899
{
99-
name: 'chalk',
100+
name: 'ansis',
100101
message:
101-
'Use the safe chalk import that handles colors for json output: `import { chalk } from "src/utils/command-helpers.js"`',
102+
'Use the configured ansis import that handles colors for json output: `import { ansis } from "src/utils/command-helpers.js"`',
102103
},
103104
],
104105
},

package-lock.json

+15-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@
7474
"@pnpm/tabtab": "0.5.4",
7575
"ansi-escapes": "7.0.0",
7676
"ansi-to-html": "0.7.2",
77+
"ansis": "^3.17.0",
7778
"ascii-table": "0.0.9",
7879
"backoff": "2.5.0",
7980
"boxen": "8.0.1",
80-
"chalk": "5.4.1",
8181
"chokidar": "3.6.0",
8282
"ci-info": "4.1.0",
8383
"clean-deep": "3.4.0",

scripts/postinstall.js

+7-33
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,10 @@ import fs from 'node:fs/promises'
77
import path from 'node:path'
88
import { fileURLToPath } from 'node:url'
99
import process from 'node:process'
10-
// eslint-disable-next-line no-restricted-imports
11-
import chalk from 'chalk'
1210

13-
const __dirname = path.dirname(fileURLToPath(import.meta.url))
14-
15-
const identity = (message) => message
11+
import ansis from 'ansis'
1612

17-
/**
18-
*
19-
* @param {string} message
20-
* @param {Array<chalk['Color'] | chalk['Modifiers']>} styles
21-
* @returns
22-
*/
23-
const format = (message, styles) => {
24-
let func = identity
25-
try {
26-
func = chalk
27-
styles.forEach((style) => {
28-
func = func[style]
29-
})
30-
} catch {}
31-
return func(message)
32-
}
13+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
3314

3415
const postInstall = async () => {
3516
const { createMainCommand } = await import('../dist/commands/index.js')
@@ -45,24 +26,17 @@ const postInstall = async () => {
4526
}
4627

4728
console.log('')
48-
console.log(await format('Success! Netlify CLI has been installed!', ['greenBright', 'bold', 'underline']))
29+
console.log(ansis.greenBright.bold.underline('Success! Netlify CLI has been installed!'))
4930
console.log('')
5031
console.log('Your device is now configured to use Netlify CLI to deploy and manage your Netlify sites.')
5132
console.log('')
5233
console.log('Next steps:')
5334
console.log('')
54-
console.log(
55-
` ${await format('netlify init', [
56-
'cyanBright',
57-
'bold',
58-
])} Connect or create a Netlify site from current directory`,
59-
)
60-
console.log(
61-
` ${await format('netlify deploy', ['cyanBright', 'bold'])} Deploy the latest changes to your Netlify site`,
62-
)
35+
console.log(` ${ansis.cyanBright.bold('netlify init')} Connect or create a Netlify site from current directory`)
36+
console.log(` ${ansis.cyan.bold('netlify deploy')} Deploy the latest changes to your Netlify site`)
6337
console.log('')
64-
console.log(`For more information on the CLI run ${await format('netlify help', ['cyanBright', 'bold'])}`)
65-
console.log(`Or visit the docs at ${await format('https://cli.netlify.com', ['cyanBright', 'bold'])}`)
38+
console.log(`For more information on the CLI run ${ansis.cyanBright.bold('netlify help')}`)
39+
console.log(`Or visit the docs at ${ansis.cyanBright.bold('https://cli.netlify.com')}`)
6640
console.log('')
6741
}
6842

scripts/prepublishOnly.js

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const main = async () => {
1010
//
1111
// Leaving development dependencies makes the CLI installation significantly larger and increases
1212
// the risk of platform-specific dependency installation issues.
13-
// eslint-disable-next-line no-restricted-properties
1413
const packageJSONPath = path.join(process.cwd(), 'package.json')
1514
const rawPackageJSON = await fs.readFile(packageJSONPath, 'utf8')
1615

site/scripts/util/generate-command-data.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { createMainCommand } from '../../../src/commands/index.js'
2-
import { sortOptions } from '../../../src/utils/command-helpers.js'
2+
import { compareOptions } from '../../../src/utils/command-helpers.js'
33

44
const program = createMainCommand()
55

6-
/** @type {Array<import('../../../src/commands/base-command.js').default>} */
7-
// @ts-ignore typecast needed
8-
const commands = program.commands.sort((cmdA, cmdB) => cmdA.name().localeCompare(cmdB.name()))
6+
const commands = [...program.commands].sort((cmdA, cmdB) => cmdA.name().localeCompare(cmdB.name()))
97

108
/**
119
*
@@ -19,7 +17,7 @@ const parseCommand = function (command) {
1917

2018
const flags = command.options
2119
.filter((option) => !option.hidden)
22-
.sort(sortOptions)
20+
.sort(compareOptions)
2321
.reduce((prev, cur) => {
2422
const name = cur.long.replace('--', '')
2523
const contentType = cur.argChoices ? cur.argChoices.join(' | ') : 'string'

src/commands/api/api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import AsciiTable from 'ascii-table'
22
import type { OptionValues } from 'commander'
33
import { methods, type NetlifyAPI } from 'netlify'
44

5-
import { chalk, logAndThrowError, exit, log, logJson } from '../../utils/command-helpers.js'
5+
import { ansis, logAndThrowError, exit, log, logJson } from '../../utils/command-helpers.js'
66
import type BaseCommand from '../base-command.js'
77

88
type ApiMethodName = keyof NetlifyAPI
@@ -23,7 +23,7 @@ export const apiCommand = async (apiMethodName: string, options: OptionValues, c
2323
log(table.toString())
2424
log()
2525
log('Above is a list of available API methods')
26-
log(`To run a method use "${chalk.cyanBright('netlify api methodName')}"`)
26+
log(`To run a method use "${ansis.cyanBright('netlify api methodName')}"`)
2727
exit()
2828
}
2929

src/commands/api/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chalk } from '../../utils/command-helpers.js'
1+
import { ansis } from '../../utils/command-helpers.js'
22
import BaseCommand from '../base-command.js'
33

44
export const createApiCommand = (program: BaseCommand) =>
@@ -7,7 +7,7 @@ export const createApiCommand = (program: BaseCommand) =>
77
.argument('[apiMethod]', 'Open API method to run')
88
.description(
99
`Run any Netlify API method
10-
For more information on available methods checkout https://open-api.netlify.com/ or run '${chalk.grey(
10+
For more information on available methods checkout https://open-api.netlify.com/ or run '${ansis.gray(
1111
'netlify api --list',
1212
)}'`,
1313
)

src/commands/base-command.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { getAgent } from '../lib/http-agent.js'
1919
import {
2020
NETLIFY_CYAN,
2121
USER_AGENT,
22-
chalk,
22+
ansis,
2323
logAndThrowError,
2424
exit,
2525
getToken,
@@ -28,7 +28,7 @@ import {
2828
normalizeConfig,
2929
padLeft,
3030
pollForToken,
31-
sortOptions,
31+
compareOptions,
3232
warn,
3333
logError,
3434
} from '../utils/command-helpers.js'
@@ -107,7 +107,7 @@ async function selectWorkspace(project: Project, filter?: string): Promise<strin
107107

108108
if (!selected) {
109109
log()
110-
log(chalk.cyan(`We've detected multiple sites inside your repository`))
110+
log(ansis.cyan(`We've detected multiple sites inside your repository`))
111111

112112
if (isCI) {
113113
throw new Error(
@@ -129,7 +129,7 @@ async function selectWorkspace(project: Project, filter?: string): Promise<strin
129129
(project.workspace?.packages || [])
130130
.filter((pkg) => pkg.path.includes(input))
131131
.map((pkg) => ({
132-
name: `${pkg.name ? `${chalk.bold(pkg.name)} ` : ''}${pkg.path} ${chalk.dim(
132+
name: `${pkg.name ? `${ansis.bold(pkg.name)} ` : ''}${pkg.path} ${ansis.dim(
133133
`--filter ${pkg.name || pkg.path}`,
134134
)}`,
135135
value: pkg.path,
@@ -286,7 +286,7 @@ export default class BaseCommand extends Command {
286286

287287
if (description) {
288288
const pad = termWidth + HELP_SEPARATOR_WIDTH
289-
const fullText = `${bang}${term.padEnd(pad - (isCommand ? 2 : 0))}${chalk.grey(description)}`
289+
const fullText = `${bang}${term.padEnd(pad - (isCommand ? 2 : 0))}${ansis.gray(description)}`
290290
return helper.wrap(fullText, helpWidth - HELP_INDENT_WIDTH, pad)
291291
}
292292

@@ -303,34 +303,34 @@ export default class BaseCommand extends Command {
303303

304304
// on the parent help command the version should be displayed
305305
if (this.name() === 'netlify') {
306-
output = [...output, chalk.bold('VERSION'), formatHelpList([formatItem(USER_AGENT)]), '']
306+
output = [...output, ansis.bold('VERSION'), formatHelpList([formatItem(USER_AGENT)]), '']
307307
}
308308

309309
// Usage
310-
output = [...output, chalk.bold('USAGE'), helper.commandUsage(command), '']
310+
output = [...output, ansis.bold('USAGE'), helper.commandUsage(command), '']
311311

312312
// Arguments
313313
const argumentList = helper
314314
.visibleArguments(command)
315315
.map((argument) => formatItem(helper.argumentTerm(argument), helper.argumentDescription(argument)))
316316
if (argumentList.length !== 0) {
317-
output = [...output, chalk.bold('ARGUMENTS'), formatHelpList(argumentList), '']
317+
output = [...output, ansis.bold('ARGUMENTS'), formatHelpList(argumentList), '']
318318
}
319319

320320
if (command.#noBaseOptions === false) {
321321
// Options
322322
const optionList = helper
323323
.visibleOptions(command)
324-
.sort(sortOptions)
324+
.sort(compareOptions)
325325
.map((option) => formatItem(helper.optionTerm(option), helper.optionDescription(option)))
326326
if (optionList.length !== 0) {
327-
output = [...output, chalk.bold('OPTIONS'), formatHelpList(optionList), '']
327+
output = [...output, ansis.bold('OPTIONS'), formatHelpList(optionList), '']
328328
}
329329
}
330330

331331
// Description
332332
if (commandDescription.length !== 0) {
333-
output = [...output, chalk.bold('DESCRIPTION'), formatHelpList(commandDescription), '']
333+
output = [...output, ansis.bold('DESCRIPTION'), formatHelpList(commandDescription), '']
334334
}
335335

336336
// Aliases
@@ -339,13 +339,13 @@ export default class BaseCommand extends Command {
339339
if (command._aliases.length !== 0) {
340340
// @ts-expect-error TS(2551) FIXME: Property '_aliases' does not exist on type 'Comman... Remove this comment to see the full error message
341341
const aliases = command._aliases.map((alias) => formatItem(`${parentCommand.name()} ${alias}`, null, true))
342-
output = [...output, chalk.bold('ALIASES'), formatHelpList(aliases), '']
342+
output = [...output, ansis.bold('ALIASES'), formatHelpList(aliases), '']
343343
}
344344

345345
if (command.examples.length !== 0) {
346346
output = [
347347
...output,
348-
chalk.bold('EXAMPLES'),
348+
ansis.bold('EXAMPLES'),
349349
formatHelpList(command.examples.map((example) => `${HELP_$} ${example}`)),
350350
'',
351351
]
@@ -355,7 +355,7 @@ export default class BaseCommand extends Command {
355355
formatItem(cmd.name(), helper.subcommandDescription(cmd).split('\n')[0], true),
356356
)
357357
if (commandList.length !== 0) {
358-
output = [...output, chalk.bold('COMMANDS'), formatHelpList(commandList), '']
358+
output = [...output, ansis.bold('COMMANDS'), formatHelpList(commandList), '']
359359
}
360360

361361
return [...output, ''].join('\n')
@@ -450,11 +450,11 @@ export default class BaseCommand extends Command {
450450

451451
// Log success
452452
log()
453-
log(chalk.greenBright('You are now logged into your Netlify account!'))
453+
log(ansis.greenBright('You are now logged into your Netlify account!'))
454454
log()
455-
log(`Run ${chalk.cyanBright('netlify status')} for account details`)
455+
log(`Run ${ansis.cyanBright('netlify status')} for account details`)
456456
log()
457-
log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`)
457+
log(`To see all available commands run: ${ansis.cyanBright('netlify help')}`)
458458
log()
459459
return accessToken
460460
}

src/commands/blobs/blobs-delete.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getStore } from '@netlify/blobs'
22

3-
import { chalk, logAndThrowError, log } from '../../utils/command-helpers.js'
3+
import { ansis, logAndThrowError, log } from '../../utils/command-helpers.js'
44
import { promptBlobDelete } from '../../utils/prompts/blob-delete-prompts.js'
55

66
/**
@@ -24,8 +24,8 @@ export const blobsDelete = async (storeName: string, key: string, _options: Reco
2424
try {
2525
await store.delete(key)
2626

27-
log(`${chalk.greenBright('Success')}: Blob ${chalk.yellow(key)} deleted from store ${chalk.yellow(storeName)}`)
27+
log(`${ansis.greenBright('Success')}: Blob ${ansis.yellow(key)} deleted from store ${ansis.yellow(storeName)}`)
2828
} catch {
29-
return logAndThrowError(`Could not delete blob ${chalk.yellow(key)} from store ${chalk.yellow(storeName)}`)
29+
return logAndThrowError(`Could not delete blob ${ansis.yellow(key)} from store ${ansis.yellow(storeName)}`)
3030
}
3131
}

src/commands/blobs/blobs-get.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { resolve } from 'path'
44
import { getStore } from '@netlify/blobs'
55
import { OptionValues } from 'commander'
66

7-
import { chalk, logAndThrowError } from '../../utils/command-helpers.js'
7+
import { ansis, logAndThrowError } from '../../utils/command-helpers.js'
88
import BaseCommand from '../base-command.js'
99

1010
interface Options extends OptionValues {
@@ -21,16 +21,16 @@ export const blobsGet = async (storeName: string, key: string, options: Options,
2121
token: api.accessToken ?? '',
2222
})
2323

24-
let blob
24+
let blob: undefined | Awaited<ReturnType<typeof store.get>>
2525

2626
try {
2727
blob = await store.get(key)
2828
} catch {
29-
return logAndThrowError(`Could not retrieve blob ${chalk.yellow(key)} from store ${chalk.yellow(storeName)}`)
29+
return logAndThrowError(`Could not retrieve blob ${ansis.yellow(key)} from store ${ansis.yellow(storeName)}`)
3030
}
3131

3232
if (blob === null) {
33-
return logAndThrowError(`Blob ${chalk.yellow(key)} does not exist in store ${chalk.yellow(storeName)}`)
33+
return logAndThrowError(`Blob ${ansis.yellow(key)} does not exist in store ${ansis.yellow(storeName)}`)
3434
}
3535

3636
if (output) {

src/commands/blobs/blobs-list.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getStore } from '@netlify/blobs'
22
import AsciiTable from 'ascii-table'
33
import { OptionValues } from 'commander'
44

5-
import { chalk, logAndThrowError, log, logJson } from '../../utils/command-helpers.js'
5+
import { ansis, logAndThrowError, log, logJson } from '../../utils/command-helpers.js'
66
import BaseCommand from '../base-command.js'
77

88
interface Options extends OptionValues {
@@ -32,7 +32,7 @@ export const blobsList = async (storeName: string, options: Options, command: Ba
3232
}
3333

3434
if (blobs.length === 0 && directories.length === 0) {
35-
log(`Netlify Blobs store ${chalk.yellow(storeName)} is empty`)
35+
log(`Netlify Blobs store ${ansis.yellow(storeName)} is empty`)
3636
return
3737
}
3838

@@ -50,6 +50,6 @@ export const blobsList = async (storeName: string, options: Options, command: Ba
5050

5151
log(table.toString())
5252
} catch {
53-
return logAndThrowError(`Could not list blobs from store ${chalk.yellow(storeName)}`)
53+
return logAndThrowError(`Could not list blobs from store ${ansis.yellow(storeName)}`)
5454
}
5555
}

0 commit comments

Comments
 (0)