Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Sep 12, 2024
1 parent e610dd7 commit 3ebdb58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
6 changes: 2 additions & 4 deletions packages/cli/src/build/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ export function buildAndroid(
}

return import("@rnx-kit/tools-android").then(({ assemble }) => {
return new Promise<BuildResult>((resolve) => {
const gradle = assemble(sourceDir, buildParams);
watch(gradle, logger, () => resolve(sourceDir), resolve);
});
const gradle = assemble(sourceDir, buildParams);
return watch(gradle, logger, () => sourceDir);
});
}
9 changes: 3 additions & 6 deletions packages/cli/src/build/apple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ export function runBuild(
logger: Ora
): Promise<BuildResult> {
return import("@rnx-kit/tools-apple").then(({ xcodebuild }) => {
return new Promise<BuildResult>((resolve) => {
const log = (message: string) => logger.info(message);
const build = xcodebuild(xcworkspace, buildParams, log);
const onSuccess = () => resolve({ xcworkspace, args: build.spawnargs });
watch(build, logger, onSuccess, resolve);
});
const log = (message: string) => logger.info(message);
const build = xcodebuild(xcworkspace, buildParams, log);
return watch(build, logger, () => ({ xcworkspace, args: build.spawnargs }));
});
}
49 changes: 26 additions & 23 deletions packages/cli/src/build/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import type { ChildProcessWithoutNullStreams } from "node:child_process";
import type { Ora } from "ora";

export function watch(
export type ExitCode = number | null;

export function watch<T>(
subproc: ChildProcessWithoutNullStreams,
logger: Ora,
onSuccess: () => void,
onFail: (exitCode: number | null) => void
onSuccess: () => T
) {
const errors: Buffer[] = [];

const isCI = Boolean(process.env.CI);
if (isCI) {
subproc.stdout.on("data", (chunk) => process.stdout.write(chunk));
subproc.stderr.on("data", (chunk) => process.stderr.write(chunk));
} else {
subproc.stdout.on("data", () => (logger.text += "."));
subproc.stderr.on("data", (data) => errors.push(data));
}
return new Promise<T | ExitCode>((resolve) => {
const errors: Buffer[] = [];

subproc.on("close", (code) => {
if (code === 0) {
logger.succeed("Build succeeded");
onSuccess();
const isCI = Boolean(process.env.CI);
if (isCI) {
subproc.stdout.on("data", (chunk) => process.stdout.write(chunk));
subproc.stderr.on("data", (chunk) => process.stderr.write(chunk));
} else {
logger.fail(Buffer.concat(errors).toString());
process.exitCode = code ?? 1;
onFail(code);
subproc.stdout.on("data", () => (logger.text += "."));
subproc.stderr.on("data", (data) => errors.push(data));
}
});

logger.info(`Command: ${subproc.spawnargs.join(" ")}`);
logger.start("Building");
subproc.on("close", (code) => {
if (code === 0) {
logger.succeed("Build succeeded");
resolve(onSuccess());
} else {
logger.fail(Buffer.concat(errors).toString());
process.exitCode = code ?? 1;
resolve(code);
}
});

logger.info(`Command: ${subproc.spawnargs.join(" ")}`);
logger.start("Building");
});
}

0 comments on commit 3ebdb58

Please sign in to comment.