2018-12-15 22:55:59 +01:00
# ! / u s r / b i n / e n v n o d e
2020-05-04 18:21:58 +02:00
import * as log from '../build/output/log'
2019-03-24 23:38:03 +01:00
import arg from 'next/dist/compiled/arg/index.js'
2020-05-04 18:21:58 +02:00
import { NON_STANDARD_NODE_ENV } from '../lib/constants'
2020-05-18 21:24:37 +02:00
; [ 'react' , 'react-dom' ] . forEach ( ( dependency ) = > {
2018-12-15 22:55:59 +01:00
try {
// When 'npm link' is used it checks the clone location. Not the project.
require . resolve ( dependency )
} catch ( err ) {
2019-05-29 13:57:26 +02:00
console . warn (
2020-01-27 14:35:12 +01:00
` The module ' ${ dependency } ' was not found. Next.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install ${ dependency } ' `
2019-05-29 13:57:26 +02:00
)
2018-12-15 22:55:59 +01:00
}
} )
const defaultCommand = 'dev'
2019-02-26 21:57:32 +01:00
export type cliCommand = ( argv? : string [ ] ) = > void
2019-05-29 13:57:26 +02:00
const commands : { [ command : string ] : ( ) = > Promise < cliCommand > } = {
2021-10-16 14:22:42 +02:00
build : ( ) = > Promise . resolve ( require ( '../cli/next-build' ) . nextBuild ) ,
start : ( ) = > Promise . resolve ( require ( '../cli/next-start' ) . nextStart ) ,
export : ( ) = > Promise . resolve ( require ( '../cli/next-export' ) . nextExport ) ,
dev : ( ) = > Promise . resolve ( require ( '../cli/next-dev' ) . nextDev ) ,
lint : ( ) = > Promise . resolve ( require ( '../cli/next-lint' ) . nextLint ) ,
telemetry : ( ) = >
Promise . resolve ( require ( '../cli/next-telemetry' ) . nextTelemetry ) ,
2022-01-04 16:35:32 +01:00
info : ( ) = > Promise . resolve ( require ( '../cli/next-info' ) . nextInfo ) ,
2019-02-26 21:57:32 +01:00
}
2018-12-15 22:55:59 +01:00
2019-05-29 13:57:26 +02:00
const args = arg (
{
// Types
'--version' : Boolean ,
'--help' : Boolean ,
'--inspect' : Boolean ,
// Aliases
'-v' : '--version' ,
'-h' : '--help' ,
} ,
{
permissive : true ,
}
)
2018-12-15 22:55:59 +01:00
// Version is inlined into the file using taskr build pipeline
if ( args [ '--version' ] ) {
2019-02-15 17:49:40 +01:00
console . log ( ` Next.js v ${ process . env . __NEXT_VERSION } ` )
2018-12-15 22:55:59 +01:00
process . exit ( 0 )
}
// Check if we are running `next <subcommand>` or `next`
2019-02-26 21:57:32 +01:00
const foundCommand = Boolean ( commands [ args . _ [ 0 ] ] )
2018-12-15 22:55:59 +01:00
2020-08-20 15:32:55 +02:00
// Makes sure the `next --help` case is covered
2018-12-15 22:55:59 +01:00
// This help message is only showed for `next --help`
2020-08-20 15:32:55 +02:00
// `next <subcommand> --help` falls through to be handled later
2018-12-15 22:55:59 +01:00
if ( ! foundCommand && args [ '--help' ] ) {
console . log ( `
Usage
$ next < command >
Available commands
2019-02-26 21:57:32 +01:00
$ { Object . keys ( commands ) . join ( ', ' ) }
2018-12-31 14:44:27 +01:00
2018-12-15 22:55:59 +01:00
Options
2019-08-18 23:50:20 +02:00
-- version , - v Version number
2018-12-31 14:44:27 +01:00
-- help , - h Displays this message
2018-12-15 22:55:59 +01:00
For more information run a command with the -- help flag
$ next build -- help
` )
process . exit ( 0 )
}
2019-02-17 20:13:10 +01:00
const command = foundCommand ? args . _ [ 0 ] : defaultCommand
const forwardedArgs = foundCommand ? args . _ . slice ( 1 ) : args . _
2018-12-15 22:55:59 +01:00
2019-05-29 13:57:26 +02:00
if ( args [ '--inspect' ] )
throw new Error (
2020-03-04 11:53:20 +01:00
` --inspect flag is deprecated. Use env variable NODE_OPTIONS instead: NODE_OPTIONS='--inspect' next ${ command } `
2019-05-29 13:57:26 +02:00
)
2019-02-26 21:57:32 +01:00
2018-12-15 22:55:59 +01:00
// Make sure the `next <subcommand> --help` case is covered
if ( args [ '--help' ] ) {
forwardedArgs . push ( '--help' )
}
const defaultEnv = command === 'dev' ? 'development' : 'production'
2020-05-04 18:21:58 +02:00
const standardEnv = [ 'production' , 'development' , 'test' ]
2021-12-07 15:25:39 +01:00
if ( process . env . NODE_ENV ) {
const isNotStandard = ! standardEnv . includes ( process . env . NODE_ENV )
const shouldWarnCommands =
process . env . NODE_ENV === 'development'
? [ 'start' , 'build' ]
: process . env . NODE_ENV === 'production'
? [ 'dev' ]
: [ ]
if ( isNotStandard || shouldWarnCommands . includes ( command ) ) {
log . warn ( NON_STANDARD_NODE_ENV )
}
2020-05-04 18:21:58 +02:00
}
2019-06-24 20:26:17 +02:00
; ( process . env as any ) . NODE_ENV = process . env . NODE_ENV || defaultEnv
2018-12-15 22:55:59 +01:00
2020-11-25 14:30:06 +01:00
// Make sure commands gracefully respect termination signals (e.g. from Docker)
process . on ( 'SIGTERM' , ( ) = > process . exit ( 0 ) )
process . on ( 'SIGINT' , ( ) = > process . exit ( 0 ) )
2021-03-19 09:06:28 +01:00
commands [ command ] ( )
. then ( ( exec ) = > exec ( forwardedArgs ) )
. then ( ( ) = > {
if ( command === 'build' ) {
// ensure process exits after build completes so open handles/connections
// don't cause process to hang
process . exit ( 0 )
}
} )
2018-12-15 22:55:59 +01:00
if ( command === 'dev' ) {
2021-10-16 14:22:42 +02:00
const { CONFIG_FILES } = require ( '../shared/lib/constants' )
2019-05-29 13:57:26 +02:00
const { watchFile } = require ( 'fs' )
2021-10-16 14:22:42 +02:00
for ( const CONFIG_FILE of CONFIG_FILES ) {
watchFile ( ` ${ process . cwd ( ) } / ${ CONFIG_FILE } ` , ( cur : any , prev : any ) = > {
if ( cur . size > 0 || prev . size > 0 ) {
console . log (
` \ n> Found a change in ${ CONFIG_FILE } . Restart the server to see the changes in effect. `
)
}
} )
}
2018-12-15 22:55:59 +01:00
}