diff --git a/packages/nx/bin/init-local.spec.ts b/packages/nx/bin/init-local.spec.ts new file mode 100644 index 00000000000000..25f3c56c30e447 --- /dev/null +++ b/packages/nx/bin/init-local.spec.ts @@ -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']); + }); +}); diff --git a/packages/nx/bin/init-local.ts b/packages/nx/bin/init-local.ts index 13a98b054ae052..f83d6b9609de4c 100644 --- a/packages/nx/bin/init-local.ts +++ b/packages/nx/bin/init-local.ts @@ -31,7 +31,7 @@ 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'); @@ -39,10 +39,10 @@ export function initLocal(workspace: WorkspaceTypeAndRoot) { 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); @@ -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) {