-
Notifications
You must be signed in to change notification settings - Fork 45
/
cli.js
executable file
·340 lines (301 loc) · 10.5 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const program = require('commander');
const cosmiconfig = require('cosmiconfig');
const explorer = cosmiconfig('bolt');
const configStore = require('@bolt/build-utils/config-store');
const log = require('@bolt/build-utils/log');
const { readYamlFileSync } = require('@bolt/build-utils/yaml');
const { getPort } = require('@bolt/build-utils/get-port');
const configSchema = readYamlFileSync(
path.join(__dirname, './utils/config.schema.yml'),
);
const packageJson = require('./package.json');
let userConfig;
// global `bolt` cli options & meta
program
.version(packageJson.version)
.option(
'-C, --config-file <path>',
'Pass in a specific config file instead of default of ".boltrc.js/json".',
)
.option('--prod', configSchema.properties.prod.description)
.option(
'-v, --verbosity <amount>',
configSchema.properties.verbosity.description,
parseInt,
)
.parse(process.argv);
if (program.configFile) {
const configFilePath = path.join(process.cwd(), program.configFile);
if (!fs.existsSync(configFilePath)) {
console.error(`Error, config file does not exist: ${configFilePath}`);
process.exit(1);
}
const configFile = require(configFilePath);
userConfig = {
...configFile,
configFileUsed: configFilePath,
};
} else {
try {
const searchedFor = explorer.searchSync();
userConfig = {
...searchedFor.config,
configFileUsed: searchedFor.filepath,
};
} catch (error) {
log.errorAndExit('Could not find config in a .boltrc file', error);
}
}
(async () => {
await configStore.init(userConfig).then(async () => {
// Now that config is initilized, we can start requiring other things
const { buildBoltManifest } = require('@bolt/build-utils/manifest');
const log = require('@bolt/build-utils/log');
// store a copy of the original config before modifying with defaults + auto values. This allows us to point out if a config option was manually vs automatically set.
let originalConfig;
/**
* Update config with all options flags
* @param {Object} options
* @param programInstance - The commander `program`
* @returns {Object} config - Final updated config
*/
async function updateConfig(options, programInstance) {
await configStore.updateConfig(async config => {
originalConfig = config;
config.verbosity =
typeof program.verbosity === 'undefined'
? config.verbosity
: program.verbosity;
config.openServerAtStart =
typeof options.open === 'undefined'
? config.openServerAtStart
: options.open;
config.renderingService =
typeof options.renderingService === 'undefined'
? process.env.TRAVIS
? false
: config.renderingService
: options.renderingService;
config.env = process.env.NODE_ENV
? process.env.NODE_ENV
: typeof options.env === 'undefined'
? config.env
: options.env;
//check port not only when setting defaults but in boltrc.js too
config.port = await getPort(config.port);
config.webpackStats =
typeof options.webpackStats === 'undefined'
? config.webpackStats
: options.webpackStats;
config.webpackDevServer =
typeof options.webpackDevServer === 'undefined'
? config.webpackDevServer
: options.webpackDevServer;
config.mode =
typeof options.mode === 'undefined' ? config.mode : options.mode;
config.quick =
typeof options.quick === 'undefined' ? config.quick : options.quick;
config.watch =
typeof options.watch === 'undefined' ? config.watch : options.watch;
config.prod =
typeof program.prod === 'undefined' ? config.prod : program.prod;
// automatically set enableSSR to true in prod mode and false in dev mode, unless manually set.
config.enableSSR = false;
// automatically enable i18n in production builds if undefined
config.i18n =
typeof options.i18n !== 'undefined'
? options.i18n
: config.prod
? true
: false;
// If i18n is disabled, ignore and remove lang config settings
if (config.lang && config.i18n === false) {
// Remove any lang-specific settings for local dev to speed things up.
delete config['lang'];
}
return config;
});
const config = await configStore.getConfig();
log.dim(`Verbosity: ${config.verbosity}`);
log.dim(`Prod: ${config.prod}`);
log.dim(`i18n: ${config.i18n}`);
log.dim(
`enableSSR: ${config.enableSSR} ${
originalConfig.enableSSR ? '(manually set)' : '(auto set)'
}`,
);
log.dim(`Rendering Mode: ${config.mode}`);
if (config.verbosity > 2) {
log.dim(`Opening browser: ${config.openServerAtStart}`);
log.dim(`Quick mode: ${config.quick}`);
log.dim(`buildDir: ${config.buildDir}`);
log.dim(`dataDir: ${config.dataDir}`);
log.dim(`wwwDir: ${config.wwwDir}`);
}
// Basically at this point, the cli is bootstrapped and ready to go.
// Let's build the core bolt manifest
await buildBoltManifest();
return config;
}
await log.intro();
program
.option(
'--no-ssr',
'Manually disables server side rendering (vs auto enable in prod mode)',
)
.option(
'--ssr',
'Manually enabless server side rendering in all enviornments (vs by default only enabling automatically in prod mode)',
);
// `bolt build`
program
.command('build')
.description('Build it')
.option(
'--webpack-stats',
configSchema.properties.webpackStats.description,
)
.option('-I, --i18n', configSchema.properties.i18n.description)
.option('-Q, --quick', configSchema.properties.quick.description)
.action(async options => {
log.info(
`Starting build (${options.parallel ? 'parallel' : 'serial'})`,
);
await updateConfig(options, program);
require('./tasks/task-collections').build();
});
// `bolt prep`
program
.command('prep')
.description('Prepwork before building')
.action(async options => {
log.info('Starting prep work.');
await updateConfig(options, program);
require('./tasks/task-collections').buildPrep();
});
// `bolt prep`
program
.command('criticalcss')
.description('Generate Critical CSS')
.action(async options => {
log.info('Starting critical CSS');
await updateConfig(options, program);
require('./tasks/task-collections').criticalcss();
});
program
.command('serve')
.description('Spin up local server')
.option(
'-O, --open',
configSchema.properties.openServerAtStart.description,
)
.option(
'--webpack-dev-server',
configSchema.properties.webpackDevServer.description,
)
.option('--watch', configSchema.properties.watch.description)
.action(async options => {
if (options.watch === undefined) {
options.watch = true;
}
await updateConfig(options, program);
require('./tasks/task-collections').serve();
});
program.command('watch').action(async options => {
options.watch = true;
await updateConfig(options, program);
require('./tasks/task-collections').watch();
});
program.command('clean').action(async options => {
await updateConfig(options, program);
require('./tasks/task-collections').clean();
});
program
.command('start')
.option(
'-O, --open',
configSchema.properties.openServerAtStart.description,
)
.option('-I, --i18n', configSchema.properties.i18n.description)
.option('-Q, --quick', configSchema.properties.quick.description)
.option(
'--webpack-dev-server',
configSchema.properties.webpackDevServer.description,
)
.option('--watch', configSchema.properties.watch.description)
.action(async options => {
if (options.watch === undefined) {
options.watch = true;
}
await updateConfig(options, program);
require('./tasks/task-collections').start();
});
// `bolt lint`
program
.command('lint')
.description("A linter... that doesn't work!")
.action(async options => {
await updateConfig(options, program);
});
program
.command('img')
.alias('images')
.description('Image process')
.action(async options => {
await updateConfig(options, program);
require('./tasks/task-collections').images();
});
program
.command('webpack')
.alias('wp')
.description('WebPack Compile')
.option(
'--webpack-stats',
configSchema.properties.webpackStats.description,
)
.action(async options => {
await updateConfig(options, program);
try {
await require('./tasks/webpack-tasks').compile();
} catch (error) {
log.errorAndExit('WebPack failed', error);
}
});
program
.command('pattern-lab')
.alias('pl')
.description('Pattern Lab Compile')
.action(async options => {
await updateConfig(options, program);
try {
await require('./tasks/pattern-lab-tasks').compile();
} catch (error) {
log.errorAndExit('Pattern Lab failed', error);
}
});
program
.command('static')
.description('Static Site Compile')
.action(async options => {
await updateConfig(options, program);
try {
await require('./tasks/task-collections').buildPrep();
await require('./tasks/static-tasks').compile();
} catch (error) {
log.errorAndExit('Static Site Generation failed', error);
}
});
// This will tell you all that got `require()`-ed
// We want to only load what we need - that's why not all `require` statements are at top
// log.info('All that got `require()`-ed');
// console.log(Object.keys(require.cache).filter(x => !x.includes('node_modules')));
// log.info('END: All that got `require()`-ed');
// cli init ~ must go at bottom
program.parse(process.argv);
});
})().catch(err => {
console.error(err);
});