Skip to content

Commit

Permalink
fix: nonzero return code when filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRomaa committed Feb 1, 2025
1 parent 09656ca commit 600b32c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
32 changes: 20 additions & 12 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { fromError } from "zod-validation-error";

import { description, version } from "./package.json";
import { machBuild, machWatch } from "./src/mach";
import { type MachArgs, MachArgsSchema, MachConfigSchema } from "./src/types";
import { MachArgsSchema, MachConfigSchema } from "./src/types";

const CONFIG_FILENAMES = ["mach.config.js", "mach.config.ts"];

Expand Down Expand Up @@ -95,39 +95,47 @@ cli.name("mach").version(version).description(description);

commandWithOptions("build")
.description("compile instruments specified in configuration file")
.action((args: MachArgs) => {
.action((args) => {
const parsedArgs = MachArgsSchema.parse(args);
const numInstruments = parsedArgs.config.instruments.length;
const instruments = parsedArgs.filter
? parsedArgs.config.instruments.filter((instrument) => parsedArgs.filter!.test(instrument.name))
: parsedArgs.config.instruments;

signale.start(`Building ${numInstruments} instruments\n`);
signale.start(`Building ${instruments.length} instrument${instruments.length !== 1 ? "s" : ""}\n`);

const startTime = performance.now();
machBuild(parsedArgs).then((results) => {
machBuild(instruments, parsedArgs).then((results) => {
const stopTime = performance.now();

const numSuccess = results.filter(({ status }) => status === "fulfilled").length;
const numFailed = instruments.length - numSuccess;

if (numSuccess > 0) {
signale.success(
`Built ${numSuccess} instruments in`,
`Built ${numSuccess} instrument${instruments.length !== 1 ? "s" : ""} in`,
chalk.greenBright(`${(stopTime - startTime).toFixed()} ms`),
"\n",
);
} else {
signale.error(`All ${numInstruments} instruments failed to build`);
}

if (numSuccess < numInstruments) {
if (numFailed > 0) {
signale.error(`${instruments.length} instrument${instruments.length !== 1 ? "s" : ""} failed to build`);
process.exit(1);
}
});
});

commandWithOptions("watch")
.description("watch instruments for changes and re-compile bundles when updated")
.action((args: MachArgs) => {
machWatch(MachArgsSchema.parse(args)).then((results) => {
.action((args) => {
const parsedArgs = MachArgsSchema.parse(args);
const instruments = parsedArgs.filter
? parsedArgs.config.instruments.filter((instrument) => parsedArgs.filter!.test(instrument.name))
: args.config.instruments;

machWatch(instruments, parsedArgs).then((results) => {
if (results.some(({ status }) => status === "rejected")) {
signale.error("Watch mode requires a build-able bundle to initialize");
signale.error("Watch mode requires a successful build to initialize");
process.exit(1);
}

Expand Down
18 changes: 10 additions & 8 deletions src/mach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

import { buildInstrument, watchInstrument } from "./esbuild";
import { BuildLogger } from "./logger";
import type { BuildResultWithMeta, MachArgs } from "./types";
import type { BuildResultWithMeta, Instrument, MachArgs } from "./types";

/**
* Run a one-off build with provided arguments.
* Run a one-off build with provided instruments and arguments.
* @returns List of all build results.
*/
export async function machBuild(args: MachArgs): Promise<PromiseSettledResult<BuildResultWithMeta>[]> {
const instruments = args.config.instruments.filter((instrument) => args.filter?.test(instrument.name) ?? true);

export async function machBuild(
instruments: Instrument[],
args: MachArgs,
): Promise<PromiseSettledResult<BuildResultWithMeta>[]> {
return Promise.allSettled(
instruments.map((instrument) =>
buildInstrument(args, instrument, new BuildLogger(instrument.name, args.verbose)),
Expand All @@ -25,9 +26,10 @@ export async function machBuild(args: MachArgs): Promise<PromiseSettledResult<Bu
* Continuously build instruments when files are updated.
* @returns list of initial build results.
*/
export async function machWatch(args: MachArgs): Promise<PromiseSettledResult<BuildResultWithMeta>[]> {
const instruments = args.config.instruments.filter((instrument) => args.filter?.test(instrument.name) ?? true);

export async function machWatch(
instruments: Instrument[],
args: MachArgs,
): Promise<PromiseSettledResult<BuildResultWithMeta>[]> {
return Promise.allSettled(
instruments.map((instrument) =>
watchInstrument(args, instrument, new BuildLogger(instrument.name, args.verbose)),
Expand Down

0 comments on commit 600b32c

Please sign in to comment.