Skip to content

Commit

Permalink
feat(core): format additional flags values for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Aug 12, 2022
1 parent 45f4e37 commit b97ac31
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
44 changes: 44 additions & 0 deletions packages/nx/src/tasks-runner/life-cycles/formatting-utils.spec.ts
@@ -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'
);
});
});
14 changes: 13 additions & 1 deletion packages/nx/src/tasks-runner/life-cycles/formatting-utils.ts
Expand Up @@ -5,5 +5,17 @@ export function formatFlags(
): string {
return flag == '_'
? `${leftPadding} ${(value as string[]).join(' ')}`
: `${leftPadding} --${flag}=${value}`;
: `${leftPadding} --${flag}=${formatValue(value)}`;
}

function formatValue(value: any) {
if (typeof value === 'string' || typeof value === 'number') {
return value;
} else if (Array.isArray(value)) {
return `[${value.join(',')}]`;
} else if (typeof value === 'object') {
return JSON.stringify(value);
} else {
return value;
}
}

0 comments on commit b97ac31

Please sign in to comment.