2016-10-05 23:52:50 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2017-04-10 14:07:38 +00:00
|
|
|
import { join, resolve } from 'path'
|
2016-10-17 00:00:17 +00:00
|
|
|
import { spawn } from 'cross-spawn'
|
2016-12-17 18:38:11 +00:00
|
|
|
import { watchFile } from 'fs'
|
2016-12-19 20:14:23 +00:00
|
|
|
import pkg from '../../package.json'
|
2017-04-10 14:07:38 +00:00
|
|
|
import getConfig from '../server/config'
|
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) {
|
|
|
|
console.warn(`${dependency} not found. Please install ${dependency} using 'npm install ${dependency}'`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
let args
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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 = () => {
|
|
|
|
const proc = spawn(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()
|
2017-04-10 14:07:38 +00:00
|
|
|
const { pagesDirectory = resolve(process.cwd(), 'pages') } = getConfig(process.cwd())
|
2016-12-17 18:38:11 +00:00
|
|
|
|
|
|
|
if (cmd === 'dev') {
|
2017-04-10 14:07:38 +00:00
|
|
|
watchFile(`${resolve(pagesDirectory, '..')}/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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|