Skip to content

Commit e1b4af4

Browse files
committed
=refactor(utils): extract command with args string conversion
1 parent b60b8aa commit e1b4af4

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export {
5555
pluralize,
5656
pluralizeToken,
5757
roundDecimals,
58+
serializeCommandWithArgs,
5859
slugify,
5960
transformLines,
6061
truncateDescription,

packages/utils/src/lib/formatting.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,13 @@ export function transformLines(
154154
export function indentLines(text: string, identation: number): string {
155155
return transformLines(text, line => `${' '.repeat(identation)}${line}`);
156156
}
157+
158+
export function serializeCommandWithArgs({
159+
command,
160+
args,
161+
}: {
162+
command: string;
163+
args?: string[];
164+
}): string {
165+
return [command, ...(args ?? [])].join(' ');
166+
}

packages/utils/src/lib/formatting.unit.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
pluralize,
99
pluralizeToken,
1010
roundDecimals,
11+
serializeCommandWithArgs,
1112
slugify,
1213
transformLines,
1314
truncateMultilineText,
@@ -253,3 +254,18 @@ describe('indentLines', () => {
253254
);
254255
});
255256
});
257+
258+
describe('serializeCommandWithArgs', () => {
259+
it('should serialize command and args into an equivalent shell string', () => {
260+
expect(
261+
serializeCommandWithArgs({
262+
command: 'npx',
263+
args: ['eslint', '.', '--format=json'],
264+
}),
265+
).toBe('npx eslint . --format=json');
266+
});
267+
268+
it('should omit args if missing', () => {
269+
expect(serializeCommandWithArgs({ command: 'ls' })).toBe('ls');
270+
});
271+
});

0 commit comments

Comments
 (0)