Skip to content

Commit c59a8c0

Browse files
committed
feat: add kebab-case options to cli flags - first example
1 parent 83510e4 commit c59a8c0

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/commands/addons/addons.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const createAddonsCommand = (program) => {
3030
.command('addons')
3131
.alias('addon')
3232
.description('(Beta) Manage Netlify Add-ons')
33-
.noHelpOptions()
3433
.addExamples([
3534
'netlify addons:create addon-xyz',
3635
'netlify addons:list',

src/commands/base-command.mjs

+20-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import {
2525
} from '../utils/command-helpers.mjs'
2626
import getGlobalConfig from '../utils/get-global-config.mjs'
2727
import openBrowser from '../utils/open-browser.mjs'
28+
import {
29+
warnForDeprecatedOptions,
30+
deprecatedArgParser,
31+
returnDeprecatedOptionValue,
32+
} from '../utils/option-deprecation.mjs'
2833
import StateConfig from '../utils/state-config.mjs'
2934
import { identify, track } from '../utils/telemetry/index.mjs'
3035

@@ -99,13 +104,20 @@ export default class BaseCommand extends Command {
99104
.addOption(new Option('--cwd <cwd>').hideHelp(true))
100105
.addOption(new Option('-o, --offline').hideHelp(true))
101106
.addOption(new Option('--auth <token>', 'Netlify auth token').hideHelp(true))
107+
// TODO v13: remove this deprecated command
108+
.addOption(
109+
new Option('--httpProxy [address]', '[DEPRECATED IN V13] Proxy server address to route requests through.')
110+
.default(process.env.HTTP_PROXY || process.env.HTTPS_PROXY)
111+
.hideHelp(true)
112+
.argParser(deprecatedArgParser),
113+
)
102114
.option(
103115
'--httpProxyCertificateFilename [file]',
104116
'Certificate file to use when connecting using a proxy server',
105117
process.env.NETLIFY_PROXY_CERTIFICATE_FILENAME,
106118
)
107119
.option(
108-
'--httpProxy [address]',
120+
'--http-proxy [address]',
109121
'Proxy server address to route requests through.',
110122
process.env.HTTP_PROXY || process.env.HTTPS_PROXY,
111123
)
@@ -117,6 +129,11 @@ export default class BaseCommand extends Command {
117129
await this.init(actionCommand)
118130
debug(`${name}:preAction`)('end')
119131
})
132+
.hook('preAction', (parentCommand) => {
133+
const options = parentCommand.opts()
134+
if (!options) return
135+
warnForDeprecatedOptions(options)
136+
})
120137
)
121138
}
122139

@@ -419,8 +436,9 @@ export default class BaseCommand extends Command {
419436
const cachedConfig = await actionCommand.getConfig({ cwd, state, token, ...apiUrlOpts })
420437
const { buildDir, config, configPath, repositoryRoot, siteInfo } = cachedConfig
421438
const normalizedConfig = normalizeConfig(config)
439+
422440
const agent = await getAgent({
423-
httpProxy: options.httpProxy,
441+
httpProxy: returnDeprecatedOptionValue(options.httpProxy),
424442
certificateFile: options.httpProxyCertificateFilename,
425443
})
426444
const apiOpts = { ...apiUrlOpts, agent }

src/utils/option-deprecation.mjs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { chalk, log, NETLIFYDEVWARN } from './command-helpers.mjs'
2+
3+
// This file contains logic for flags in camelcase that will be deprecated in v13 and replaced in kebab-case.
4+
// Flags to be deprecated in v13
5+
const DEPRECATED_OPTIONS = {
6+
httpProxy: { option: '--httpProxy', newOption: '--http-proxy', deprecatedVersion: 'v13' },
7+
}
8+
9+
export const deprecatedArgParser = (value) => ({
10+
value,
11+
deprecationWarn: true,
12+
})
13+
14+
// TODO v13: remove all uses of this function in the code so they can fall back on just using `option` instead of `option.value`
15+
export const returnDeprecatedOptionValue = (option) => (option.value ? option.value : option)
16+
17+
export const warnForDeprecatedOptions = (options) => {
18+
Object.entries(options).forEach(([option, value]) => {
19+
const deprecatedOption = DEPRECATED_OPTIONS[option]
20+
21+
if (value.deprecationWarn && deprecatedOption) {
22+
log(
23+
NETLIFYDEVWARN,
24+
chalk.yellowBright(
25+
`The flag ${deprecatedOption.option} is replaced by ${deprecatedOption.newOption} and will be deprecated in ${deprecatedOption.deprecatedVersion}`,
26+
),
27+
)
28+
}
29+
})
30+
}

0 commit comments

Comments
 (0)