Skip to content

Commit

Permalink
feat(core): adds run
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 25, 2019
1 parent dcdb7c7 commit 7ca5479
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { getTask, getAllTasks } from './tasks';
import { IPaths, ILoaded, ITask, ITasks } from './types';
import getBin from './bin';
import exec from './exec';
import run from './run';
import logger from '~/utils/logger';
import { TCoreOptions, IExecOptions } from '~/types';
import { TCoreOptions, IExecOptions, TScript } from '~/types';
import { rejects } from 'errorish';

export interface ICoreState {
scopes: string[];
Expand Down Expand Up @@ -65,6 +67,17 @@ const core = {
const { kpo, pkg } = await core.load();
return getTask(path, kpo || undefined, pkg || undefined);
},
async run(
script: TScript,
args: string[],
opts?: IExecOptions
): Promise<void> {
return run(script, async (item) => {
return typeof item === 'string'
? core.exec(item, args, opts)
: item(args);
}).catch(rejects);
},
async exec(
command: string,
args: string[],
Expand Down
32 changes: 32 additions & 0 deletions src/core/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import logger from '~/utils/logger';
import { TScript } from '~/types';
import { open } from '~/utils/errors';

export default async function run(
script: TScript,
runner: (
item: string | ((args: string[]) => Promise<TScript> | TScript)
) => Promise<any>
): Promise<void> {
if (!script) {
logger.debug('Empty task');
} else if (typeof script === 'string') {
logger.debug('Command exec: ' + script);
await runner(script);
} else if (typeof script === 'function') {
logger.debug('Run function' + (script.name ? ` ${script.name}` : ''));
let res: TScript;
try {
res = await runner(script);
} catch (err) {
throw open.ensure(err);
}
await run(res, runner);
} else if (Array.isArray(script)) {
for (let sub of script) {
await run(sub, runner);
}
} else {
throw Error(`Task wasn't a TScript`);
}
}

0 comments on commit 7ca5479

Please sign in to comment.