1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const index = require('./cli-index.js');
|
11 | require('path');
|
12 | require('@nuxt/config');
|
13 | require('exit');
|
14 | require('@nuxt/utils');
|
15 | require('chalk');
|
16 | require('std-env');
|
17 | require('wrap-ansi');
|
18 | require('boxen');
|
19 | require('consola');
|
20 | require('minimist');
|
21 | require('hable');
|
22 | require('fs');
|
23 | require('execa');
|
24 |
|
25 | const 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 |
|
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 |
|
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 |
|
118 | exports.default = generate;
|