-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use util.parseArgs
instead of yargs
#380
base: main
Are you sure you want to change the base?
Changes from 7 commits
ec2397e
8d04f7c
24cbc2e
1d8560a
7427b24
06dbd1a
81d6f59
74bc7b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { join, relative } from 'node:path'; | ||
import yargs from 'yargs'; | ||
import { parseArgs } from 'node:util'; | ||
import { getCacheDir } from '../util/cache.js'; | ||
import { DeepPartial } from '../util/type-check.js'; | ||
import { VERSION } from './package.js'; | ||
|
@@ -29,90 +29,81 @@ | |
|
||
/** Parse CLI options */ | ||
export function parseArgv(argv: string[]): ParsedCLIOptions { | ||
const parsedArgv = yargs(argv.slice(2)) | ||
.wrap(Math.min(140, process.stdout.columns)) | ||
.scriptName('eslint-interactive') | ||
.version(VERSION) | ||
.usage('$0 [file.js] [dir]') | ||
.detectLocale(false) | ||
// NOTE: yargs doesn't support negative only option. So we use `--eslintrc` instead of `--no-eslintrc`. | ||
.option('eslintrc', { | ||
type: 'boolean', | ||
describe: 'Enable use of configuration from .eslintrc.*', | ||
default: cliOptionsDefaults.useEslintrc, | ||
}) | ||
.option('config', { | ||
alias: 'c', | ||
type: 'string', | ||
describe: 'Use this configuration, overriding .eslintrc.* config options if present', | ||
}) | ||
.option('ext', { | ||
type: 'array', | ||
describe: 'Specify JavaScript file extensions', | ||
}) | ||
.nargs('ext', 1) | ||
.option('resolve-plugins-relative-to', { | ||
type: 'string', | ||
describe: 'A folder where plugins should be resolved from, CWD by default', | ||
}) | ||
.option('rulesdir', { | ||
type: 'array', | ||
describe: 'Use additional rules from this directory', | ||
}) | ||
.nargs('rulesdir', 1) | ||
// Following ESLint, --ignore-path accepts only one path. However, this limitation may be relaxed in the future. | ||
// ref: https://github.com/eslint/eslint/issues/9794 | ||
.option('ignore-path', { | ||
type: 'string', | ||
describe: 'Specify path of ignore file', | ||
}) | ||
.option('format', { | ||
type: 'string', | ||
describe: 'Specify the format to be used for the `Display problem messages` action', | ||
default: cliOptionsDefaults.formatterName, | ||
}) | ||
.option('quiet', { | ||
type: 'boolean', | ||
describe: 'Report errors only', | ||
default: cliOptionsDefaults.quiet, | ||
}) | ||
.option('cache', { | ||
type: 'boolean', | ||
describe: 'Only check changed files', | ||
default: cliOptionsDefaults.cache, | ||
}) | ||
.option('cache-location', { | ||
type: 'string', | ||
describe: `Path to the cache file or directory`, | ||
default: cliOptionsDefaults.cacheLocation, | ||
}) | ||
.example('$0 ./src', 'Lint ./src/ directory') | ||
.example('$0 ./src ./test', 'Lint multiple directories') | ||
.example("$0 './src/**/*.{ts,tsx,vue}'", 'Lint with glob pattern') | ||
.example('$0 ./src --ext .ts,.tsx,.vue', 'Lint with custom extensions') | ||
.example('$0 ./src --rulesdir ./rules', 'Lint with custom rules') | ||
.example('$0 ./src --no-eslintrc --config ./.eslintrc.ci.js', 'Lint with custom config') | ||
.parseSync(); | ||
// NOTE: convert `string` type because yargs convert `'10'` (`string` type) into `10` (`number` type) | ||
// and `lintFiles` only accepts `string[]`. | ||
const patterns = parsedArgv._.map((pattern) => pattern.toString()); | ||
const rulePaths = parsedArgv.rulesdir?.map((rulePath) => rulePath.toString()); | ||
const extensions = parsedArgv.ext | ||
?.map((extension) => extension.toString()) | ||
// map '.js,.ts' into ['.js', '.ts'] | ||
.flatMap((extension) => extension.split(',')); | ||
const formatterName = parsedArgv.format; | ||
const options = { | ||
'eslintrc': { type: 'boolean', default: cliOptionsDefaults.useEslintrc }, | ||
'config': { type: 'string', short: 'c' }, | ||
'ext': { type: 'string', multiple: true }, | ||
'resolve-plugins-relative-to': { type: 'string' }, | ||
'rulesdir': { type: 'string', multiple: true }, | ||
'ignore-path': { type: 'string' }, | ||
'format': { type: 'string', default: cliOptionsDefaults.formatterName }, | ||
'quiet': { type: 'boolean', default: cliOptionsDefaults.quiet }, | ||
'cache': { type: 'boolean', default: cliOptionsDefaults.cache }, | ||
'cache-location': { type: 'string', default: cliOptionsDefaults.cacheLocation }, | ||
'version': { type: 'boolean' }, | ||
'help': { type: 'boolean' }, | ||
} as const; | ||
|
||
const { values, positionals } = parseArgs({ | ||
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, ubuntu-latest)src/cli/parse-argv.test.ts > parseArgv > --no-eslintrc
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, ubuntu-latest)src/cli/parse-argv.test.ts > parseArgv > --quiet
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, ubuntu-latest)src/cli/parse-argv.test.ts > parseArgv > --cache
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, macos-latest)src/cli/parse-argv.test.ts > parseArgv > --no-eslintrc
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, macos-latest)src/cli/parse-argv.test.ts > parseArgv > --quiet
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, macos-latest)src/cli/parse-argv.test.ts > parseArgv > --cache
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, windows-latest)src/cli/parse-argv.test.ts > parseArgv > --no-eslintrc
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, windows-latest)src/cli/parse-argv.test.ts > parseArgv > --quiet
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 8, windows-latest)src/cli/parse-argv.test.ts > parseArgv > --cache
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, ubuntu-latest)src/cli/parse-argv.test.ts > parseArgv > --no-eslintrc
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, ubuntu-latest)src/cli/parse-argv.test.ts > parseArgv > --quiet
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, ubuntu-latest)src/cli/parse-argv.test.ts > parseArgv > --cache
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, macos-latest)src/cli/parse-argv.test.ts > parseArgv > --no-eslintrc
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, macos-latest)src/cli/parse-argv.test.ts > parseArgv > --quiet
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, macos-latest)src/cli/parse-argv.test.ts > parseArgv > --cache
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, windows-latest)src/cli/parse-argv.test.ts > parseArgv > --no-eslintrc
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, windows-latest)src/cli/parse-argv.test.ts > parseArgv > --quiet
Check failure on line 47 in src/cli/parse-argv.ts GitHub Actions / test (18, 9, windows-latest)src/cli/parse-argv.test.ts > parseArgv > --cache
|
||
allowPositionals: true, | ||
allowNegative: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
strict: true, | ||
args: argv.slice(2), | ||
options, | ||
}); | ||
|
||
if (values.version) { | ||
console.log(VERSION); | ||
// eslint-disable-next-line n/no-process-exit | ||
process.exit(0); | ||
} | ||
|
||
if (values.help) { | ||
console.log(` | ||
eslint-interactive [file.js] [dir] | ||
|
||
Options: | ||
--help Show help [boolean] | ||
--version Show version number [boolean] | ||
--eslintrc Enable use of configuration from .eslintrc.* [boolean] [default: true] | ||
-c, --config Use this configuration, overriding .eslintrc.* config options if present [string] | ||
--resolve-plugins-relative-to A folder where plugins should be resolved from, CWD by default [string] | ||
--ext Specify JavaScript file extensions [array] | ||
--rulesdir Use additional rules from this directory [array] | ||
--ignore-path Specify path of ignore file [string] | ||
--format Specify the format to be used for the \`Display problem messages\` action [string] [default: "codeframe"] | ||
--quiet Report errors only [boolean] [default: false] | ||
--cache Only check changed files [boolean] [default: true] | ||
--cache-location Path to the cache file or directory | ||
|
||
Examples: | ||
eslint-interactive ./src Lint ./src/ directory | ||
eslint-interactive ./src ./test Lint multiple directories | ||
eslint-interactive './src/**/*.{ts,tsx,vue}' Lint with glob pattern | ||
eslint-interactive ./src --ext .ts,.tsx,.vue Lint with custom extensions | ||
eslint-interactive ./src --rulesdir ./rules Lint with custom rules | ||
eslint-interactive ./src --no-eslintrc --config ./.eslintrc.ci.js Lint with custom config | ||
`); | ||
// eslint-disable-next-line n/no-process-exit | ||
process.exit(0); | ||
} | ||
|
||
const patterns = positionals.map((pattern) => pattern.toString()); | ||
const rulePaths = values.rulesdir?.map((rulePath) => rulePath.toString()); | ||
const extensions = values.ext?.map((extension) => extension.toString()).flatMap((extension) => extension.split(',')); | ||
const formatterName = values.format; | ||
|
||
return { | ||
patterns, | ||
formatterName, | ||
quiet: parsedArgv.quiet, | ||
useEslintrc: parsedArgv.eslintrc, | ||
overrideConfigFile: parsedArgv.config, | ||
quiet: values.quiet, | ||
useEslintrc: values.eslintrc, | ||
overrideConfigFile: values.config, | ||
extensions, | ||
rulePaths, | ||
ignorePath: parsedArgv.ignorePath, | ||
cache: parsedArgv.cache, | ||
cacheLocation: parsedArgv['cache-location'], | ||
resolvePluginsRelativeTo: parsedArgv['resolve-plugins-relative-to'], | ||
ignorePath: values['ignore-path'], | ||
cache: values.cache, | ||
cacheLocation: values['cache-location'], | ||
resolvePluginsRelativeTo: values['resolve-plugins-relative-to'], | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result: 8d04f7c