1 | #!/usr/bin/env node
|
2 |
|
3 | // Show logs
|
4 | process.env.DEBUG = 'nuxt:*'
|
5 |
|
6 | var _ = require('lodash')
|
7 | var debug = require('debug')('nuxt:build')
|
8 | debug.color = 2 // force green color
|
9 | var fs = require('fs')
|
10 | var Nuxt = require('../')
|
11 | var chokidar = require('chokidar')
|
12 | var resolve = require('path').resolve
|
13 |
|
14 | var rootDir = resolve(process.argv.slice(2)[0] || '.')
|
15 | var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
|
16 |
|
17 | var options = {}
|
18 | if (fs.existsSync(nuxtConfigFile)) {
|
19 | options = require(nuxtConfigFile)
|
20 | }
|
21 | if (typeof options.rootDir !== 'string') {
|
22 | options.rootDir = rootDir
|
23 | }
|
24 | options.dev = true // Add hot reloading and watching changes
|
25 |
|
26 | var nuxt = new Nuxt(options)
|
27 | nuxt.build()
|
28 | .then(() => {
|
29 | var server = new nuxt.Server(nuxt)
|
30 | .listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
|
31 | listenOnConfigChanges(nuxt, server)
|
32 | })
|
33 | .catch((err) => {
|
34 | console.error(err) // eslint-disable-line no-console
|
35 | process.exit(1)
|
36 | })
|
37 |
|
38 | function listenOnConfigChanges (nuxt, server) {
|
39 | // Listen on nuxt.config.js changes
|
40 | var build = _.debounce(() => {
|
41 | debug('[nuxt.config.js] changed')
|
42 | delete require.cache[nuxtConfigFile]
|
43 | var options = {}
|
44 | if (fs.existsSync(nuxtConfigFile)) {
|
45 | try {
|
46 | options = require(nuxtConfigFile)
|
47 | } catch (e) {
|
48 | return console.error(e) // eslint-disable-line no-console
|
49 | }
|
50 | }
|
51 | options.rootDir = rootDir
|
52 | nuxt.close()
|
53 | .then(() => {
|
54 | nuxt.renderer = null
|
55 | debug('Rebuilding the app...')
|
56 | return new Nuxt(options).build()
|
57 | })
|
58 | .then((nuxt) => {
|
59 | server.nuxt = nuxt
|
60 | })
|
61 | .catch((error) => {
|
62 | console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
|
63 | process.exit(1)
|
64 | })
|
65 | }, 200)
|
66 | var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
|
67 | chokidar.watch(nuxtConfigFile, { ignoreInitial: true })
|
68 | .on('all', build)
|
69 | }
|