Skip to content

Commit

Permalink
fix(cli): print the command being invoked during build (#3325)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Sep 3, 2024
1 parent f66c44d commit 4aa8e55
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-pumpkins-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/cli": patch
---

Print the command being invoked during build
29 changes: 8 additions & 21 deletions packages/cli/src/build/android.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
import type { Config } from "@react-native-community/cli-types";
import ora from "ora";
import type { AndroidBuildParams } from "./types";
import { watch } from "./watcher";

export async function buildAndroid(
export type BuildResult = string | number | null;

export function buildAndroid(
config: Config,
buildParams: AndroidBuildParams,
logger = ora()
): Promise<string | number | null> {
): Promise<BuildResult> {
const { sourceDir } = config.project.android ?? {};
if (!sourceDir) {
logger.fail("No Android project was found");
process.exitCode = 1;
return null;
return Promise.resolve(null);
}

return import("@rnx-kit/tools-android").then(({ assemble }) => {
return new Promise((resolve) => {
logger.start("Building");

const errors: Buffer[] = [];
return new Promise<BuildResult>((resolve) => {
const gradle = assemble(sourceDir, buildParams);

gradle.stdout.on("data", () => (logger.text += "."));
gradle.stderr.on("data", (data) => errors.push(data));

gradle.on("close", (code) => {
if (code === 0) {
logger.succeed("Build succeeded");
resolve(sourceDir);
} else {
logger.fail(Buffer.concat(errors).toString());
process.exitCode = code ?? 1;
resolve(code);
}
});
watch(gradle, logger, () => resolve(sourceDir), resolve);
});
});
}
24 changes: 4 additions & 20 deletions packages/cli/src/build/apple.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Ora } from "ora";
import type { AppleBuildParams } from "./types";
import { watch } from "./watcher";

export type BuildArgs = {
xcworkspace: string;
Expand All @@ -15,28 +16,11 @@ export function runBuild(
): Promise<BuildResult> {
return import("@rnx-kit/tools-apple").then(({ xcodebuild }) => {
return new Promise<BuildResult>((resolve) => {
logger.start("Building");

const errors: Buffer[] = [];
const proc = xcodebuild(xcworkspace, buildParams, (text) => {
const current = logger.text;
const onSuccess = () => resolve({ xcworkspace, args: build.spawnargs });
const build = xcodebuild(xcworkspace, buildParams, (text) => {
logger.info(text);
logger.start(current);
});

proc.stdout.on("data", () => (logger.text += "."));
proc.stderr.on("data", (data) => errors.push(data));

proc.on("close", (code) => {
if (code === 0) {
logger.succeed("Build succeeded");
resolve({ xcworkspace, args: proc.spawnargs });
} else {
logger.fail(Buffer.concat(errors).toString());
process.exitCode = code ?? 1;
resolve(code);
}
});
watch(build, logger, onSuccess, resolve);
});
});
}
28 changes: 28 additions & 0 deletions packages/cli/src/build/watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { ChildProcessWithoutNullStreams } from "node:child_process";
import type { Ora } from "ora";

export function watch(
subproc: ChildProcessWithoutNullStreams,
logger: Ora,
onSuccess: () => void,
onFail: (exitCode: number | null) => void
) {
subproc.stdout.on("data", () => (logger.text += "."));

const errors: Buffer[] = [];
subproc.stderr.on("data", (data) => errors.push(data));

subproc.on("close", (code) => {
if (code === 0) {
logger.succeed("Build succeeded");
onSuccess();
} else {
logger.fail(Buffer.concat(errors).toString());
process.exitCode = code ?? 1;
onFail(code);
}
});

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

0 comments on commit 4aa8e55

Please sign in to comment.