Skip to content
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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 27 additions & 81 deletions src/cli/parse-argv.ts
Copy link
Owner Author

@mizdra mizdra Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

result: 8d04f7c

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';
Expand Down Expand Up @@ -29,90 +29,36 @@ export const cliOptionsDefaults = {

/** 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' },
'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 },
};
const { values, positionals } = parseArgs({ args: argv.slice(2), options });

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'],
};
}