2019-02-26 21:57:32 +01:00
|
|
|
#!/usr/bin/env node
|
2019-03-24 23:38:03 +01:00
|
|
|
import arg from 'next/dist/compiled/arg/index.js'
|
2019-02-26 21:57:32 +01:00
|
|
|
import { existsSync } from 'fs'
|
|
|
|
import startServer from '../server/lib/start-server'
|
|
|
|
import { printAndExit } from '../server/lib/utils'
|
2020-08-06 16:41:11 +02:00
|
|
|
import * as Log from '../build/output/log'
|
2019-02-26 21:57:32 +01:00
|
|
|
import { startedDevelopmentServer } from '../build/output'
|
|
|
|
import { cliCommand } from '../bin/next'
|
2021-09-16 18:06:57 +02:00
|
|
|
import isError from '../lib/is-error'
|
2021-09-22 23:29:27 +02:00
|
|
|
import { getProjectDir } from '../lib/get-project-dir'
|
2019-02-26 21:57:32 +01:00
|
|
|
|
2020-05-18 21:24:37 +02:00
|
|
|
const nextDev: cliCommand = (argv) => {
|
2020-07-28 12:12:57 +02:00
|
|
|
const validArgs: arg.Spec = {
|
|
|
|
// Types
|
|
|
|
'--help': Boolean,
|
|
|
|
'--port': Number,
|
|
|
|
'--hostname': String,
|
2019-02-26 21:57:32 +01:00
|
|
|
|
2020-07-28 12:12:57 +02:00
|
|
|
// Aliases
|
|
|
|
'-h': '--help',
|
|
|
|
'-p': '--port',
|
|
|
|
'-H': '--hostname',
|
|
|
|
}
|
|
|
|
let args: arg.Result<arg.Spec>
|
|
|
|
try {
|
|
|
|
args = arg(validArgs, { argv })
|
|
|
|
} catch (error) {
|
2021-09-16 18:06:57 +02:00
|
|
|
if (isError(error) && error.code === 'ARG_UNKNOWN_OPTION') {
|
2020-07-28 12:12:57 +02:00
|
|
|
return printAndExit(error.message, 1)
|
|
|
|
}
|
|
|
|
throw error
|
|
|
|
}
|
2019-02-26 21:57:32 +01:00
|
|
|
if (args['--help']) {
|
|
|
|
console.log(`
|
|
|
|
Description
|
|
|
|
Starts the application in development mode (hot-code reloading, error
|
|
|
|
reporting, etc)
|
|
|
|
|
|
|
|
Usage
|
|
|
|
$ next dev <dir> -p <port number>
|
|
|
|
|
2020-03-18 10:18:29 +01:00
|
|
|
<dir> represents the directory of the Next.js application.
|
|
|
|
If no directory is provided, the current directory will be used.
|
2019-02-26 21:57:32 +01:00
|
|
|
|
|
|
|
Options
|
|
|
|
--port, -p A port number on which to start the application
|
2021-01-25 16:13:12 +01:00
|
|
|
--hostname, -H Hostname on which to start the application (default: 0.0.0.0)
|
2019-02-26 21:57:32 +01:00
|
|
|
--help, -h Displays this message
|
|
|
|
`)
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:29:27 +02:00
|
|
|
const dir = getProjectDir(args._[0])
|
2019-02-26 21:57:32 +01:00
|
|
|
|
|
|
|
// Check if pages dir exists and warn if not
|
|
|
|
if (!existsSync(dir)) {
|
|
|
|
printAndExit(`> No such directory exists as the project root: ${dir}`)
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:41:11 +02:00
|
|
|
async function preflight() {
|
2021-10-16 14:22:42 +02:00
|
|
|
const { getPackageVersion } = await Promise.resolve(
|
|
|
|
require('../lib/get-package-version')
|
|
|
|
)
|
2020-08-20 18:05:29 +02:00
|
|
|
const [sassVersion, nodeSassVersion] = await Promise.all([
|
|
|
|
getPackageVersion({ cwd: dir, name: 'sass' }),
|
|
|
|
getPackageVersion({ cwd: dir, name: 'node-sass' }),
|
|
|
|
])
|
|
|
|
if (sassVersion && nodeSassVersion) {
|
|
|
|
Log.warn(
|
|
|
|
'Your project has both `sass` and `node-sass` installed as dependencies, but should only use one or the other. ' +
|
|
|
|
'Please remove the `node-sass` dependency from your project. ' +
|
2021-03-29 10:25:00 +02:00
|
|
|
' Read more: https://nextjs.org/docs/messages/duplicate-sass'
|
2020-08-20 18:05:29 +02:00
|
|
|
)
|
|
|
|
}
|
2020-08-06 16:41:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 14:36:25 +02:00
|
|
|
let port: number =
|
2021-06-14 16:20:34 +02:00
|
|
|
args['--port'] || (process.env.PORT && parseInt(process.env.PORT)) || 3000
|
2021-07-20 01:12:27 +02:00
|
|
|
|
2021-09-13 14:36:25 +02:00
|
|
|
// we allow the server to use a random port while testing
|
|
|
|
// instead of attempting to find a random port and then hope
|
|
|
|
// it doesn't become occupied before we leverage it
|
|
|
|
if (process.env.__NEXT_RAND_PORT) {
|
|
|
|
port = 0
|
|
|
|
}
|
|
|
|
|
2021-07-20 01:12:27 +02:00
|
|
|
// We do not set a default host value here to prevent breaking
|
|
|
|
// some set-ups that rely on listening on other interfaces
|
|
|
|
const host = args['--hostname']
|
2019-02-26 21:57:32 +01:00
|
|
|
|
2021-01-25 16:13:12 +01:00
|
|
|
startServer({ dir, dev: true, isNextDevCommand: true }, port, host)
|
2021-09-13 14:36:25 +02:00
|
|
|
.then(async ({ app, actualPort }) => {
|
|
|
|
const appUrl = `http://${
|
|
|
|
!host || host === '0.0.0.0' ? 'localhost' : host
|
|
|
|
}:${actualPort}`
|
|
|
|
startedDevelopmentServer(appUrl, `${host || '0.0.0.0'}:${actualPort}`)
|
2020-08-06 16:41:11 +02:00
|
|
|
// Start preflight after server is listening and ignore errors:
|
|
|
|
preflight().catch(() => {})
|
|
|
|
// Finalize server bootup:
|
2019-02-26 21:57:32 +01:00
|
|
|
await app.prepare()
|
|
|
|
})
|
2020-05-18 21:24:37 +02:00
|
|
|
.catch((err) => {
|
2019-02-26 21:57:32 +01:00
|
|
|
if (err.code === 'EADDRINUSE') {
|
|
|
|
let errorMessage = `Port ${port} is already in use.`
|
2020-03-29 18:21:53 +02:00
|
|
|
const pkgAppPath = require('next/dist/compiled/find-up').sync(
|
|
|
|
'package.json',
|
|
|
|
{
|
|
|
|
cwd: dir,
|
|
|
|
}
|
|
|
|
)
|
2019-02-26 21:57:32 +01:00
|
|
|
const appPackage = require(pkgAppPath)
|
|
|
|
if (appPackage.scripts) {
|
2019-05-29 13:57:26 +02:00
|
|
|
const nextScript = Object.entries(appPackage.scripts).find(
|
2020-05-18 21:24:37 +02:00
|
|
|
(scriptLine) => scriptLine[1] === 'next'
|
2019-05-29 13:57:26 +02:00
|
|
|
)
|
2019-02-26 21:57:32 +01:00
|
|
|
if (nextScript) {
|
2019-11-11 04:24:53 +01:00
|
|
|
errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p <some other port>\`.`
|
2019-02-26 21:57:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
console.error(errorMessage)
|
|
|
|
} else {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
process.nextTick(() => process.exit(1))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export { nextDev }
|