Skip to content

Commit

Permalink
fix(core): properly print overrides for dot notation
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Oct 27, 2022
1 parent faa6e09 commit 45c9ad5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 48 deletions.
40 changes: 2 additions & 38 deletions packages/nx/src/tasks-runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { workspaceRoot } from '../utils/workspace-root';
import { NxJsonConfiguration } from '../config/nx-json';
import { joinPathFragments } from '../utils/path';
import { isRelativePath } from 'nx/src/utils/fileutils';
import { serializeOverridesIntoCommandLine } from 'nx/src/utils/serialize-overrides-into-command-line';

export function getCommandAsString(execCommand: string, task: Task) {
const args = getPrintableCommandArgsForTask(task);
Expand Down Expand Up @@ -362,42 +363,5 @@ function longRunningTask(task: Task) {

// TODO: vsavkin remove when nx-cloud doesn't depend on it
export function unparse(options: Object): string[] {
const unparsed = [];
for (const key of Object.keys(options)) {
const value = options[key];
unparseOption(key, value, unparsed);
}

return unparsed;
}

function unparseOption(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) => unparseOption(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) {
unparseOption(
`${key}.${flattenedKey}`,
flattened[flattenedKey],
unparsed
);
}
} 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('"');
return serializeOverridesIntoCommandLine(options);
}
49 changes: 39 additions & 10 deletions packages/nx/src/utils/serialize-overrides-into-command-line.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
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 = [];
for (const key of Object.keys(options)) {
const value = options[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('"');
}

0 comments on commit 45c9ad5

Please sign in to comment.