From 444356f89695509cbd1bc3b33808622961da352a Mon Sep 17 00:00:00 2001 From: Phil Kedy Date: Fri, 20 Jan 2023 11:40:33 -0500 Subject: [PATCH] Fix to `apex ` & skip undefined tasks when multiple configs are parsed (#17) * Fix the default run path. If multiple configs are parsed, skip tasks that are not found instead of erroring. * Add --fail-undefined flag to `apex run`. --- apex.ts | 33 ++++++++++++++++++++++++++------- src/commands/run.ts | 30 ++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/apex.ts b/apex.ts index 4eede78..253b602 100644 --- a/apex.ts +++ b/apex.ts @@ -26,6 +26,7 @@ import * as describe from "./src/commands/describe.ts"; import * as watch from "./src/commands/watch.ts"; import * as run from "./src/commands/run.ts"; import { findApexConfig, setupLogger } from "./src/utils.ts"; +import { parseConfigYaml } from "./src/config.ts"; // Version bump this on release. const version = "v0.0.12"; @@ -75,17 +76,35 @@ if ( // Run target if defined in the config. const nonFlagArgs = args.filter((v) => !v.startsWith("-")); if (nonFlagArgs.length > 0 && !cli.getBaseCommand(args[0], true)) { - const configFile = findApexConfig(); - if (!configFile) { + const configPath = findApexConfig(); + if (!configPath) { console.log("could not find configuration"); Deno.exit(1); } - const targetMap = await run.loadTasks(configFile); - if (targetMap[args[0]]) { - await run.runTasks(configFile, targetMap, args); - Deno.exit(0); + let config; + try { + config = await Deno.readTextFile(configPath); + } catch (_e) { + log.error(`Could not read config ${configPath}`); + Deno.exit(1); + } + try { + const configs = parseConfigYaml(config); + for (const cfg of configs) { + const taskMap = await run.loadTasks(cfg); + await run.runTasks(cfg, taskMap, nonFlagArgs, configs.length == 1); + } + } catch (e) { + log.error(e); + Deno.exit(1); } + Deno.exit(0); } - await cli.parse(args); + try { + await cli.parse(args); + } catch (e) { + log.error(e); + Deno.exit(1); + } } diff --git a/src/commands/run.ts b/src/commands/run.ts index 2bfd353..ae35017 100644 --- a/src/commands/run.ts +++ b/src/commands/run.ts @@ -10,6 +10,7 @@ import { CmdOutput, Task } from "../task.ts"; export interface RunOptions { config?: string; quiet?: boolean; + failUndefined?: boolean; } export const command = new Command() @@ -23,6 +24,10 @@ export const command = new Command() "-q, --quiet", "silence extraneous apex output", ) + .option( + "--fail-undefined", + "when there are multiple configurations, force the command to fail if a task is not defined", + ) .description("Run tasks.") .action(async (options: RunOptions, tasks: string[]) => { const configFile = options.config || "apex.yaml"; @@ -36,12 +41,19 @@ export const command = new Command() config = await Deno.readTextFile(configPath); } catch (_e) { log.error(`Could not read config ${configPath}`); - return {}; + Deno.exit(1); } + const configs = parseConfigYaml(config); for (const cfg of configs) { const taskMap = await loadTasks(cfg); - await runTasks(cfg, taskMap, tasks, options); + await runTasks( + cfg, + taskMap, + tasks, + configs.length == 1 || options.failUndefined == true, + options, + ); } }); @@ -94,6 +106,7 @@ export async function runTasks( config: Configuration, taskMap: Record, tasks: string[] = [], + taskNotFoundError = true, opts: RunOptions = {}, ): Promise | undefined> { if (tasks.length === 0) { @@ -101,7 +114,7 @@ export async function runTasks( tasks = defaultTask ? [defaultTask] : []; } - if (!tasks.length) { + if (taskNotFoundError && !tasks.length) { log.error(`no tasks defined`); return; } @@ -109,7 +122,7 @@ export async function runTasks( const hasRun = new Set(); for (const t of tasks) { - await run(config, hasRun, taskMap, t, opts); + await run(config, hasRun, taskMap, t, taskNotFoundError, opts); } } @@ -118,6 +131,7 @@ async function run( hasRun: Set, taskMap: Record, task: string, + taskNotFoundError = true, opts: RunOptions = {}, ): Promise | undefined> { if (hasRun.has(task)) { @@ -134,11 +148,15 @@ async function run( return; } - throw new Error(`task not defined: "${task}"`); + if (taskNotFoundError) { + throw new Error(`task not defined: "${task}"`); + } else { + return; + } } for (const d of t.deps) { - await run(config, hasRun, taskMap, d, opts); + await run(config, hasRun, taskMap, d, taskNotFoundError, opts); } const env = {