2018-12-15 22:55:59 +01:00
# ! / u s r / b i n / e n v n o d e
2019-03-24 23:38:03 +01:00
import arg from 'next/dist/compiled/arg/index.js'
2018-12-15 22:55:59 +01:00
2018-12-31 14:44:27 +01: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 ) {
2018-12-31 14:44:27 +01:00
// tslint:disable-next-line
2018-12-15 22:55:59 +01: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 } ' ` )
}
} )
2019-04-04 23:47:17 +02:00
const React = require ( 'react' )
if ( typeof React . Suspense === 'undefined' ) {
throw new Error ( ` The version of React you are using is lower than the minimum required version needed for Next.js. Please upgrade "react" and "react-dom": "npm install --save react react-dom" https://err.sh/zeit/next.js/invalid-react-version ` )
}
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
const commands : { [ command : string ] : ( ) = > Promise < cliCommand > } = {
build : async ( ) = > await import ( '../cli/next-build' ) . then ( ( i ) = > i . nextBuild ) ,
start : async ( ) = > await import ( '../cli/next-start' ) . then ( ( i ) = > i . nextStart ) ,
export : async ( ) = > await import ( '../cli/next-export' ) . then ( ( i ) = > i . nextExport ) ,
dev : async ( ) = > await import ( '../cli/next-dev' ) . then ( ( i ) = > i . nextDev ) ,
}
2018-12-15 22:55:59 +01:00
const args = arg ( {
// Types
'--version' : Boolean ,
'--help' : Boolean ,
'--inspect' : Boolean ,
// Aliases
'-v' : '--version' ,
2018-12-31 14:44:27 +01:00
'-h' : '--help' ,
2018-12-15 22:55:59 +01:00
} , {
2018-12-31 14:44:27 +01:00
permissive : true ,
2018-12-15 22:55:59 +01:00
} )
// Version is inlined into the file using taskr build pipeline
if ( args [ '--version' ] ) {
2018-12-31 14:44:27 +01:00
// tslint:disable-next-line
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
// 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 14:44:27 +01:00
// tslint:disable-next-line
2018-12-15 22:55:59 +01:00
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
-- version , - p Version number
2018-12-17 19:17:29 +01:00
-- inspect Enable the Node . js inspector
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-02-26 21:57:32 +01:00
if ( args [ '--inspect' ] ) throw new Error ( ` Use env variable NODE_OPTIONS instead: NODE_OPTIONS="--inspect" next ${ command } ` )
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'
process . env . NODE_ENV = process . env . NODE_ENV || defaultEnv
2019-02-26 21:57:32 +01:00
commands [ command ] ( ) . then ( ( exec ) = > exec ( forwardedArgs ) )
2018-12-15 22:55:59 +01:00
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 14:44:27 +01:00
// tslint:disable-next-line
2019-02-26 21:57:32 +01:00
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
}
} )
}