Skip to content

Commit

Permalink
fix(core): consistently process args with yargs-parser instead of min…
Browse files Browse the repository at this point in the history
…imist (nrwl#5488)
  • Loading branch information
FrozenPandaz authored Apr 30, 2021
1 parent 1f21e48 commit c7ce8d2
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 24 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@
"mime": "2.4.4",
"mini-css-extract-plugin": "0.8.0",
"minimatch": "3.0.4",
"minimist": "^1.2.5",
"next": "10.1.2",
"ng-packagr": "~11.2.0",
"ngrx-store-freeze": "0.2.4",
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/lib/parse-run-one-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import yargsParser = require('yargs-parser');
import * as yargsParser from 'yargs-parser';

function calculateDefaultProjectName(cwd: string, root: string, wc: any) {
let relativeCwd = cwd.replace(/\\/g, '/').split(root.replace(/\\/g, '/'))[1];
Expand Down Expand Up @@ -70,6 +70,9 @@ export function parseRunOneOptions(
alias: {
c: 'configuration',
},
configuration: {
'strip-dashed': true,
},
});

if (parsedArgs['help']) {
Expand Down
1 change: 0 additions & 1 deletion packages/tao/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"chalk": "4.1.0",
"enquirer": "~2.3.6",
"fs-extra": "^9.1.0",
"minimist": "^1.2.5",
"rxjs": "^6.5.4",
"rxjs-for-await": "0.0.2",
"semver": "7.3.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/tao/src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as minimist from 'minimist';
import * as yargsParser from 'yargs-parser';
import {
combineOptionsForGenerator,
convertToCamelCase,
Expand Down Expand Up @@ -38,7 +38,7 @@ function parseGenerateOpts(
defaultCollection: string | null
): GenerateOptions {
const generatorOptions = convertToCamelCase(
minimist(args, {
yargsParser(args, {
boolean: ['help', 'dryRun', 'debug', 'force', 'interactive', 'defaults'],
alias: {
dryRun: 'dry-run',
Expand Down
4 changes: 2 additions & 2 deletions packages/tao/src/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execSync } from 'child_process';
import { readFileSync, writeFileSync, removeSync } from 'fs-extra';
import * as minimist from 'minimist';
import * as yargsParser from 'yargs-parser';
import { dirname, join } from 'path';
import { gt, lte } from 'semver';
import * as stripJsonComments from 'strip-json-comments';
Expand Down Expand Up @@ -367,7 +367,7 @@ export function parseMigrationsOptions(
args: string[]
): GenerateMigrations | RunMigrations {
const options = convertToCamelCase(
minimist(args, {
yargsParser(args, {
string: ['runMigrations', 'from', 'to'],
alias: {
runMigrations: 'run-migrations',
Expand Down
4 changes: 2 additions & 2 deletions packages/tao/src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as minimist from 'minimist';
import * as yargsParser from 'yargs-parser';
import {
combineOptionsForExecutor,
convertToCamelCase,
Expand Down Expand Up @@ -42,7 +42,7 @@ function parseRunOpts(
defaultProjectName: string | null
): RunOptions {
const runOptions = convertToCamelCase(
minimist(args, {
yargsParser(args, {
boolean: ['help', 'prod'],
string: ['configuration', 'project'],
alias: {
Expand Down
32 changes: 19 additions & 13 deletions packages/tao/src/shared/params.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParsedArgs } from 'minimist';
import * as yargsParser from 'yargs-parser';
import {
coerceTypesInOptions,
convertAliases,
Expand Down Expand Up @@ -137,33 +137,39 @@ describe('params', () => {
describe('convertToCamelCase', () => {
it('should convert dash case to camel case', () => {
expect(
convertToCamelCase({
_: undefined,
'one-two': 1,
} as ParsedArgs)
convertToCamelCase(
yargsParser(['--one-two', '1'], {
number: ['oneTwo'],
})
)
).toEqual({
_: [],
oneTwo: 1,
});
});

it('should not convert camel case', () => {
expect(
convertToCamelCase({
_: undefined,
oneTwo: 1,
})
convertToCamelCase(
yargsParser(['--oneTwo', '1'], {
number: ['oneTwo'],
})
)
).toEqual({
_: [],
oneTwo: 1,
});
});

it('should handle mixed case', () => {
expect(
convertToCamelCase({
_: undefined,
'one-Two': 1,
})
convertToCamelCase(
yargsParser(['--one-Two', '1'], {
number: ['oneTwo'],
})
)
).toEqual({
_: [],
oneTwo: 1,
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/tao/src/shared/params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParsedArgs } from 'minimist';
import type { Arguments } from 'yargs-parser';
import { TargetConfiguration, WorkspaceJsonConfiguration } from './workspace';
import { logger } from './logger';

Expand Down Expand Up @@ -67,7 +67,7 @@ function camelCase(input: string): string {
}
}

export function convertToCamelCase(parsed: ParsedArgs): Options {
export function convertToCamelCase(parsed: Arguments): Options {
return Object.keys(parsed).reduce(
(m, c) => ({ ...m, [camelCase(c)]: parsed[c] }),
{}
Expand Down
3 changes: 3 additions & 0 deletions packages/workspace/src/command-line/nx-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const noop = (yargs: yargs.Argv): yargs.Argv => yargs;
* be executed correctly.
*/
export const commandsObject = yargs
.parserConfiguration({
'strip-dashed': true,
})
.usage('Powerful, Extensible Dev Tools')
.command(
'run [project][:target][:configuration] [options, ...]',
Expand Down

0 comments on commit c7ce8d2

Please sign in to comment.