diff --git a/packages/next/src/executors/build/build.impl.ts b/packages/next/src/executors/build/build.impl.ts index 7249ea933460a..199a58cff16ce 100644 --- a/packages/next/src/executors/build/build.impl.ts +++ b/packages/next/src/executors/build/build.impl.ts @@ -50,7 +50,7 @@ export default async function buildExecutor( const { experimentalAppOnly, profile, debug } = options; const args = createCliOptions({ experimentalAppOnly, profile, debug }); - const command = `npx next build ${args}`; + const command = `npx next build ${args.join(' ')}`; const execSyncOptions: ExecSyncOptions = { stdio: 'inherit', encoding: 'utf-8', diff --git a/packages/next/src/executors/server/server.impl.ts b/packages/next/src/executors/server/server.impl.ts index e17f4d76f4ed9..6532433927dd5 100644 --- a/packages/next/src/executors/server/server.impl.ts +++ b/packages/next/src/executors/server/server.impl.ts @@ -12,7 +12,7 @@ import { NextBuildBuilderOptions, NextServeBuilderOptions, } from '../../utils/types'; -import { spawn } from 'child_process'; +import { fork } from 'child_process'; import customServer from './custom-server.impl'; import { createCliOptions } from '../../utils/create-cli-options'; import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable'; @@ -49,13 +49,13 @@ export default async function* serveExecutor( const mode = options.dev ? 'dev' : 'start'; const turbo = options.turbo && options.dev ? '--turbo' : ''; - const command = `npx next ${mode} ${args} ${turbo}`; + const nextBin = require.resolve('next/dist/bin/next'); + yield* createAsyncIterable<{ success: boolean; baseUrl: string }>( async ({ done, next, error }) => { - const server = spawn(command, { + const server = fork(nextBin, [mode, ...args, turbo], { cwd: options.dev ? root : nextDir, stdio: 'inherit', - shell: true, }); server.once('exit', (code) => { @@ -66,15 +66,15 @@ export default async function* serveExecutor( } }); - process.on('exit', async (code) => { - if (code === 128 + 2) { - server.kill('SIGINT'); - } else if (code === 128 + 1) { - server.kill('SIGHUP'); - } else { + const killServer = () => { + if (server.connected) { server.kill('SIGTERM'); } - }); + }; + process.on('exit', () => killServer()); + process.on('SIGINT', () => killServer()); + process.on('SIGTERM', () => killServer()); + process.on('SIGHUP', () => killServer()); await waitForPortOpen(port); diff --git a/packages/next/src/utils/create-cli-options.ts b/packages/next/src/utils/create-cli-options.ts index 41c86b3a14391..41569a4a84c1e 100644 --- a/packages/next/src/utils/create-cli-options.ts +++ b/packages/next/src/utils/create-cli-options.ts @@ -1,12 +1,11 @@ -export function createCliOptions(obj: { [key: string]: any }): string { - return Object.entries(obj) - .reduce((arr, [key, value]) => { - if (value !== undefined) { - const kebabCase = key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase()); - return `${arr}--${kebabCase}=${value} `; - } else { - return arr; - } - }, '') - .trim(); +export function createCliOptions( + obj: Record +): string[] { + return Object.entries(obj).reduce((arr, [key, value]) => { + if (value !== undefined) { + const kebabCase = key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase()); + arr.push(`--${kebabCase}=${value}`); + } + return arr; + }, []); }