Skip to content

Commit

Permalink
Fix to apex <tasks...> & skip undefined tasks when multiple configs…
Browse files Browse the repository at this point in the history
… 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`.
  • Loading branch information
pkedy authored Jan 20, 2023
1 parent cb02e15 commit 444356f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
33 changes: 26 additions & 7 deletions apex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}
30 changes: 24 additions & 6 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CmdOutput, Task } from "../task.ts";
export interface RunOptions {
config?: string;
quiet?: boolean;
failUndefined?: boolean;
}

export const command = new Command()
Expand All @@ -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";
Expand All @@ -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,
);
}
});

Expand Down Expand Up @@ -94,22 +106,23 @@ export async function runTasks(
config: Configuration,
taskMap: Record<string, Task>,
tasks: string[] = [],
taskNotFoundError = true,
opts: RunOptions = {},
): Promise<Record<string, CmdOutput> | undefined> {
if (tasks.length === 0) {
const defaultTask = Object.keys(taskMap).shift();
tasks = defaultTask ? [defaultTask] : [];
}

if (!tasks.length) {
if (taskNotFoundError && !tasks.length) {
log.error(`no tasks defined`);
return;
}

const hasRun = new Set<string>();

for (const t of tasks) {
await run(config, hasRun, taskMap, t, opts);
await run(config, hasRun, taskMap, t, taskNotFoundError, opts);
}
}

Expand All @@ -118,6 +131,7 @@ async function run(
hasRun: Set<string>,
taskMap: Record<string, Task>,
task: string,
taskNotFoundError = true,
opts: RunOptions = {},
): Promise<Record<string, CmdOutput> | undefined> {
if (hasRun.has(task)) {
Expand All @@ -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 = {
Expand Down

0 comments on commit 444356f

Please sign in to comment.