2016-10-05 23:52:50 +00:00
#!/usr/bin/env node
2017-12-24 18:05:23 +00:00
import { join } from 'path'
2016-10-17 00:00:17 +00:00
import { spawn } from 'cross-spawn'
2016-12-19 20:14:23 +00:00
import pkg from '../../package.json'
2016-10-05 23:52:50 +00:00
2017-02-23 16:00:37 +00:00
if (pkg.peerDependencies) {
Object.keys(pkg.peerDependencies).forEach(dependency => {
try {
// When 'npm link' is used it checks the clone location. Not the project.
require.resolve(dependency)
} catch (err) {
2017-06-07 09:13:49 +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}'`)
2017-02-23 16:00:37 +00:00
}
})
}
2016-10-05 23:52:50 +00:00
const defaultCommand = 'dev'
const commands = new Set([
2016-10-16 23:55:30 +00:00
'init',
2016-10-05 23:52:50 +00:00
'build',
2016-12-18 20:28:34 +00:00
'start',
2017-05-07 22:47:40 +00:00
'export',
2016-12-18 20:28:34 +00:00
defaultCommand
2016-10-05 23:52:50 +00:00
])
let cmd = process.argv[2]
2017-11-20 14:51:46 +00:00
let args = []
let nodeArgs = []
2016-10-05 23:52:50 +00:00
2016-12-19 20:14:23 +00:00
if (new Set(['--version', '-v']).has(cmd)) {
console.log(`next.js v${pkg.version}`)
process.exit(0)
}
2018-05-25 13:10:55 +00:00
const inspectArg = process.argv.find(arg => arg.includes('--inspect'))
if (inspectArg) {
nodeArgs.push(inspectArg)
2017-11-20 14:51:46 +00:00
}
2016-12-18 20:28:34 +00:00
if (new Set(['--help', '-h']).has(cmd)) {
console.log(`
Usage
$ next <command>
Available commands
${Array.from(commands).join(', ')}
For more information run a command with the --help flag
$ next init --help
`)
process.exit(0)
}
2016-10-05 23:52:50 +00:00
if (commands.has(cmd)) {
args = process.argv.slice(3)
} else {
cmd = defaultCommand
args = process.argv.slice(2)
}
2017-10-28 22:19:48 +00:00
const defaultEnv = cmd === 'dev' ? 'development' : 'production'
process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv
2016-10-19 12:58:08 +00:00
const bin = join(__dirname, 'next-' + cmd)
2016-10-05 23:52:50 +00:00
2016-12-17 18:38:11 +00:00
const startProcess = () => {
2017-11-20 14:51:46 +00:00
const proc = spawn('node', [...nodeArgs, ...[bin], ...args], { stdio: 'inherit', customFds: [0, 1, 2] })
2017-01-26 01:10:48 +00:00
proc.on('close', (code, signal) => {
if (code !== null) {
process.exit(code)
}
if (signal) {
if (signal === 'SIGKILL') {
process.exit(137)
}
2017-04-03 03:49:29 +00:00
console.log(`got signal ${signal}, exiting`)
2017-01-26 01:10:48 +00:00
process.exit(1)
}
process.exit(0)
})
2016-12-17 18:38:11 +00:00
proc.on('error', (err) => {
console.error(err)
process.exit(1)
})
return proc
}
let proc = startProcess()
if (cmd === 'dev') {
2017-12-24 18:05:23 +00:00
const {watchFile} = require('fs')
watchFile(`${process.cwd()}/next.config.js`, (cur, prev) => {
2016-12-17 18:38:11 +00:00
if (cur.size > 0 || prev.size > 0) {
console.log('\n> Found a change in next.config.js, restarting the server...')
// Don't listen to 'close' now since otherwise parent gets killed by listener
proc.removeAllListeners('close')
proc.kill()
proc = startProcess()
}
})
}