Skip to content

Commit

Permalink
feat: all functions taking options and tasks take options as a parame…
Browse files Browse the repository at this point in the history
…ter before tasks
  • Loading branch information
rafamel committed Apr 20, 2021
1 parent 4e4d542 commit 44f5479
Show file tree
Hide file tree
Showing 27 changed files with 120 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/cli/bin/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function execute(task: NullaryFn<Task>): Promise<void> {
const cancellation = new Promise<void>((resolve) => cbs.push(resolve));

const exits = await import('exits');
const promise = run(create(task), { cancellation });
const promise = run({ cancellation }, create(task));

exits.attach();
exits.options({
Expand All @@ -28,7 +28,7 @@ export async function execute(task: NullaryFn<Task>): Promise<void> {

await promise;
} catch (err) {
await run(log('error', stringifyError(err)));
await run(null, log('error', stringifyError(err)));
return process.exit(1);
}
}
4 changes: 2 additions & 2 deletions src/cli/bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ export function main(
(task) => {
return async (ctx: Context): Promise<void> => {
try {
await run(task, ctx);
await run(ctx, task);
} catch (err) {
await run(log('trace', err), ctx);
await run(ctx, log('trace', err));
throw err;
}
};
Expand Down
17 changes: 10 additions & 7 deletions src/cli/commands/lift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ export async function lift(params: CLI.Extension.Params): Promise<Task> {

return series(
print(),
_lift(tasks, {
purge: cmd['--purge'],
defaults: cmd['--defaults'],
mode: cmd['--mode'] as any,
bin: params.options.bin,
multitask: params.options.multitask
})
_lift(
{
purge: cmd['--purge'],
defaults: cmd['--defaults'],
mode: cmd['--mode'] as any,
bin: params.options.bin,
multitask: params.options.multitask
},
tasks
)
);
}
11 changes: 7 additions & 4 deletions src/cli/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ export async function list(params: CLI.Extension.Params): Promise<Task> {

return series(
print(),
_list(tasks, {
defaults: cmd['--defaults'],
bin: params.options.bin
})
_list(
{
defaults: cmd['--defaults'],
bin: params.options.bin
},
tasks
)
);
}
2 changes: 1 addition & 1 deletion src/cli/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export async function run(params: CLI.Extension.Params): Promise<Task> {
directory: params.options.directory
});

return context({ args }, combine(tasks, { include: names }));
return context({ args }, combine({ include: names }, tasks));
}
2 changes: 1 addition & 1 deletion src/cli/commands/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function watch(params: CLI.Extension.Params): Promise<Task> {
stringifyArgvCommands(params.argv)
);
}),
combine(tasks, { include: names })
combine({ include: names }, tasks)
)
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function useSource(
if (options.strict) {
throw Error(`Source path doesn't exist: ${src}`);
}
await run(log('debug', 'Ignore source:', src), context);
await run(context, log('debug', 'Ignore source:', src));
}

