Skip to content

Commit

Permalink
fix: add --no-stash as hidden option for backwards-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Oct 2, 2021
1 parent ff320fb commit 73db492
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
38 changes: 23 additions & 15 deletions bin/lint-staged.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ require('please-upgrade-node')(
})
)

const cmdline = require('commander')
const { Command, Option } = require('commander')
const debugLib = require('debug')
const lintStaged = require('../lib')
const { CONFIG_STDIN_ERROR } = require('../lib/messages')

const debug = debugLib('lint-staged:bin')

cmdline
.version(pkg.version)
const program = new Command('lint-staged')

program.version(pkg.version)

program
.option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
.option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
.option('-d, --debug', 'print additional debug information', false)
Expand All @@ -48,9 +51,13 @@ cmdline
'show task output even when tasks succeed; by default only failed output is shown',
false
)
.parse(process.argv)

if (cmdline.debug) {
// Added for backwards-compatibility
program.addOption(new Option('--no-stash').hideHelp())

program.parse(process.argv)

if (program.debug) {
debugLib.enable('lint-staged*')
}

Expand All @@ -74,19 +81,20 @@ const getMaxArgLength = () => {
}
}

const cmdlineOptions = cmdline.opts()
const programOpts = program.opts()

const options = {
allowEmpty: !!cmdlineOptions.allowEmpty,
concurrent: JSON.parse(cmdlineOptions.concurrent),
configPath: cmdlineOptions.config,
debug: !!cmdlineOptions.debug,
allowEmpty: !!programOpts.allowEmpty,
concurrent: JSON.parse(programOpts.concurrent),
configPath: programOpts.config,
debug: !!programOpts.debug,
maxArgLength: getMaxArgLength() / 2,
quiet: !!cmdlineOptions.quiet,
relative: !!cmdlineOptions.relative,
reset: !!cmdlineOptions.reset, // commander inverts `no-<x>` flags to `!x`
shell: cmdlineOptions.shell /* Either a boolean or a string pointing to the shell */,
verbose: !!cmdlineOptions.verbose,
quiet: !!programOpts.quiet,
relative: !!programOpts.relative,
reset: !!programOpts.reset, // commander inverts `no-<x>` flags to `!x`
shell: programOpts.shell /* Either a boolean or a string pointing to the shell */,
stash: programOpts.stash /** kept for backwards-compatibility */,
verbose: !!programOpts.verbose,
}

debug('Options parsed from command-line:', options)
Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ const lintStaged = async (
relative = false,
reset = true,
shell = false,
stash, // kept for backwards-compatibility
verbose = false,
} = {},
logger = console
) => {
await validateOptions({ shell }, logger)
await validateOptions({ shell, stash }, logger)

debugLog('Loading config using `cosmiconfig`')

Expand Down Expand Up @@ -121,7 +122,7 @@ const lintStaged = async (
maxArgLength,
quiet,
relative,
reset,
reset: reset || !!stash,
shell,
verbose,
},
Expand Down
6 changes: 6 additions & 0 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const DEPRECATED_GIT_ADD = yellow(
`
)

const DEPRECATED_NO_STASH = yellow(
`${warning} The \`--no-stash\` option has been renamed to \`--no-reset\`.
`
)

const TASK_ERROR = 'Skipped because of errors from tasks.'

const SKIPPED_GIT_ERROR = 'Skipped because of previous git error.'
Expand All @@ -63,6 +68,7 @@ module.exports = {
CONFIG_STDIN_ERROR,
configurationError,
DEPRECATED_GIT_ADD,
DEPRECATED_NO_STASH,
FAILED_GET_STAGED_FILES,
GIT_ERROR,
incorrectBraces,
Expand Down
6 changes: 5 additions & 1 deletion lib/validateOptions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { promises: fs, constants } = require('fs')

const { invalidOption } = require('./messages')
const { DEPRECATED_NO_STASH, invalidOption } = require('./messages')
const { InvalidOptionsError } = require('./symbols')

const debug = require('debug')('lint-staged:options')
Expand All @@ -25,6 +25,10 @@ const validateOptions = async (options = {}, logger) => {
}
}

if (typeof options.stash === 'boolean') {
logger.warn(DEPRECATED_NO_STASH)
}

debug('Validated options!')
}

Expand Down
13 changes: 13 additions & 0 deletions test/validateOptions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ describe('validateOptions', () => {
See https://github.com/okonet/lint-staged#command-line-flags"
`)
})

it('should warn when deprecated stash option is used', async () => {
const logger = makeConsoleMock()

await expect(validateOptions({ stash: false }, logger)).resolves.toBeUndefined()

expect(logger.history()).toHaveLength(1)
expect(logger.printHistory()).toMatchInlineSnapshot(`
"
WARN ⚠ The \`--no-stash\` option has been renamed to \`--no-reset\`.
"
`)
})
})

0 comments on commit 73db492

Please sign in to comment.