@@ -3,14 +3,20 @@ import 'dotenv/config';
3
3
import {
4
4
detectPackageManager ,
5
5
ExecutorContext ,
6
+ getPackageManagerCommand ,
6
7
logger ,
7
8
readJsonFile ,
8
- workspaceRoot ,
9
9
writeJsonFile ,
10
10
} from '@nx/devkit' ;
11
11
import { createLockFile , createPackageJson , getLockFileName } from '@nx/js' ;
12
- import { join , resolve as pathResolve } from 'path' ;
13
- import { copySync , existsSync , mkdir , writeFileSync } from 'fs-extra' ;
12
+ import { join } from 'path' ;
13
+ import {
14
+ copySync ,
15
+ ensureDirSync ,
16
+ existsSync ,
17
+ mkdir ,
18
+ writeFileSync ,
19
+ } from 'fs-extra' ;
14
20
import { gte } from 'semver' ;
15
21
import { directoryExists } from '@nx/workspace/src/utilities/fileutils' ;
16
22
import { checkAndCleanWithSemver } from '@nx/devkit/src/utils/semver' ;
@@ -19,15 +25,15 @@ import { updatePackageJson } from './lib/update-package-json';
19
25
import { createNextConfigFile } from './lib/create-next-config-file' ;
20
26
import { checkPublicDirectory } from './lib/check-project' ;
21
27
import { NextBuildBuilderOptions } from '../../utils/types' ;
22
- import { ChildProcess , fork } from 'child_process' ;
28
+ import { execSync , ExecSyncOptions } from 'child_process' ;
23
29
import { createCliOptions } from '../../utils/create-cli-options' ;
24
30
25
- let childProcess : ChildProcess ;
26
-
27
31
// This executor is a modified version of the original `@nrwl/next:build` executor.
28
32
// It's main modification is to use the cloudflare next-on-pages package.
29
33
// Because the Cloudflare builder doesn't allow us to locate the build output outside the project root directory
30
34
// we need to change the output path config options to the project root directory and then copy the build output to the desired output path.
35
+ // We also move from invoking the bin script to executing the command with the `execSync` function. This is because the bin script
36
+ // is not exported on the package.json
31
37
export default async function buildExecutor (
32
38
options : NextBuildBuilderOptions ,
33
39
context : ExecutorContext
@@ -54,21 +60,18 @@ export default async function buildExecutor(
54
60
if ( hasReact18 ) {
55
61
process . env [ '__NEXT_REACT_ROOT' ] ||= 'true' ;
56
62
}
63
+ ensureDirSync ( join ( projectRoot , '.vercel' ) ) ;
57
64
58
65
const { outputPath : originalOutputPath } = options ;
59
66
// Set the outputPath to the projectRoot to bypass cloudflare builded limitations.
60
67
options . outputPath = projectRoot ;
61
68
62
69
try {
63
- await runCliBuild ( workspaceRoot , projectRoot , options ) ;
70
+ runCliBuild ( projectRoot , options ) ;
64
71
} catch ( error ) {
65
72
logger . error ( `Error occurred while trying to run the build command` ) ;
66
73
logger . error ( error ) ;
67
74
return { success : false } ;
68
- } finally {
69
- if ( childProcess ) {
70
- childProcess . kill ( ) ;
71
- }
72
75
}
73
76
74
77
if ( ! directoryExists ( options . outputPath ) ) {
@@ -130,43 +133,24 @@ export default async function buildExecutor(
130
133
return { success : true } ;
131
134
}
132
135
133
- function runCliBuild (
134
- workspaceRoot : string ,
135
- projectRoot : string ,
136
- options : NextBuildBuilderOptions
137
- ) {
136
+ function runCliBuild ( projectRoot : string , options : NextBuildBuilderOptions ) {
138
137
const { experimentalAppOnly, profile, debug, outputPath } = options ;
139
138
140
139
// Set output path here since it can also be set via CLI
141
140
// We can retrieve it inside plugins/with-nx
142
141
process . env . NX_NEXT_OUTPUT_PATH ??= outputPath ;
143
142
144
143
const args = createCliOptions ( { experimentalAppOnly, profile, debug } ) ;
145
- return new Promise ( ( resolve , reject ) => {
146
- childProcess = fork (
147
- require . resolve ( '@cloudflare/next-on-pages/bin' ) ,
148
- [ ...args ] ,
149
- {
150
- cwd : pathResolve ( workspaceRoot , projectRoot ) ,
151
- stdio : 'inherit' ,
152
- env : process . env ,
153
- }
154
- ) ;
155
144
156
- // Ensure the child process is killed when the parent exits
157
- process . on ( 'exit' , ( ) => childProcess . kill ( ) ) ;
158
- process . on ( 'SIGTERM' , ( ) => childProcess . kill ( ) ) ;
159
-
160
- childProcess . on ( 'error' , ( err ) => {
161
- reject ( err ) ;
162
- } ) ;
145
+ const pmc = getPackageManagerCommand ( 'npm' ) ;
146
+ const execSyncOptions : ExecSyncOptions = {
147
+ stdio : [ 'inherit' , 'inherit' , 'inherit' ] ,
148
+ encoding : 'utf-8' ,
149
+ cwd : projectRoot ,
150
+ } ;
163
151
164
- childProcess . on ( 'exit' , ( code ) => {
165
- if ( code === 0 ) {
166
- resolve ( code ) ;
167
- } else {
168
- reject ( code ) ;
169
- }
170
- } ) ;
171
- } ) ;
152
+ execSync (
153
+ `${ pmc . exec } @cloudflare/next-on-pages ${ args . join ( ' ' ) } ` ,
154
+ execSyncOptions
155
+ ) ;
172
156
}
0 commit comments