-
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): unparse ovverrids for command line output
- Loading branch information
Lukasz Gawrys
authored and
Lukasz Gawrys
committed
Oct 14, 2022
1 parent
9788393
commit a3f7e0c
Showing
7 changed files
with
147 additions
and
65 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
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
22 changes: 9 additions & 13 deletions
22
packages/nx/src/tasks-runner/life-cycles/formatting-utils.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,19 +1,15 @@ | ||
import { serializeSingleOverride } from 'nx/src/utils/serialize-overrides-into-command-line'; | ||
|
||
export function formatFlags( | ||
leftPadding: string, | ||
flag: string, | ||
value: any | ||
): string { | ||
return flag == '_' | ||
? `${leftPadding} ${(value as string[]).join(' ')}` | ||
: `${leftPadding} --${flag}=${formatValue(value)}`; | ||
} | ||
|
||
function formatValue(value: any) { | ||
if (Array.isArray(value)) { | ||
return `[${value.join(',')}]`; | ||
} else if (typeof value === 'object') { | ||
return JSON.stringify(value); | ||
} else { | ||
return value; | ||
): string[] { | ||
if (flag === '_') { | ||
return [`${leftPadding} ${(value as string[]).join(' ')}`]; | ||
} | ||
|
||
return serializeSingleOverride(flag, value).map( | ||
(s) => `${leftPadding} ${s}` | ||
); | ||
} |
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
53 changes: 42 additions & 11 deletions
53
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,17 +1,48 @@ | ||
import { flatten } from 'flat'; | ||
|
||
export function serializeOverridesIntoCommandLine(args: { | ||
[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}=${ | ||
typeof args[a] === 'object' ? JSON.stringify(args[a]) : args[a] | ||
}` | ||
); | ||
} | ||
}); | ||
|
||
Object.keys(args) | ||
.filter((a) => a !== '_') | ||
.forEach((a) => r.push(...serializeSingleOverride(a, args[a]))); | ||
|
||
return r; | ||
} | ||
|
||
export function serializeSingleOverride(key: string, value: any): string[] { | ||
if (value === true) { | ||
return [`--${key}`]; | ||
} else if (value === false) { | ||
return [`--no-${key}`]; | ||
} else if (Array.isArray(value)) { | ||
return value | ||
.map((item) => serializeSingleOverride(key, item)) | ||
.flat() as string[]; | ||
} else if (typeof value === 'object') { | ||
const flattened = flatten<any, any>(value, { safe: true }); | ||
|
||
return Object.keys(flattened) | ||
.map((flattenedKey) => { | ||
return serializeSingleOverride( | ||
`${key}.${flattenedKey}`, | ||
flattened[flattenedKey] | ||
); | ||
}) | ||
.flat(); | ||
} else if ( | ||
typeof value === 'string' && | ||
stringShouldBeWrappedIntoQuotes(value) | ||
) { | ||
const sanitized = value.replace(/"/g, String.raw`\"`); | ||
return [`--${key}="${sanitized}"`]; | ||
} else if (value != null) { | ||
return [`--${key}=${value}`]; | ||
} | ||
} | ||
|
||
function stringShouldBeWrappedIntoQuotes(str: string) { | ||
return str.includes(' ') || str.includes('{') || str.includes('"'); | ||
} |