next-start 1.5 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
  default: { p: 3000 }
N
nkzawa 已提交
18 19
})

S
Stephen Sauceda 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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
35
      --hostname, -H  Hostname on which to start the application
36
      --help, -h      Displays this message
S
Stephen Sauceda 已提交
37 38 39 40
  `)
  process.exit(0)
}

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

N
nkzawa 已提交
43
const srv = new Server({ dir })
44

45 46
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.`)
47 48 49
  process.exit(1)
}

50
srv.start(argv.port, argv.hostname)
N
nkzawa 已提交
51
.then(() => {
52
  if (!process.env.NOW) {
53
    console.log(`> Ready on http://${argv.hostname && typeof argv.hostname !== 'boolean' ? argv.hostname : 'localhost'}:${argv.port}`)
54
  }
N
nkzawa 已提交
55 56 57
})
.catch((err) => {
  console.error(err)
D
Dan Zajdband 已提交
58
  process.exit(1)
N
nkzawa 已提交
59
})
60