Skip to content

Commit

Permalink
feat: removes parse util in favor of added combine task and recreate …
Browse files Browse the repository at this point in the history
…util
  • Loading branch information
rafamel committed Feb 19, 2021
1 parent 33391e3 commit dbca481
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 116 deletions.
84 changes: 0 additions & 84 deletions src/helpers/parse-record.ts

This file was deleted.

79 changes: 79 additions & 0 deletions src/helpers/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Task } from '../definitions';
import { series } from '../tasks/aggregate/series';
import { stringifyRoute } from './stringify-route';
import { Members } from 'type-core';

interface Item {
name: string;
route: string[];
task: Task;
}

interface Options {
include: string[] | null;
exclude: string[] | null;
}

export function parseToRecord(
options: Options,
record: Task.Record
): Members<Task> {
const { include, exclude } = options;
const arr = parseToArray(record);

const members: Members<Task> = {};
for (const item of arr) {
if (exclude && exclude.includes(item.name)) continue;
if (include && !include.includes(item.name)) continue;
members[item.name] = item.task;
}

if (include) {
for (const name of include) {
if (!Object.hasOwnProperty.call(members, name)) {
throw Error(`Task not found: ${name}`);
}
}
}

return members;
}

export function parseToArray(record: Task.Record): Item[] {
const names: string[] = [];

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

if (names.includes(name)) {
throw Error(`Name collusion on parse: ${name}`);
}

names.push(name);
return { name, route, task };
})
.filter((item): item is Item => Boolean(item.task));
}

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

for (const [name, tasks] of Object.entries(record)) {
if (typeof tasks === 'function') {
arr.push([[name], tasks]);
} else {
const all: Task[] = [];
const every: Array<[string[], Task]> = [];
for (const [route, task] of parseHelper(tasks)) {
every.push([[name, ...route], task]);
if (route.length <= 1) all.push(task);
}

if (all.length) arr.push([[name], series(...all)]);
if (every.length) arr.push(...every);
}
}

return arr;
}
10 changes: 2 additions & 8 deletions src/helpers/prefix.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from '../definitions';
import { stringifyRoute } from './stringify-route';
import { into } from 'pipettes';
import ObjectPath from 'objectpath';
import chalk from 'chalk';

export function getPrefix(
Expand All @@ -9,13 +9,7 @@ export function getPrefix(
context: Context
): string {
return into(
context.route.length
? ObjectPath.stringify(
context.route.map((x) => String(x)),
"'",
false
)
: '',
context.route.length ? stringifyRoute(context.route) : '',
(prefix) => {
if (!prefix || (context.prefix !== target && context.prefix !== 'all')) {
return extra ? extra + ' ' : null;
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/stringify-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ObjectPath from 'objectpath';

export function stringifyRoute(route: Array<string | number>): string {
return ObjectPath.stringify(
route.map((x) => String(x)),
"'",
false
);
}
33 changes: 33 additions & 0 deletions src/tasks/aggregate/combine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Task, Context } from '../../definitions';
import { parseToRecord } from '../../helpers/parse';
import { run } from '../../utils/run';
import { recreate } from '../../utils/recreate';
import { context } from '../transform/context';
import { series } from './series';
import { into } from 'pipettes';

export function combine(
tasks: Task.Record,
names: string | string[]
): Task.Async {
const keys = Array.isArray(names) ? names : [names];

return async (ctx: Context): Promise<void> => {
return into(
tasks,
recreate.bind(null, (task, route) => {
return context(
(ctx) => ({
...ctx,
route: ctx.route.concat(route)
}),
task
);
}),
parseToRecord.bind(null, { include: keys, exclude: null }),
(record) => keys.map((key) => record[key]),
(arr) => series(...arr),
(task) => run(task, ctx)
);
};
}
1 change: 1 addition & 0 deletions src/tasks/aggregate/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './combine';
export * from './parallel';
export * from './select';
export * from './series';
18 changes: 14 additions & 4 deletions src/tasks/reflection/lift.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Task, Context } from '../../definitions';
import { parseToRecord } from '../../helpers/parse';
import { getAbsolutePath } from '../../helpers/paths';
import { isCancelled } from '../../utils/is-cancelled';
import { parse } from '../../utils/parse';
import { run } from '../../utils/run';
import { select } from '../aggregate/select';
import { write } from '../filesystem/write';
Expand Down Expand Up @@ -29,9 +29,19 @@ export function lift(tasks: Task.Record, options?: LiftOptions): Task.Async {
const pkg = JSON.parse(content.toString());

const pkgScripts: Members<string> = pkg.scripts || {};
const taskScripts = Object.keys(parse(tasks)).reduce(
(acc: Members<string>, name) => ({ ...acc, [name]: `kpo ${name} --` }),
{}
const taskScripts = into(
tasks,
parseToRecord.bind(null, { include: null, exclude: null }),
(record) => Object.keys(record),
(keys) => {
return keys.reduce(
(acc: Members<string>, name) => ({
...acc,
[name]: `kpo ${name} --`
}),
{}
);
}
);

pkg.scripts = opts.purge ? taskScripts : { ...pkg.scripts, ...taskScripts };
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/reflection/list.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Task, Context } from '../../definitions';
import { parseRecord } from '../../helpers/parse-record';
import { parseToArray } from '../../helpers/parse';
import { print } from '../stdio/print';
import { into } from 'pipettes';
import chalk from 'chalk';

export function list(tasks: Task.Record): Task.Sync {
return (ctx: Context): void => {
const str = parseRecord(tasks)
const str = parseToArray(tasks)
.map((item) => {
return ' '.repeat((item.route.length - 1) * 2) + item.name;
})
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './fetch';
export * from './is-cancelled';
export * from './parse';
export * from './recreate';
export * from './run';
17 changes: 0 additions & 17 deletions src/utils/parse.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/utils/recreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Task } from '../definitions';
import { Empty } from 'type-core';

export function recreate(
map: (task: Task, route: string[]) => Task | Empty,
tasks: Task.Record
): Task.Record {
return recreateHelper([], tasks, map);
}

function recreateHelper(
route: string[],
tasks: Task.Record,
map: (task: Task, route: string[]) => Task | Empty
): Task.Record {
return Object.entries(tasks).reduce((acc, [key, value]) => {
if (typeof value === 'function') {
const task = map(value, route.concat(key));
return task ? { ...acc, [key]: task } : acc;
} else {
const record = recreateHelper(route.concat(key), value, map);
return Object.keys(record).length ? { ...acc, [key]: record } : acc;
}
}, {} as Task.Record);
}

0 comments on commit dbca481

Please sign in to comment.