Skip to content

Commit

Permalink
Merge pull request #85 from ianwremmel/fix-arbitrary-run
Browse files Browse the repository at this point in the history
Fix running of arbitrary commands without definition in .clarkrc
  • Loading branch information
ianwremmel committed Apr 12, 2018
2 parents 5f67f43 + 34450dc commit 603e390
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {CommandModule} from 'yargs';
import {Run} from '../lib/handlers/run';

const RunCommand: CommandModule = {
command: 'run',
command: 'run [script]',

describe:
'Runs a script in each package directory. This is different from `exec` in that scripts must be defined in .clarkrc and may be overridden on a per-package basis via npm scripts.',
'Runs a script in each package directory. This is different from `exec` in that scripts should be defined in .clarkrc and may be overridden on a per-package basis via npm scripts. npm scripts defined only in subpackage package.jsons can be run this way, but only scripts named in .clarkrc will populate the help output.',

builder: Run.builder,

Expand Down
78 changes: 46 additions & 32 deletions src/lib/handlers/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export namespace Run {
const config = load();

if (config.scripts && config.scripts) {
return Object.entries(config.scripts).reduce(
yargs = Object.entries(config.scripts).reduce(
(y, [command, script]: [string, string]): Argv =>
y.command({
command,
Expand All @@ -27,41 +27,17 @@ export namespace Run {
type: 'string',
}),
handler: async (argv: apply.InvocationOptions): Promise<void> => {
await apply(
{
before: (packages) =>
f`Running ${command} against ${packages.length} packages`,
beforeEach: (packageName) =>
f`Running ${command} against ${packageName}`,
afterEach: (packageName, error) => {
if (error) {
return `${command} failed against ${packageName}`;
}
return `Ran ${command} against ${packageName}`;
},
after: (packages, errors) => {
if (errors.length) {
return f`clark run failed to execute the following command against ${
errors.length
} packages\n> ${command}\n`;
}

return `Ran ${command} successfully against ${
packages.length
}`;
},
},
async (packageName) => {
await execScript(command, packageName, script);
},
argv,
);
await handle(command, script, argv);
},
}),
yargs,
);
}
return yargs
.positional('script', {
describe: 'npm run script to execute in each package that defines it. ',
type: 'string',
})
.options({
'fail-fast': {
alias: 'ff',
Expand All @@ -80,11 +56,49 @@ export namespace Run {
.strict();
}

/**
* Helper
*/
async function handle(
command: string,
script: string,
argv: apply.InvocationOptions,
) {
await apply(
{
before: (packages) =>
f`Running ${command} against ${packages.length} packages`,
beforeEach: (packageName) =>
f`Running ${command} against ${packageName}`,
afterEach: (packageName, error) => {
if (error) {
return `${command} failed against ${packageName}`;
}
return `Ran ${command} against ${packageName}`;
},
after: (packages, errors) => {
if (errors.length) {
return f`clark run failed to execute the following command against ${
errors.length
} packages\n> ${command}\n`;
}

return `Ran ${command} successfully against ${packages.length}`;
},
},
async (packageName) => {
await execScript(command, packageName, script);
},
argv,
);
}
/**
* Implementation of the run command
* @param options
*/
export async function handler(): Promise<void> {
// noop
export async function handler(
argv: apply.InvocationOptions & {script: string},
): Promise<void> {
await handle(argv.script, '', argv);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions test/integration/spec/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ describe('run', () => {
assert.equal(result, 'this is an override');
});
});

describe('when invoked with a script not in .clarkrc', () => {
it('runs that script in any package that defines it', async () => {
const result = await run('clark run --silent not-in-clarkrc');
assert.equal(result, '1\n2');
});

it('supports --package', async () => {
const result = await run(
'clark run --silent --package @example/scoped-package-the-first not-in-clarkrc',
);
assert.equal(result, '1');
});
});
});

0 comments on commit 603e390

Please sign in to comment.