next-start 1.3 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 10 11 12 13 14 15 16 17 18

const argv = parseArgs(process.argv.slice(2), {
  alias: {
    h: 'help',
    p: 'port'
  },
  boolean: ['h'],
  default: {
    p: 3000
  }
})

S
Stephen Sauceda 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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
34
      --help, -h      Displays this message
S
Stephen Sauceda 已提交
35 36 37 38
  `)
  process.exit(0)
}

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

N
nkzawa 已提交
41
const srv = new Server({ dir })
42 43 44 45 46 47

if (!existsSync(resolve(dir, '.next'))) {
  console.error(`> Could not find the '.next' directory! Try building your app with 'next build' before starting the server.`)
  process.exit(1)
}

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