-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): properly serialize override with object arg for command li…
…ne (#12499) Co-authored-by: Lukasz Gawrys <lukaszgawrys@MBP-von-Lukasz.fritz.box> Co-authored-by: Jason Jean <jasonjean1993@gmail.com>
- Loading branch information
1 parent
0f2b89b
commit 594c63b
Showing
3 changed files
with
45 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 41 additions & 10 deletions
51
packages/nx/src/utils/serialize-overrides-into-command-line.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
export function serializeOverridesIntoCommandLine(args: { | ||
import { flatten } from 'flat'; | ||
|
||
export function serializeOverridesIntoCommandLine(options: { | ||
[k: string]: any; | ||
}): string[] { | ||
const r = args['_'] ? [...args['_']] : []; | ||
Object.keys(args).forEach((a) => { | ||
if (a !== '_') { | ||
r.push( | ||
typeof args[a] === 'string' && args[a].includes(' ') | ||
? `--${a}="${args[a].replace(/"/g, '"')}"` | ||
: `--${a}=${args[a]}` | ||
const unparsed = options._ ? [...options._] : []; | ||
for (const key of Object.keys(options)) { | ||
const value = options[key]; | ||
if (key !== '_') { | ||
serializeOption(key, value, unparsed); | ||
} | ||
} | ||
|
||
return unparsed; | ||
} | ||
|
||
function serializeOption(key: string, value: any, unparsed: string[]) { | ||
if (value === true) { | ||
unparsed.push(`--${key}`); | ||
} else if (value === false) { | ||
unparsed.push(`--no-${key}`); | ||
} else if (Array.isArray(value)) { | ||
value.forEach((item) => serializeOption(key, item, unparsed)); | ||
} else if (Object.prototype.toString.call(value) === '[object Object]') { | ||
const flattened = flatten<any, any>(value, { safe: true }); | ||
for (const flattenedKey in flattened) { | ||
serializeOption( | ||
`${key}.${flattenedKey}`, | ||
flattened[flattenedKey], | ||
unparsed | ||
); | ||
} | ||
}); | ||
return r; | ||
} else if ( | ||
typeof value === 'string' && | ||
stringShouldBeWrappedIntoQuotes(value) | ||
) { | ||
const sanitized = value.replace(/"/g, String.raw`\"`); | ||
unparsed.push(`--${key}="${sanitized}"`); | ||
} else if (value != null) { | ||
unparsed.push(`--${key}=${value}`); | ||
} | ||
} | ||
|
||
function stringShouldBeWrappedIntoQuotes(str: string) { | ||
return str.includes(' ') || str.includes('{') || str.includes('"'); | ||
} |
594c63b
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.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app