From 2c939b0e600a31cd3f3f08c3ca1958a7295afc3f Mon Sep 17 00:00:00 2001 From: Rafa Mel Date: Sat, 3 Apr 2021 12:56:42 +0200 Subject: [PATCH] feat: lift and list tasks can fetch default tasks file; fetch will use the default tasks file name when none is provided --- src/helpers/get-task-record.ts | 15 +++++++++++++++ src/tasks/reflection/lift.ts | 14 +++++++++++--- src/tasks/reflection/list.ts | 11 +++++++---- src/utils/fetch.ts | 7 ++++--- 4 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 src/helpers/get-task-record.ts diff --git a/src/helpers/get-task-record.ts b/src/helpers/get-task-record.ts new file mode 100644 index 0000000..b5788a6 --- /dev/null +++ b/src/helpers/get-task-record.ts @@ -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 { + 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`); +} diff --git a/src/tasks/reflection/lift.ts b/src/tasks/reflection/lift.ts index 0a20563..83a715a 100644 --- a/src/tasks/reflection/lift.ts +++ b/src/tasks/reflection/lift.ts @@ -1,6 +1,7 @@ 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'; @@ -8,7 +9,7 @@ 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'; @@ -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 => { 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) { @@ -54,7 +62,7 @@ export function lift(tasks: Task.Record, options?: LiftOptions): Task.Async { const pkgScripts: Members = pkg.scripts || {}; const taskScripts = into( - tasks, + source, parseToRecord.bind(null, { include: null, exclude: null }), (record) => Object.keys(record), (keys) => { diff --git a/src/tasks/reflection/list.ts b/src/tasks/reflection/list.ts index 63aca51..5be493d 100644 --- a/src/tasks/reflection/list.ts +++ b/src/tasks/reflection/list.ts @@ -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'; @@ -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 => { 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 diff --git a/src/utils/fetch.ts b/src/utils/fetch.ts index 181c0ce..5dea4c2 100644 --- a/src/utils/fetch.ts +++ b/src/utils/fetch.ts @@ -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'; @@ -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 { @@ -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}`); }