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'
|
2022-05-22 21:42:53 +02:00
|
|
|
import { existsSync, watchFile } from 'fs'
|
2021-11-28 17:48:43 +01:00
|
|
|
import { startServer } from '../server/lib/start-server'
|
2022-09-01 00:13:39 +02:00
|
|
|
import { getPort, 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'
|
2022-08-07 19:31:30 +02:00
|
|
|
import { cliCommand } from '../lib/commands'
|
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'
|
2022-05-22 21:42:53 +02:00
|
|
|
import { CONFIG_FILES } from '../shared/lib/constants'
|
|
|
|
import path from 'path'
|
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
|
2022-02-13 01:22:52 +01:00
|
|
|
reporting, etc.)
|
2019-02-26 21:57:32 +01:00
|
|
|
|
|
|
|
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-07-20 01:12:27 +02:00
|
|
|
|
2022-09-01 00:13:39 +02:00
|
|
|
const port = getPort(args)
|
|
|
|
// If neither --port nor PORT were specified, it's okay to retry new ports.
|
|
|
|
const allowRetry =
|
|
|
|
args['--port'] === undefined && process.env.PORT === undefined
|
2021-09-13 14:36:25 +02:00
|
|
|
|
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-11-28 17:48:43 +01:00
|
|
|
startServer({
|
|
|
|
allowRetry,
|
|
|
|
dev: true,
|
|
|
|
dir,
|
|
|
|
hostname: host,
|
|
|
|
isNextDevCommand: true,
|
2021-11-11 23:19:20 +01:00
|
|
|
port,
|
2021-11-28 17:48:43 +01:00
|
|
|
})
|
|
|
|
.then(async (app) => {
|
|
|
|
const appUrl = `http://${app.hostname}:${app.port}`
|
|
|
|
startedDevelopmentServer(appUrl, `${host || '0.0.0.0'}:${app.port}`)
|
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))
|
|
|
|
})
|
2022-05-22 21:42:53 +02:00
|
|
|
|
|
|
|
for (const CONFIG_FILE of CONFIG_FILES) {
|
|
|
|
watchFile(path.join(dir, CONFIG_FILE), (cur: any, prev: any) => {
|
|
|
|
if (cur.size > 0 || prev.size > 0) {
|
|
|
|
console.log(
|
|
|
|
`\n> Found a change in ${CONFIG_FILE}. Restart the server to see the changes in effect.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-02-26 21:57:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export { nextDev }
|