2018-12-15 21:55:59 +00:00
# ! / u s r / b i n / e n v n o d e
import { join } from 'path'
import spawn from 'cross-spawn'
import arg from 'arg'
2018-12-31 13:44:27 +00:00
[ 'react' , 'react-dom' ] . forEach ( ( dependency ) = > {
2018-12-15 21:55:59 +00:00
try {
// When 'npm link' is used it checks the clone location. Not the project.
require . resolve ( dependency )
} catch ( err ) {
2018-12-31 13:44:27 +00:00
// tslint:disable-next-line
2018-12-15 21:55:59 +00:00
console . warn ( ` 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 --save ${ dependency } ' ` )
}
} )
const defaultCommand = 'dev'
const commands = [
'build' ,
'start' ,
'export' ,
2018-12-31 13:44:27 +00:00
defaultCommand ,
2018-12-15 21:55:59 +00:00
]
const args = arg ( {
// Types
'--version' : Boolean ,
'--help' : Boolean ,
'--inspect' : Boolean ,
// Aliases
'-v' : '--version' ,
2018-12-31 13:44:27 +00:00
'-h' : '--help' ,
2018-12-15 21:55:59 +00:00
} , {
2018-12-31 13:44:27 +00:00
permissive : true ,
2018-12-15 21:55:59 +00:00
} )
// Version is inlined into the file using taskr build pipeline
if ( args [ '--version' ] ) {
2018-12-31 13:44:27 +00:00
// tslint:disable-next-line
2018-12-15 21:55:59 +00:00
console . log ( ` Next.js v ${ process . env . NEXT_VERSION } ` )
process . exit ( 0 )
}
// Check if we are running `next <subcommand>` or `next`
2018-12-31 13:44:27 +00:00
const foundCommand = args . _ . find ( ( cmd ) = > commands . includes ( cmd ) )
2018-12-15 21:55:59 +00:00
// Makes sure the `next <subcommand> --help` case is covered
// This help message is only showed for `next --help`
if ( ! foundCommand && args [ '--help' ] ) {
2018-12-31 13:44:27 +00:00
// tslint:disable-next-line
2018-12-15 21:55:59 +00:00
console . log ( `
Usage
$ next < command >
Available commands
$ { commands . join ( ', ' ) }
2018-12-31 13:44:27 +00:00
2018-12-15 21:55:59 +00:00
Options
-- version , - p Version number
2018-12-17 18:17:29 +00:00
-- inspect Enable the Node . js inspector
2018-12-31 13:44:27 +00:00
-- help , - h Displays this message
2018-12-15 21:55:59 +00:00
For more information run a command with the -- help flag
$ next build -- help
` )
process . exit ( 0 )
}
2018-12-17 18:17:29 +00:00
const nodeArguments : string [ ] = [ ]
2018-12-15 21:55:59 +00:00
if ( args [ '--inspect' ] ) {
2018-12-31 13:44:27 +00:00
// tslint:disable-next-line
2018-12-17 18:17:29 +00:00
console . log ( 'Passing "--inspect" to Node.js' )
2018-12-15 21:55:59 +00:00
nodeArguments . push ( '--inspect' )
}
const command = foundCommand || defaultCommand
2018-12-31 13:44:27 +00:00
const forwardedArgs = args . _ . filter ( ( arg ) = > arg !== command )
2018-12-15 21:55:59 +00:00
// Make sure the `next <subcommand> --help` case is covered
if ( args [ '--help' ] ) {
forwardedArgs . push ( '--help' )
}
const defaultEnv = command === 'dev' ? 'development' : 'production'
process . env . NODE_ENV = process . env . NODE_ENV || defaultEnv
const bin = join ( __dirname , 'next-' + command )
const startProcess = ( ) = > {
const proc = spawn ( 'node' , [ . . . nodeArguments , bin , . . . forwardedArgs ] , { stdio : 'inherit' } )
proc . on ( 'close' , ( code , signal ) = > {
if ( code !== null ) {
process . exit ( code )
}
if ( signal ) {
if ( signal === 'SIGKILL' ) {
process . exit ( 137 )
}
2018-12-31 13:44:27 +00:00
// tslint:disable-next-line
2018-12-15 21:55:59 +00:00
console . log ( ` got signal ${ signal } , exiting ` )
2019-01-26 21:51:08 +00:00
process . exit ( signal === 'SIGINT' || signal === 'SIGTERM' ? 0 : 1 )
2018-12-15 21:55:59 +00:00
}
process . exit ( 0 )
} )
proc . on ( 'error' , ( err ) = > {
2018-12-31 13:44:27 +00:00
// tslint:disable-next-line
2018-12-15 21:55:59 +00:00
console . error ( err )
process . exit ( 1 )
} )
return proc
}
let proc = startProcess ( )
2018-12-31 13:44:27 +00:00
function wrapper() {
2018-12-15 21:55:59 +00:00
if ( proc ) {
proc . kill ( )
}
}
process . on ( 'SIGINT' , wrapper )
process . on ( 'SIGTERM' , wrapper )
process . on ( 'exit' , wrapper )
if ( command === 'dev' ) {
const { CONFIG_FILE } = require ( 'next-server/constants' )
const { watchFile } = require ( 'fs' )
watchFile ( ` ${ process . cwd ( ) } / ${ CONFIG_FILE } ` , ( cur : any , prev : any ) = > {
if ( cur . size > 0 || prev . size > 0 ) {
2018-12-31 13:44:27 +00:00
// tslint:disable-next-line
2018-12-15 21:55:59 +00:00
console . log ( ` \ n> Found a change in ${ CONFIG_FILE } , restarting the server... ` )
// Don't listen to 'close' now since otherwise parent gets killed by listener
proc . removeAllListeners ( 'close' )
proc . kill ( )
proc = startProcess ( )
}
} )
}