7
7
getLambdaOptionsFromFunction,
8
8
getNodeVersion,
9
9
getSpawnOptions,
10
+ getScriptName,
10
11
glob,
11
12
runNpmInstall,
12
13
runPackageJsonScript,
@@ -35,13 +36,7 @@ import {
35
36
import { nodeFileTrace , NodeFileTraceReasons } from '@zeit/node-file-trace' ;
36
37
import { ChildProcess , fork } from 'child_process' ;
37
38
import escapeStringRegexp from 'escape-string-regexp' ;
38
- import {
39
- lstat ,
40
- pathExists ,
41
- readFile ,
42
- unlink as unlinkFile ,
43
- writeFile ,
44
- } from 'fs-extra' ;
39
+ import { lstat , pathExists , readFile , remove , writeFile } from 'fs-extra' ;
45
40
import os from 'os' ;
46
41
import path from 'path' ;
47
42
import resolveFrom from 'resolve-from' ;
@@ -108,13 +103,7 @@ const MAX_AGE_ONE_YEAR = 31536000;
108
103
/**
109
104
* Read package.json from files
110
105
*/
111
- async function readPackageJson (
112
- entryPath : string
113
- ) : Promise < {
114
- scripts ?: { [ key : string ] : string } ;
115
- dependencies ?: { [ key : string ] : string } ;
116
- devDependencies ?: { [ key : string ] : string } ;
117
- } > {
106
+ async function readPackageJson ( entryPath : string ) : Promise < PackageJson > {
118
107
const packagePath = path . join ( entryPath , 'package.json' ) ;
119
108
120
109
try {
@@ -329,49 +318,38 @@ export const build = async ({
329
318
}
330
319
331
320
const isLegacy = nextVersionRange && isLegacyNext ( nextVersionRange ) ;
332
- let shouldRunScript = 'now-build' ;
333
-
334
321
debug ( `MODE: ${ isLegacy ? 'legacy' : 'serverless' } ` ) ;
335
322
336
323
if ( isLegacy ) {
337
- try {
338
- await unlinkFile ( path . join ( entryPath , 'yarn.lock' ) ) ;
339
- } catch ( err ) {
340
- debug ( 'no yarn.lock removed' ) ;
341
- }
342
-
343
- try {
344
- await unlinkFile ( path . join ( entryPath , 'package-lock.json' ) ) ;
345
- } catch ( err ) {
346
- debug ( 'no package-lock.json removed' ) ;
347
- }
348
-
349
324
console . warn (
350
325
"WARNING: your application is being deployed in @vercel/next's legacy mode. http://err.sh/vercel/vercel/now-next-legacy-mode"
351
326
) ;
352
327
328
+ await Promise . all ( [
329
+ remove ( path . join ( entryPath , 'yarn.lock' ) ) ,
330
+ remove ( path . join ( entryPath , 'package-lock.json' ) ) ,
331
+ ] ) ;
332
+
353
333
debug ( 'Normalizing package.json' ) ;
354
334
const packageJson = normalizePackageJson ( pkg ) ;
355
335
debug ( 'Normalized package.json result: ' , packageJson ) ;
356
336
await writePackageJson ( entryPath , packageJson ) ;
357
- } else if ( pkg . scripts && pkg . scripts [ 'now-build' ] ) {
358
- debug ( 'Found user `now-build` script' ) ;
359
- shouldRunScript = 'now-build' ;
360
- } else if ( pkg . scripts && pkg . scripts [ 'build' ] ) {
361
- debug ( 'Found user `build` script' ) ;
362
- shouldRunScript = 'build' ;
363
- } else if ( ! pkg . scripts || ! pkg . scripts [ 'now-build' ] ) {
364
- debug (
337
+ }
338
+
339
+ const buildScriptName = getScriptName ( pkg , [
340
+ 'vercel-build' ,
341
+ 'now-build' ,
342
+ 'build' ,
343
+ ] ) ;
344
+ let { buildCommand } = config ;
345
+
346
+ if ( ! buildScriptName && ! buildCommand ) {
347
+ console . log (
365
348
'Your application is being built using `next build`. ' +
366
- 'If you need to define a different build step, please create a `now -build` script in your `package.json` ' +
367
- '(e.g. `{ "scripts": { "now -build": "npm run prepare && next build" } }`).'
349
+ 'If you need to define a different build step, please create a `vercel -build` script in your `package.json` ' +
350
+ '(e.g. `{ "scripts": { "vercel -build": "npm run prepare && next build" } }`).'
368
351
) ;
369
- pkg . scripts = {
370
- 'now-build' : 'next build' ,
371
- ...( pkg . scripts || { } ) ,
372
- } ;
373
- shouldRunScript = 'now-build' ;
374
- await writePackageJson ( entryPath , pkg ) ;
352
+ buildCommand = 'next build' ;
375
353
}
376
354
377
355
if ( process . env . NPM_AUTH_TOKEN ) {
@@ -398,12 +376,11 @@ export const build = async ({
398
376
await createServerlessConfig ( workPath , entryPath , nextVersion ) ;
399
377
}
400
378
401
- debug ( 'Running user script...' ) ;
402
379
const memoryToConsume = Math . floor ( os . totalmem ( ) / 1024 ** 2 ) - 128 ;
403
380
const env : { [ key : string ] : string | undefined } = { ...spawnOpts . env } ;
404
381
env . NODE_OPTIONS = `--max_old_space_size=${ memoryToConsume } ` ;
405
382
406
- if ( config . buildCommand ) {
383
+ if ( buildCommand ) {
407
384
// Add `node_modules/.bin` to PATH
408
385
const nodeBinPath = await getNodeBinPath ( { cwd : entryPath } ) ;
409
386
env . PATH = `${ nodeBinPath } ${ path . delimiter } ${ env . PATH } ` ;
@@ -412,14 +389,14 @@ export const build = async ({
412
389
`Added "${ nodeBinPath } " to PATH env because a build command was used.`
413
390
) ;
414
391
415
- console . log ( `Running "${ config . buildCommand } "` ) ;
416
- await execCommand ( config . buildCommand , {
392
+ console . log ( `Running "${ buildCommand } "` ) ;
393
+ await execCommand ( buildCommand , {
417
394
...spawnOpts ,
418
395
cwd : entryPath ,
419
396
env,
420
397
} ) ;
421
- } else {
422
- await runPackageJsonScript ( entryPath , shouldRunScript , {
398
+ } else if ( buildScriptName ) {
399
+ await runPackageJsonScript ( entryPath , buildScriptName , {
423
400
...spawnOpts ,
424
401
env,
425
402
} ) ;
@@ -667,7 +644,7 @@ export const build = async ({
667
644
}
668
645
669
646
if ( process . env . NPM_AUTH_TOKEN ) {
670
- await unlinkFile ( path . join ( entryPath , '.npmrc' ) ) ;
647
+ await remove ( path . join ( entryPath , '.npmrc' ) ) ;
671
648
}
672
649
673
650
const pageLambdaRoutes : Route [ ] = [ ] ;
0 commit comments