Skip to content

Commit

Permalink
feat: passes arguments to tasks, both for commands and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 25, 2019
1 parent 1a6bd2a commit 18042df
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/bin/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import { loadPackage, flags, safePairs } from 'cli-belt';
import { loadPackage, flags, safePairs, splitBy } from 'cli-belt';
import { stripIndent as indent } from 'common-tags';
import arg from 'arg';
import chalk from 'chalk';
Expand Down Expand Up @@ -129,8 +129,7 @@ export default async function main(argv: string[]): Promise<void> {
case ':raise':
return console.log('TODO :raise');
case ':run':
// TODO: add arguments after --
return run(cmd._);
return run(...splitBy(cmd._));
default:
throw Error('Unknown command ' + first);
}
Expand Down
7 changes: 5 additions & 2 deletions src/commands/run/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import runTask from './task';

export default async function run(tasks: string[]): Promise<void> {
export default async function run(
tasks: string[],
args: string[]
): Promise<void> {
if (!tasks || !tasks.length) throw Error(`No tasks to run`);
if (tasks.find((x) => x[0] === ':')) {
throw Error(
Expand All @@ -14,6 +17,6 @@ export default async function run(tasks: string[]): Promise<void> {
}

for (let path of tasks) {
await runTask(path);
await runTask(path, args);
}
}
17 changes: 10 additions & 7 deletions src/commands/run/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,38 @@ import chalk from 'chalk';
import core from '~/core';
import { open } from '~/utils/errors';

export default async function runTask(path: string): Promise<void> {
export default async function runTask(
path: string,
args: string[]
): Promise<void> {
const task = await core.task(path);
logger.info('\nRunning task: ' + chalk.bold.green(task.path));
await trunk(task.script);
await trunk(task.script, args);
logger.debug('Done with task: ' + task.path);
}

export async function trunk(script: TScript): Promise<void> {
export async function trunk(script: TScript, args: string[]): Promise<void> {
if (!script) {
logger.debug('Empty task');
return;
}
if (typeof script === 'string') {
logger.debug('Command exec: ' + script);
return core.exec(script);
return core.exec(script, args);
}
if (typeof script === 'function') {
logger.debug('Run function' + (script.name ? ` ${script.name}` : ''));
let res: TScript;
try {
res = await script();
res = await script(args);
} catch (err) {
throw open.ensure(err);
}
return trunk(res);
return trunk(res, args);
}
if (Array.isArray(script)) {
for (let sub of script) {
await trunk(sub);
await trunk(sub, args);
}
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import _exec from '~/utils/exec';

export default async function exec(
command: string,
args: string[],
cwd: string,
bin: string[],
env?: IOfType<string> | undefined
Expand All @@ -15,5 +16,5 @@ export default async function exec(
const alter = manage(opts.env);
alter.unshift(bin);

return _exec(command, opts);
return _exec(command, args, opts);
}
4 changes: 2 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ const core = {
const { kpo, pkg } = await core.load();
return getTask(path, kpo || undefined, pkg || undefined);
},
async exec(command: string): Promise<void> {
async exec(command: string, args: string[]): Promise<void> {
const paths = await core.paths();
const bin = await core.bin();
const env = await core.get('env');
return exec(command, paths.directory, bin, env);
return exec(command, args, paths.directory, bin, env);
},
async setScope(names: string[]): Promise<void> {
const paths = await core.paths();
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type TScript =
| null
| false
| string
| (() => Promise<TScript> | TScript)
| ((args: string[]) => Promise<TScript> | TScript)
| IScriptsArray;

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down
7 changes: 5 additions & 2 deletions src/utils/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ onExit(() => Object.values(processes).forEach((p) => p.kill()));

export default async function exec(
command: string,
args: string[],
options?: SpawnOptions
): Promise<void> {
const opts: SpawnOptions = Object.assign({}, options);
Expand All @@ -21,9 +22,11 @@ export default async function exec(
if (!opts.stdio) opts.stdio = DEFAULT_STDIO;
if (!opts.env) opts.env = process.env;

logger.debug('Executing: ' + command);
logger.debug(
'Executing: ' + command + (args.length ? ` "${args.join('" "')}"` : '')
);
const id = uuid();
const ps = spawn(command, opts);
const ps = spawn(command, args, opts);
processes[id] = ps;

return new Promise((resolve: (arg: void) => void, reject) => {
Expand Down

0 comments on commit 18042df

Please sign in to comment.