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 startServer from '../server/lib/start-server'
|
2020-07-28 12:12:57 +02:00
|
|
|
import { printAndExit } from '../server/lib/utils'
|
2019-02-26 21:57:32 +01:00
|
|
|
import { cliCommand } from '../bin/next'
|
2020-04-29 10:49:28 +02:00
|
|
|
import * as Log from '../build/output/log'
|
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 nextStart: 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 production mode.
|
|
|
|
The application should be compiled with \`next build\` first.
|
|
|
|
|
|
|
|
Usage
|
|
|
|
$ next start <dir> -p <port>
|
|
|
|
|
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])
|
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-01-25 16:13:12 +01:00
|
|
|
const host = args['--hostname'] || '0.0.0.0'
|
2021-09-13 14:36:25 +02:00
|
|
|
|
|
|
|
if (process.env.__NEXT_RAND_PORT) {
|
|
|
|
port = 0
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:13:12 +01:00
|
|
|
startServer({ dir }, port, host)
|
2021-09-13 14:36:25 +02:00
|
|
|
.then(async ({ app, actualPort }) => {
|
|
|
|
const appUrl = `http://${
|
|
|
|
host === '0.0.0.0' ? 'localhost' : host
|
|
|
|
}:${actualPort}`
|
|
|
|
Log.ready(`started server on ${host}:${actualPort}, url: ${appUrl}`)
|
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
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export { nextStart }
|