export async function useDestination(
Expand All @@ -55,7 +55,7 @@ export async function useDestination(
if (!exists) return cb(dest);

if (options.exists === 'ignore') {
await run(log('debug', 'Ignore destination:', dest), context);
await run(context, log('debug', 'Ignore destination:', dest));
} else {
throw Error(`Destination exists: ${dest}`);
}
Expand Down
14 changes: 7 additions & 7 deletions src/tasks/aggregate/combine.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Empty, NullaryFn } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import { Task } from '../../definitions';
import { parseToRecord } from '../../helpers/parse';
import { recreate } from '../../utils/recreate';
import { context } from '../creation/context';
import { create } from '../creation/create';
import { series } from './series';
import { NullaryFn } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';

export interface CombineOptions {
/**
Expand All @@ -32,8 +32,8 @@ export interface CombineOptions {
* @returns Task
*/
export function combine(
tasks: Task.Record | NullaryFn<Task.Record>,
options?: CombineOptions
options: CombineOptions | Empty,
tasks: Task.Record | NullaryFn<Task.Record>
): Task.Async {
return create(() => {
const opts: Required<CombineOptions> = shallow(
Expand All @@ -42,12 +42,12 @@ export function combine(
);

return into(
recreate(tasks, (task, route) => {
recreate((task, route) => {
return context(
(ctx) => ({ ...ctx, route: ctx.route.concat(route) }),
task
);
}),
}, tasks),
parseToRecord.bind(null, {
include: opts.include,
exclude: opts.exclude,
Expand Down
17 changes: 10 additions & 7 deletions src/tasks/aggregate/parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ export function parallel(
try {
await Promise.all(
items.map((task) => {
return run(task, {
...ctx,
stdio: [null, ctx.stdio[1], ctx.stdio[2]],
cancellation: new Promise((resolve) => {
cbs.push(resolve);
})
});
return run(
{
...ctx,
stdio: [null, ctx.stdio[1], ctx.stdio[2]],
cancellation: new Promise((resolve) => {
cbs.push(resolve);
})
},
task
);
})
);
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/aggregate/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function series(
return async (ctx: Context): Promise<void> => {
for (const task of items) {
if (await isCancelled(ctx)) break;
await run(task, ctx);
await run(ctx, task);
}
};
}
6 changes: 3 additions & 3 deletions src/tasks/creation/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Task, Context } from '../../definitions';
import { run } from '../../utils/run';
import { UnaryFn, Empty } from 'type-core';
import { shallow } from 'merge-strategies';
import { Task, Context } from '../../definitions';
import { run } from '../../utils/run';

/**
* Modifies a task's context with a given `context`.
Expand All @@ -20,6 +20,6 @@ export function context(
): Task.Async {
const fn = typeof context === 'function' ? context : () => context;
return async (context: Context): Promise<void> => {
await run(task, shallow(context, fn(context) || undefined));
await run(shallow(context, fn(context) || undefined), task);
};
}
2 changes: 1 addition & 1 deletion src/tasks/creation/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function pipe(...fns: Array<UnaryFn<any, MaybePromise<any>>>): Task.Async {
if (await isCancelled(ctx)) break;
value = await fn(value);
}
if (value) await run(value, ctx);
if (value) await run(ctx, value);
};
}

Expand Down
13 changes: 7 additions & 6 deletions src/tasks/exception/catches.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Empty } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import { Task, Context, LogLevel } from '../../definitions';
import { stringifyError } from '../../helpers/stringify';
import { run } from '../../utils/run';
import { series } from '../aggregate/series';
import { log } from '../stdio/log';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';

export interface CatchesOptions {
/** Logs the error message with a given level. Default: `'warn'` */
Expand All @@ -22,23 +23,23 @@ export interface CatchesOptions {
* @returns Task
*/
export function catches(
options: CatchesOptions | Empty,
task: Task,
alternate?: Task | null,
options?: CatchesOptions
alternate?: Task | null
): Task.Async {
return async (ctx: Context): Promise<void> => {
const opts = shallow({ level: 'warn' }, options || undefined);

try {
await run(task, ctx);
await run(ctx, task);
} catch (err) {
await into(
series(
log('trace', err),
log(opts.level, stringifyError(err)),
alternate
),
(task) => run(task, ctx)
(task) => run(ctx, task)
);
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/exception/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export function finalize(task: Task, final?: Task | null): Task.Async {
const errors: Error[] = [];

try {
await run(task, ctx);
await run(ctx, task);
} catch (err) {
errors.push(err);
}

if (await isCancelled(ctx)) return;

try {
if (final) await run(final, ctx);
if (final) await run(ctx, final);
} catch (err) {
errors.push(err);
}
Expand All @@ -36,7 +36,7 @@ export function finalize(task: Task, final?: Task | null): Task.Async {
errors,
(arr) => arr.map((err) => log('trace', err)),
(tasks) => series(...tasks),
(task) => run(task, ctx)
(task) => run(ctx, task)
);
throw err;
};
Expand Down
30 changes: 16 additions & 14 deletions src/tasks/filesystem/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,25 @@ export function watch(options: WatchOptions | Empty, task: Task): Task.Async {
current = after
.then(() => {
i += 1;
return run(series(i > 0 && opts.clear ? clear() : null, task), {
...ctx,
route: opts.parallel ? ctx.route.concat(String(i)) : ctx.route,
cancellation: new Promise((resolve) => {
cbs.push(resolve);
})
});
return run(
{
...ctx,
route: opts.parallel
? ctx.route.concat(String(i))
: ctx.route,
cancellation: new Promise((resolve) => {
cbs.push(resolve);
})
},
series(i > 0 && opts.clear ? clear() : null, task)
);
})
.catch((err) => {
return opts.fail
? onError(err)
: run(
series(
log('trace', err),
log('error', stringifyError(err))
),
ctx
ctx,
series(log('trace', err), log('error', stringifyError(err)))
);
});
},
Expand All @@ -127,14 +129,14 @@ export function watch(options: WatchOptions | Empty, task: Task): Task.Async {
if (opts.prime) {
watcher.on('ready', () => {
promises.push(
run(log('debug', 'Watch event:', 'prime'), ctx).catch(reject)
run(ctx, log('debug', 'Watch event:', 'prime')).catch(reject)
);
onEvent(reject);
});
}
watcher.on('all', (event) => {
promises.push(
run(log('debug', 'Watch event:', event), ctx).catch(reject)
run(ctx, log('debug', 'Watch event:', event)).catch(reject)
);
onEvent(reject);
});
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/process/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function exec(
? ''
: `: ${cmd}`;

await run(log('trace', err), ctx);
await run(ctx, log('trace', err));
throw Error(message);
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/tasks/reflection/lift.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Members, NullaryFn, TypeGuard } from 'type-core';
import { Empty, Members, NullaryFn, TypeGuard } from 'type-core';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';
import fs from 'fs-extra';
Expand Down Expand Up @@ -50,8 +50,8 @@ export interface LiftOptions {
* @returns Task
*/
export function lift(
tasks: Task.Record | NullaryFn<Task.Record>,
options?: LiftOptions
options: LiftOptions | Empty,
tasks: Task.Record | NullaryFn<Task.Record>
): Task.Async {
return create(async (ctx) => {
const opts = shallow(
Expand Down Expand Up @@ -188,7 +188,7 @@ async function evaluateChanges(
strArr.push(style('No pending scripts changes', { bold: true }));
}

await run(print(strArr.join('\n')), context);
await run(context, print(strArr.join('\n')));

return areChangesPending;
}
6 changes: 3 additions & 3 deletions src/tasks/reflection/list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NullaryFn, TypeGuard } from 'type-core';
import { Empty, NullaryFn, TypeGuard } from 'type-core';
import { shallow } from 'merge-strategies';
import table from 'as-table';
import { Task } from '../../definitions';
Expand All @@ -25,8 +25,8 @@ export interface ListOptions {
* @returns Task
*/
export function list(
tasks: Task.Record | NullaryFn<Task.Record>,
options?: ListOptions
options: ListOptions | Empty,
tasks: Task.Record | NullaryFn<Task.Record>
): Task.Async {
return create(async () => {
const opts = shallow(
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/schedule/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function repeat(times: number | null, task: Task): Task.Async {
while (!isDone()) {
i++;
if (await isCancelled(ctx)) break;
await run(series(log('debug', 'Repeat task:', i), task), ctx);
await run(ctx, series(log('debug', 'Repeat task:', i), task));
}
}
);
Expand Down

0 comments on commit 44f5479

Please sign in to comment.