next-start 1.6 KB
Newer Older
N
nkzawa 已提交
1 2
#!/usr/bin/env node

N
nkzawa 已提交
3
import { resolve } from 'path'
N
nkzawa 已提交
4 5
import parseArgs from 'minimist'
import Server from '../server'
6
import { existsSync } from 'fs'
N
nkzawa 已提交
7

8 9
process.env.NODE_ENV = process.env.NODE_ENV || 'production'

N
nkzawa 已提交
10 11 12
const argv = parseArgs(process.argv.slice(2), {
  alias: {
    h: 'help',
13
    H: 'hostname',
N
nkzawa 已提交
14 15 16
    p: 'port'
  },
  boolean: ['h'],
17
  string: ['H'],
18
  default: { p: 3000 }
N
nkzawa 已提交
19 20
})

21 22 23 24 25
if (argv.hostname === '') {
  console.error(`> Provided hostname argument has no value`)
  process.exit(1)
}

S
Stephen Sauceda 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
if (argv.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>

    <dir> is the directory that contains the compiled .next folder
    created by running \`next build\`.
    If no directory is provided, the current directory will be assumed.

    Options
      --port, -p      A port number on which to start the application
41
      --hostname, -H  Hostname on which to start the application
42
      --help, -h      Displays this message
S
Stephen Sauceda 已提交
43 44 45 46
  `)
  process.exit(0)
}

N
nkzawa 已提交
47
const dir = resolve(argv._[0] || '.')
N
nkzawa 已提交
48

N
nkzawa 已提交
49
const srv = new Server({ dir })
50

51 52
if (!existsSync(resolve(dir, '.next', 'BUILD_ID'))) {
  console.error(`> Could not find a valid build in the '.next' directory! Try building your app with 'next build' before starting the server.`)
53 54 55
  process.exit(1)
}

56
srv.start(argv.port, argv.hostname)
N
nkzawa 已提交
57
.then(() => {
58
  if (!process.env.NOW) {
59
    console.log(`> Ready on http://${argv.hostname ? argv.hostname : 'localhost'}:${argv.port}`)
60
  }
N
nkzawa 已提交
61 62 63
})
.catch((err) => {
  console.error(err)
D
Dan Zajdband 已提交
64
  process.exit(1)
N
nkzawa 已提交
65
})