Skip to content

Commit

Permalink
feat(tasks): combine task takes include, exclude, and defaults options
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 3, 2021
1 parent a1cc544 commit c9f948a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export default async function main(
-f, --file <path> Configuration file
-d, --dir <path> Project directory
-e, --env <value> Environment variables
--prefix Print task routes
--level <value> Logging level
--prefix Prefix task output with its route
-h, --help Show help
-v, --version Show version number
Expand All @@ -59,8 +59,8 @@ export default async function main(
'--file': String,
'--dir': String,
'--env': [String] as [StringConstructor],
'--prefix': Boolean,
'--level': String,
'--prefix': Boolean,
'--help': Boolean,
'--version': Boolean
};
Expand Down Expand Up @@ -137,7 +137,7 @@ export default async function main(
names.join(' ')
),
print(),
combine(record, names)
combine(record, { include: names, defaults: true })
),
context.bind(null, { args }),
withContext
Expand Down
2 changes: 1 addition & 1 deletion src/bin/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default async function bin(
names.join(' ')
);
}),
combine(params.record, names)
combine(params.record, { include: names, defaults: true })
)
)
)
Expand Down
25 changes: 17 additions & 8 deletions src/helpers/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Item {
}

interface ParseToArrayOptions {
roots: boolean;
defaults: boolean;
}

Expand All @@ -24,7 +25,10 @@ export function parseToRecord(
record: Task.Record
): Members<Task> {
const { include, exclude } = options;
const arr = parseToArray({ defaults: options.defaults }, record);
const arr = parseToArray(
{ roots: options.roots, defaults: options.defaults },
record
);

const members: Members<Task> = {};
for (const item of arr) {
Expand All @@ -50,7 +54,7 @@ export function parseToArray(
): Item[] {
const names: string[] = [];

return parseHelper(record)
return parseHelper(record, options.roots)
.map(([route, task]) => {
const name = stringifyRoute(route);

Expand All @@ -73,7 +77,10 @@ export function parseToArray(
});
}

function parseHelper(record: Task.Record): Array<[string[], Task]> {
function parseHelper(
record: Task.Record,
roots: boolean
): Array<[string[], Task]> {
const arr: Array<[string[], Task]> = [];

for (const [name, tasks] of Object.entries(record)) {
Expand All @@ -83,7 +90,7 @@ function parseHelper(record: Task.Record): Array<[string[], Task]> {
const all: Task[] = [];
const defaults: Task[] = [];
const every: Array<[string[], Task]> = [];
for (const [route, task] of parseHelper(tasks)) {
for (const [route, task] of parseHelper(tasks, roots)) {
every.push([[name, ...route], task]);
if (route.length <= 1) {
route[0] === constants.record.default
Expand All @@ -92,10 +99,12 @@ function parseHelper(record: Task.Record): Array<[string[], Task]> {
}
}

if (defaults.length) {
arr.push([[name], series(...defaults)]);
} else if (all.length) {
arr.push([[name], series(...all)]);
if (roots) {
if (defaults.length) {
arr.push([[name], series(...defaults)]);
} else if (all.length) {
arr.push([[name], series(...all)]);
}
}

if (every.length) arr.push(...every);
Expand Down
55 changes: 40 additions & 15 deletions src/tasks/aggregate/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,65 @@ import { run } from '../../utils/run';
import { recreate } from '../../utils/recreate';
import { context } from '../transform/context';
import { series } from './series';
import { shallow } from 'merge-strategies';
import { into } from 'pipettes';

export interface CombineOptions {
/**
* An array of strings indicating the name of the tasks
* to include for a task record.
* A task name is the stringification of its route in the record,
* joined by with a ':' character for each depth level.
* Targetting a level with inner tasks will run all of them.
*/
include?: string[] | null;
/**
* An array of strings indicating the name of the tasks
* to exclude for a task record.
* A task name is the stringification of its route in the record,
* joined by with a ':' character for each depth level.
*/
exclude?: string[] | null;
/**
* Whether to consider default tasks as separate from their root.
*/
defaults?: boolean;
}

/**
* Takes a task nested record `tasks` and runs a number
* of them in series as selected by their `names`.
* A task name will be the stringification of its route
* in the record, with a ':' character for each level deep.
* Targetting a level with inner tasks will run all of them.
* of them in series as selected by the inclusion or exclusion options.
* @returns Task
*/
export function combine(
tasks: Task.Record,
names: string | string[]
options?: CombineOptions
): Task.Async {
const keys = Array.isArray(names) ? names : [names];

return async (ctx: Context): Promise<void> => {
const opts: Required<CombineOptions> = shallow(
{ include: null, exclude: null, defaults: false },
options || undefined
);

return into(
tasks,
recreate.bind(null, (task, route) => {
return context(
(ctx) => ({
...ctx,
route: ctx.route.concat(route)
}),
(ctx) => ({ ...ctx, route: ctx.route.concat(route) }),
task
);
}),
parseToRecord.bind(null, {
include: keys,
exclude: null,
defaults: true
include: opts.include,
exclude: opts.exclude,
defaults: opts.defaults,
roots: Array.isArray(opts.include)
}),
(record) => keys.map((key) => record[key]),
(record) => {
return Array.isArray(opts.include)
? opts.include.map((name) => record[name])
: Object.values(record);
},
(arr) => series(...arr),
(task) => run(task, ctx)
);
Expand Down
1 change: 1 addition & 0 deletions src/tasks/reflection/lift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function lift(
parseToRecord.bind(null, {
include: null,
exclude: null,
roots: true,
defaults: opts.defaults
}),
(record) => Object.keys(record),
Expand Down
5 changes: 4 additions & 1 deletion src/tasks/reflection/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export function list(
);

const source = await getTaskRecord(tasks);
const items = parseToArray({ defaults: opts.defaults }, source);
const items = parseToArray(
{ roots: true, defaults: opts.defaults },
source
);
const maxRouteLength = items.reduce(
(acc, item) => (acc > item.route.length ? acc : item.route.length),
0
Expand Down

0 comments on commit c9f948a

Please sign in to comment.