Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): format additional flags values for logging #11564

Merged
merged 2 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { formatFlags } from './formatting-utils';

describe('formatFlags', () => {
it('should properly show string values', () => {
expect(formatFlags('', 'myflag', 'myvalue')).toBe(' --myflag=myvalue');
});
it('should properly show number values', () => {
expect(formatFlags('', 'myflag', 123)).toBe(' --myflag=123');
});
it('should properly show boolean values', () => {
expect(formatFlags('', 'myflag', true)).toBe(' --myflag=true');
});
it('should properly show array values', () => {
expect(formatFlags('', 'myflag', [1, 23, 'abc'])).toBe(
' --myflag=[1,23,abc]'
);
});
it('should properly show object values', () => {
expect(formatFlags('', 'myflag', { abc: 'def', ghi: { jkl: 42 } })).toBe(
' --myflag={"abc":"def","ghi":{"jkl":42}}'
);
});
it('should not break on invalid inputs', () => {
expect(formatFlags('', 'myflag', (abc) => abc)).toBe(
' --myflag=(abc) => abc'
);
expect(formatFlags('', 'myflag', NaN)).toBe(' --myflag=NaN');
});
it('should decompose positional values', () => {
expect(formatFlags('', '_', ['foo', 'bar', 42, 'baz'])).toBe(
' foo bar 42 baz'
);
});
it('should handle indentation', () => {
expect(formatFlags('_____', 'myflag', 'myvalue')).toBe(
'_____ --myflag=myvalue'
);
});
it('should handle indentation with positionals', () => {
expect(formatFlags('_____', '_', ['foo', 'bar', 42, 'baz'])).toBe(
'_____ foo bar 42 baz'
);
});
});
12 changes: 11 additions & 1 deletion packages/nx/src/tasks-runner/life-cycles/formatting-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@ export function formatFlags(
): string {
return flag == '_'
? `${leftPadding} ${(value as string[]).join(' ')}`
: `${leftPadding} --${flag}=${value}`;
: `${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;
}
}