Skip to content

Commit 899625c

Browse files
authored
feat(core): format additional flags values for logging (#11564)
* feat(core): format additional flags values for logging * cleanup(core): simplify formatValues
1 parent 001fed8 commit 899625c

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { formatFlags } from './formatting-utils';
2+
3+
describe('formatFlags', () => {
4+
it('should properly show string values', () => {
5+
expect(formatFlags('', 'myflag', 'myvalue')).toBe(' --myflag=myvalue');
6+
});
7+
it('should properly show number values', () => {
8+
expect(formatFlags('', 'myflag', 123)).toBe(' --myflag=123');
9+
});
10+
it('should properly show boolean values', () => {
11+
expect(formatFlags('', 'myflag', true)).toBe(' --myflag=true');
12+
});
13+
it('should properly show array values', () => {
14+
expect(formatFlags('', 'myflag', [1, 23, 'abc'])).toBe(
15+
' --myflag=[1,23,abc]'
16+
);
17+
});
18+
it('should properly show object values', () => {
19+
expect(formatFlags('', 'myflag', { abc: 'def', ghi: { jkl: 42 } })).toBe(
20+
' --myflag={"abc":"def","ghi":{"jkl":42}}'
21+
);
22+
});
23+
it('should not break on invalid inputs', () => {
24+
expect(formatFlags('', 'myflag', (abc) => abc)).toBe(
25+
' --myflag=(abc) => abc'
26+
);
27+
expect(formatFlags('', 'myflag', NaN)).toBe(' --myflag=NaN');
28+
});
29+
it('should decompose positional values', () => {
30+
expect(formatFlags('', '_', ['foo', 'bar', 42, 'baz'])).toBe(
31+
' foo bar 42 baz'
32+
);
33+
});
34+
it('should handle indentation', () => {
35+
expect(formatFlags('_____', 'myflag', 'myvalue')).toBe(
36+
'_____ --myflag=myvalue'
37+
);
38+
});
39+
it('should handle indentation with positionals', () => {
40+
expect(formatFlags('_____', '_', ['foo', 'bar', 42, 'baz'])).toBe(
41+
'_____ foo bar 42 baz'
42+
);
43+
});
44+
});

packages/nx/src/tasks-runner/life-cycles/formatting-utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,15 @@ export function formatFlags(
55
): string {
66
return flag == '_'
77
? `${leftPadding} ${(value as string[]).join(' ')}`
8-
: `${leftPadding} --${flag}=${value}`;
8+
: `${leftPadding} --${flag}=${formatValue(value)}`;
9+
}
10+
11+
function formatValue(value: any) {
12+
if (Array.isArray(value)) {
13+
return `[${value.join(',')}]`;
14+
} else if (typeof value === 'object') {
15+
return JSON.stringify(value);
16+
} else {
17+
return value;
18+
}
919
}

0 commit comments

Comments
 (0)