Skip to content

Commit ef30f3c

Browse files
committed
Allow custom build command
1 parent 2df80cc commit ef30f3c

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

.changeset/pretty-tables-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Allow custom build command

packages/open-next/src/build.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ interface BuildOptions {
1010
minify?: boolean;
1111
debug?: boolean;
1212
appPath?: string;
13+
buildCommand?: string;
1314
}
1415

1516
const require = topLevelCreateRequire(import.meta.url);
@@ -57,6 +58,7 @@ function normalizeOptions(opts: BuildOptions) {
5758
tempDir: path.join(outputDir, ".build"),
5859
minify: opts.minify ?? Boolean(process.env.OPEN_NEXT_MINIFY) ?? false,
5960
debug: opts.debug ?? Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
61+
buildCommand: opts.buildCommand,
6062
};
6163
}
6264

@@ -107,18 +109,13 @@ function setStandaloneBuildMode(monorepoRoot: string) {
107109

108110
function buildNextjsApp(packager: "npm" | "yarn" | "pnpm") {
109111
const { appPath } = options;
110-
const result = cp.spawnSync(
111-
packager,
112-
packager === "npm" ? ["run", "build"] : ["build"],
113-
{
114-
stdio: "inherit",
115-
cwd: appPath,
116-
shell: true,
117-
}
118-
);
119-
if (result.status && result.status !== 0) {
120-
process.exit(1);
121-
}
112+
const command =
113+
options.buildCommand ??
114+
(packager === "npm" ? "npm run build" : `${packager} build`);
115+
cp.execSync(command, {
116+
stdio: "inherit",
117+
cwd: appPath,
118+
});
122119
}
123120

124121
function printHeader(header: string) {

packages/open-next/src/index.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,37 @@
33
import { build } from "./build.js";
44

55
const command = process.argv[2];
6+
if (command !== "build") printHelp();
67

7-
if (command === "build") {
8-
build();
9-
} else {
8+
const args = parseArgs();
9+
if (Object.keys(args).includes("--help")) printHelp();
10+
11+
build({
12+
buildCommand: args["--build-command"],
13+
});
14+
15+
function parseArgs() {
16+
return process.argv.slice(2).reduce((acc, key, ind, self) => {
17+
if (key.startsWith("--")) {
18+
if (self[ind + 1] && self[ind + 1].startsWith("-")) {
19+
acc[key] = undefined;
20+
} else if (self[ind + 1]) {
21+
acc[key] = self[ind + 1];
22+
} else if (!self[ind + 1]) {
23+
acc[key] = undefined;
24+
}
25+
}
26+
return acc;
27+
}, {} as Record<string, string | undefined>);
28+
}
29+
30+
function printHelp() {
1031
console.log("Unknown command");
1132
console.log("");
1233
console.log("Usage:");
1334
console.log(" npx open-next build");
35+
console.log(" npx open-next build --build-command 'npm run custom:build'");
1436
console.log("");
37+
38+
process.exit(1);
1539
}

0 commit comments

Comments
 (0)