Skip to content

Commit

Permalink
feat: return build result from mach functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRomaa committed Jun 7, 2024
1 parent 20fcddc commit 99249da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
8 changes: 6 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ cli.name("mach").version(version).description(description);

commandWithOptions("build")
.description("compile instruments specified in configuration file")
.action((args: MachArgs) => machBuild(MachArgsSchema.parse(args)));
.action((args: MachArgs) => {
machBuild(MachArgsSchema.parse(args));
});

commandWithOptions("watch")
.description("watch instruments for changes and re-compile bundles when updated")
.action((args: MachArgs) => machWatch(MachArgsSchema.parse(args)));
.action((args: MachArgs) => {
machWatch(MachArgsSchema.parse(args));
});

cli.parse();

Expand Down
25 changes: 19 additions & 6 deletions src/mach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import signale from "signale";

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

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

signale.start(`Building ${instruments.length} instruments\n`);

const startTime = performance.now();
Promise.all(

return Promise.all(
instruments.map(async (instrument) => {
const result = await buildInstrument(args, instrument, new BuildLogger(instrument.name, args.verbose));
result.rebuild?.dispose();
Expand All @@ -37,21 +42,29 @@ export async function machBuild(args: MachArgs) {
if (successCount < instruments.length) {
process.exit(1);
}

return results;
});
}

export async function machWatch(args: MachArgs) {
/**
* Continuously build instruments when files are updated.
* @returns list of initial build results.
*/
export async function machWatch(args: MachArgs): Promise<BuildResultWithMeta[]> {
const instruments = args.config.instruments.filter((instrument) => args.filter?.test(instrument.name) ?? true);

Promise.all(
return Promise.all(
instruments.map((instrument) =>
watchInstrument(args, instrument, new BuildLogger(instrument.name, args.verbose)),
watchInstrument(args, instrument, new BuildLogger(instrument.name, args.verbose), false),
),
).then((results) => {
if (results.some((res) => res.errors.length > 0)) {
signale.error("Watch mode requires a build-able bundle to initialize");
process.exit(1);
}
signale.watch("Watching for changes\n");

return results;
});
}

0 comments on commit 99249da

Please sign in to comment.