Skip to content

Commit

Permalink
fix dashed-args for custom args (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed Aug 13, 2023
1 parent eff1544 commit 5260c8f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {JESParserPluginOptions, JESParserOptions, createProjectWorkspace, Projec
export interface RunArgs {
args: string[];
replace?: boolean; // default is false
skipConversion?: boolean, // if true, args will not go through any conversion, such as dashed arg conversion.
}
export type SnapshotData = Record<string, string>;
export interface Options {
Expand Down
20 changes: 14 additions & 6 deletions src/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,21 @@ export default class Runner extends EventEmitter {
this._exited = false;
}

__convertDashedArgs(args: string[]): string[] {
if (!this.workspace.useDashedArgs) {
return args;
}

return args.map((arg) =>
arg && arg.startsWith('--') && arg.length > 2 ? arg.replace(/(\B)([A-Z])/gm, '-$2').toLowerCase() : arg
);
}

_getArgs(): string[] {
if (this.options.args && this.options.args.replace) {
return this.options.args.args;
return this.options.args.skipConversion
? this.options.args.args
: this.__convertDashedArgs(this.options.args.args);
}

// Handle the arg change on v18
Expand Down Expand Up @@ -88,11 +100,7 @@ export default class Runner extends EventEmitter {
if (this.options.args) {
args.push(...this.options.args.args);
}
if (this.workspace.useDashedArgs) {
args = args.map((arg) =>
arg && arg.startsWith('--') && arg.length > 2 ? arg.replace(/(\B)([A-Z])/gm, '-$2').toLowerCase() : arg
);
}
args = this.__convertDashedArgs(args);

return args;
}
Expand Down
11 changes: 7 additions & 4 deletions src/__tests__/runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,14 @@ describe('Runner', () => {
expect(args[0]).toBe(expected);
});

it('converts user passed in args', () => {
const expected = '--foo-bar-baz';

it.each`
argOption | expected
${{args: ['--fooBarBaz']}} | ${'--foo-bar-baz'}
${{args: ['--fooBarBaz'], replace: true}} | ${'--foo-bar-baz'}
${{args: ['--fooBarBaz'], replace: true, skipConversion: true}} | ${'--fooBarBaz'}
`('converts user passed in args', ({argOption, expected}) => {
const workspace: any = {useDashedArgs: true};
const options = {args: {args: ['--fooBarBaz']}};
const options = {args: argOption};
const sut = new Runner(workspace, options);
sut.start(false);

Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type Location = {
export type RunArgs = {
args: Array<string>,
replace?: boolean, // default is false
skipConversion?: boolean, // if true, args will not go through any conversion, such as dashed arg conversion.
};

export type Options = {
Expand Down

0 comments on commit 5260c8f

Please sign in to comment.