UNPKG

3 kBJavaScriptView Raw
1/*!
2 * @nuxt/cli v2.12.0 (c) 2016-2020
3
4 * - All the amazing contributors
5 * Released under the MIT License.
6 * Website: https://nuxtjs.org
7*/
8'use strict';
9
10const index = require('./cli-index.js');
11require('path');
12require('@nuxt/config');
13require('exit');
14require('@nuxt/utils');
15require('chalk');
16require('std-env');
17require('wrap-ansi');
18require('boxen');
19require('consola');
20require('minimist');
21require('hable');
22require('fs');
23require('execa');
24
25const generate = {
26 name: 'generate',
27 description: 'Generate a static web application (server-rendered)',
28 usage: 'generate <dir>',
29 options: {
30 ...index.common,
31 ...index.locking,
32 build: {
33 type: 'boolean',
34 default: true,
35 description: 'Only generate pages for dynamic routes. Nuxt has to be built once before using this option'
36 },
37 devtools: {
38 type: 'boolean',
39 default: false,
40 description: 'Enable Vue devtools',
41 prepare (cmd, options, argv) {
42 options.vue = options.vue || {};
43 options.vue.config = options.vue.config || {};
44 if (argv.devtools) {
45 options.vue.config.devtools = true;
46 }
47 }
48 },
49 quiet: {
50 alias: 'q',
51 type: 'boolean',
52 description: 'Disable output except for errors',
53 prepare (cmd, options, argv) {
54 // Silence output when using --quiet
55 options.build = options.build || {};
56 if (argv.quiet) {
57 options.build.quiet = true;
58 }
59 }
60 },
61 modern: {
62 ...index.common.modern,
63 description: 'Generate app in modern build (modern mode can be only client)',
64 prepare (cmd, options, argv) {
65 if (index.normalizeArg(argv.modern)) {
66 options.modern = 'client';
67 }
68 }
69 },
70 'fail-on-error': {
71 type: 'boolean',
72 default: false,
73 description: 'Exit with non-zero status code if there are errors when generating pages'
74 }
75 },
76 async run (cmd) {
77 const config = await cmd.getNuxtConfig({ dev: false, _generate: true, _build: cmd.argv.build });
78
79 // Disable analyze if set by the nuxt config
80 if (!config.build) {
81 config.build = {};
82 }
83 config.build.analyze = false;
84
85 const nuxt = await cmd.getNuxt(config);
86
87 if (cmd.argv.lock) {
88 await cmd.setLock(await index.createLock({
89 id: 'build',
90 dir: nuxt.options.buildDir,
91 root: config.rootDir
92 }));
93
94 nuxt.hook('build:done', async () => {
95 await cmd.releaseLock();
96
97 await cmd.setLock(await index.createLock({
98 id: 'generate',
99 dir: nuxt.options.generate.dir,
100 root: config.rootDir
101 }));
102 });
103 }
104
105 const generator = await cmd.getGenerator(nuxt);
106
107 const { errors } = await generator.generate({
108 init: true,
109 build: cmd.argv.build
110 });
111
112 if (cmd.argv['fail-on-error'] && errors.length > 0) {
113 throw new Error('Error generating pages, exiting with non-zero code')
114 }
115 }
116};
117
118exports.default = generate;