Skip to content

Commit

Permalink
fix(core): add quotes around string to command
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed May 1, 2024
1 parent abd80cf commit f7a073f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
25 changes: 25 additions & 0 deletions packages/nx/bin/init-local.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { commandsObject } from '../src/command-line/nx-commands';
import { initLocal } from './init-local';

describe('initLocal', () => {
it('should call commandsObject.parse with args wrapped in quotes if value contains space', () => {
const spy = jest.spyOn(commandsObject, 'parse');
process.argv = ['nx', 'g', 'app', '--name=my app'];
initLocal({ type: 'nx', dir: 'root' });
expect(spy).toHaveBeenCalledWith(['app', '--name="my app"']);
});

it('should call commandsObject.parse with args wrapped in quotes if it is a string with space', () => {
const spy = jest.spyOn(commandsObject, 'parse');
process.argv = ['nx', 'g', 'app', 'random string'];
initLocal({ type: 'nx', dir: 'root' });
expect(spy).toHaveBeenCalledWith(['app', '"random string"']);
});

it('should call commandsObject.parse with args not wrapped in quotes if it is one word', () => {
const spy = jest.spyOn(commandsObject, 'parse');
process.argv = ['nx', 'g', 'app', '--name=app'];
initLocal({ type: 'nx', dir: 'root' });
expect(spy).toHaveBeenCalledWith(['app', '--name=app']);
});
});
22 changes: 17 additions & 5 deletions packages/nx/bin/init-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ export function initLocal(workspace: WorkspaceTypeAndRoot) {

const command = process.argv[2];
if (command === 'run' || command === 'g' || command === 'generate') {
commandsObject.parse(process.argv.slice(2));
commandsObject.parse(wrapArgsIntoQuotesIfNeeded(process.argv.slice(2)));
} else if (isKnownCommand(command)) {
const newArgs = rewriteTargetsAndProjects(process.argv);
const help = newArgs.indexOf('--help');
const split = newArgs.indexOf('--');
if (help > -1 && (split === -1 || split > help)) {
commandsObject.showHelp();
} else {
commandsObject.parse(newArgs);
commandsObject.parse(wrapArgsIntoQuotesIfNeeded(newArgs));
}
} else {
commandsObject.parse(process.argv.slice(2));
commandsObject.parse(wrapArgsIntoQuotesIfNeeded(process.argv.slice(2)));
}
} catch (e) {
console.error(e.message);
Expand Down Expand Up @@ -81,8 +81,20 @@ export function rewriteTargetsAndProjects(args: string[]) {
return newArgs;
}

function wrapIntoQuotesIfNeeded(arg: string) {
return arg.indexOf(':') > -1 ? `"${arg}"` : arg;
function wrapArgsIntoQuotesIfNeeded(args: string[]): string[] {
return args.map((arg: string) => {
if (arg.includes('=')) {
const [key, value] = arg.split('=');
if (key.startsWith('--') && value.includes(' ') && !value.includes('"')) {
return `${key}="${value}"`;
}
return arg;
} else if (arg.includes(' ')) {
return `"${arg}"`;
} else {
return arg;
}
});
}

function isKnownCommand(command: string) {
Expand Down

0 comments on commit f7a073f

Please sign in to comment.