Skip to content

Commit

Permalink
feat: lift and list tasks can fetch default tasks file; fetch will us…
Browse files Browse the repository at this point in the history
…e the default tasks file name when none is provided
  • Loading branch information
rafamel committed Apr 3, 2021
1 parent b3f430c commit 2c939b0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/helpers/get-task-record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Task } from '../definitions';
import { fetch } from '../utils/fetch';
import { Empty, TypeGuard } from 'type-core';

export async function getTaskRecord(
tasks: string | Task.Record | Empty
): Promise<Task.Record> {
if (TypeGuard.isRecord(tasks)) {
return tasks;
}
if (TypeGuard.isString(tasks) || TypeGuard.isEmpty(tasks)) {
return fetch(tasks);
}
throw Error(`A valid task record or path to a tasks file must be provided`);
}
14 changes: 11 additions & 3 deletions src/tasks/reflection/lift.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Task, Context } from '../../definitions';
import { parseToRecord } from '../../helpers/parse';
import { getAbsolutePath } from '../../helpers/paths';
import { getTaskRecord } from '../../helpers/get-task-record';
import { isCancelled } from '../../utils/is-cancelled';
import { run } from '../../utils/run';
import { constants } from '../../constants';
import { select } from '../transform/select';
import { write } from '../filesystem/write';
import { print } from '../stdio/print';
import { shallow } from 'merge-strategies';
import { Members } from 'type-core';
import { Empty, Members } from 'type-core';
import { into } from 'pipettes';
import chalk from 'chalk';
import fs from 'fs-extra';
Expand All @@ -35,15 +36,22 @@ export interface LiftOptions {
/**
* Lifts all tasks on a `tasks` record to a package.json file,
* which is expected to be available at the context's working directory.
* The `tasks` argument can be a record itself, a string
* with the path of the tasks record, or empty to fetch
* it at the default path.
* @returns Task
*/
export function lift(tasks: Task.Record, options?: LiftOptions): Task.Async {
export function lift(
tasks: string | Task.Record | Empty,
options?: LiftOptions
): Task.Async {
return async (ctx: Context): Promise<void> => {
const opts = shallow(
{ purge: false, mode: 'default', bin: constants.bin },
options || undefined
);

const source = await getTaskRecord(tasks);
const pkgPath = getAbsolutePath('package.json', ctx);
const pkgExists = await fs.pathExists(pkgPath);
if (!pkgExists) {
Expand All @@ -54,7 +62,7 @@ export function lift(tasks: Task.Record, options?: LiftOptions): Task.Async {
const pkgScripts: Members<string> = pkg.scripts || {};

const taskScripts = into(
tasks,
source,
parseToRecord.bind(null, { include: null, exclude: null }),
(record) => Object.keys(record),
(keys) => {
Expand Down
11 changes: 7 additions & 4 deletions src/tasks/reflection/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Task, Context } from '../../definitions';
import { parseToArray } from '../../helpers/parse';
import { getTaskRecord } from '../../helpers/get-task-record';
import { constants } from '../../constants';
import { print } from '../stdio/print';
import { Empty } from 'type-core';
Expand All @@ -21,13 +22,15 @@ export interface ListOptions {
* @returns Task
*/
export function list(
tasks: Task.Record,
tasks?: Task.Record | Empty,
options?: ListOptions | Empty,
map?: (name: string, route: string[]) => string[]
): Task.Sync {
return (ctx: Context): void => {
): Task.Async {
return async (ctx: Context): Promise<void> => {
const opts = shallow({ bin: constants.bin }, options || undefined);
const items = parseToArray(tasks);

const source = await getTaskRecord(tasks);
const items = parseToArray(source);
const maxRouteLength = items.reduce(
(acc, item) => (acc > item.route.length ? acc : item.route.length),
0
Expand Down
7 changes: 4 additions & 3 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Task } from '../definitions';
import { find } from '../helpers/find';
import { constants } from '../constants';
import { Empty, TypeGuard } from 'type-core';
import path from 'path';

Expand All @@ -13,7 +14,7 @@ export interface FetchOptions {
* default export.
*/
export async function fetch(
file: string,
file?: string | Empty,
options?: FetchOptions | Empty,
cb?: (path: string) => void
): Promise<Task.Record> {
Expand All @@ -25,13 +26,13 @@ export async function fetch(
};

const filepath = await find({
file: file,
file: file || constants.file,
cwd: opts.dir,
exact: Boolean(options && options.dir)
});

if (!filepath) {
const filename = path.basename(file);
const filename = path.basename(file || constants.file);
throw Error(`File not found in path: ${filename}`);
}

Expand Down

0 comments on commit 2c939b0

Please sign in to comment.