forked from pattern-lab/patternlab-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
470 lines (412 loc) · 15.7 KB
/
index.js
File metadata and controls
470 lines (412 loc) · 15.7 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* patternlab-node https://github.com/pattern-lab/patternlab-node
*
* Brian Muenzenmeyer, Geoff Pursell, Raphael Okon, tburny and the web community.
* Licensed under the MIT license.
*
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
*
*/
'use strict';
const packageInfo = require('../package.json');
const path = require('path');
const updateNotifier = require('update-notifier');
const logger = require('./lib/log');
const PatternGraph = require('./lib/pattern_graph').PatternGraph;
const CompileState = require('./lib/object_factory').CompileState;
const pe = require('./lib/pattern_exporter');
const lh = require('./lib/lineage_hunter');
const markModifiedPatterns = require('./lib/markModifiedPatterns');
const parseAllLinks = require('./lib/parseAllLinks');
const processMetaPattern = require('./lib/processMetaPattern');
const render = require('./lib/render');
const Pattern = require('./lib/object_factory').Pattern;
const defaultConfig = require('../patternlab-config.json');
let fs = require('fs-extra'); // eslint-disable-line
let ui_builder = require('./lib/ui_builder'); // eslint-disable-line
let assetCopier = require('./lib/asset_copy'); // eslint-disable-line
let pattern_exporter = new pe(); // eslint-disable-line
let serve = require('./lib/serve'); // eslint-disable-line
const lineage_hunter = new lh();
//bootstrap update notifier
updateNotifier({
pkg: packageInfo,
updateCheckInterval: 1000 * 60 * 60 * 24, // notify at most once a day
}).notify();
/**
* Returns the standardized default config
*
* @return {object} Returns the object representation of the patternlab-config.json
*/
const getDefaultConfig = function() {
return defaultConfig;
};
const patternlab_module = function(config) {
const PatternLab = require('./lib/patternlab');
const patternlab = new PatternLab(config);
const paths = patternlab.config.paths;
function help() {
logger.info('');
logger.info('Pattern Lab Node v' + patternlab.package.version);
logger.info('');
logger.info('Usage: patternlab.<FUNCTION_NAME>()');
logger.info('');
logger.info(' build');
logger.info(
' > builds patterns, copies assets, and constructs ui into config.paths.public'
);
logger.info('');
logger.info(' patternsonly');
logger.info(
' > builds patterns only, leaving existing public files intact'
);
logger.info('');
logger.info(' version');
logger.info(' > logs current version');
logger.info('');
logger.info(' v');
logger.info(' > return current version as a string');
logger.info('');
logger.info(' help');
logger.info(
' > logs more information about patternlab-node, pattern lab in general, and where to report issues.'
);
logger.info('');
logger.info(' liststarterkits');
logger.info(
' > fetches starterkit repos from pattern-lab github org that contain "starterkit" in their name'
);
logger.info('');
logger.info(' loadstarterkit');
logger.info(
' > loads starterkit already available via `node_modules/` into config.paths.source/*'
);
logger.info(
' > NOTE: Overwrites existing content, and only cleans out existing directory if --clean=true argument is passed.'
);
logger.info(
' > NOTE: In most cases, `npm install starterkit-name` will precede this call.'
);
logger.info(' > parameters:');
logger.info(' kit:string ');
logger.info(' > the name of the starter kit to load');
logger.info(' clean:bool ');
logger.info(
' > whether or not to remove all files from config.paths.source/ prior to load'
);
logger.info(' > example:');
logger.info(
' `patternlab.loadstarterkit("starterkit-mustache-demo", true)'
);
logger.info('');
// installplugin
// serve
// getSupportedTemplateExtensions
// events
// getDefaultConfig
logger.info('===============================');
logger.info('');
logger.info('Visit http://patternlab.io/ for more info about Pattern Lab');
logger.info(
'Visit https://github.com/pattern-lab/patternlab-node/issues to open an issue.'
);
logger.info(
'Visit https://github.com/pattern-lab/patternlab-node/wiki to view the changelog, roadmap, and other info.'
);
logger.info('');
logger.info('===============================');
}
/**
* If a graph was serialized and then {@code deletePatternDir == true}, there is a mismatch in the
* pattern metadata and not all patterns might be recompiled.
* For that reason an empty graph is returned in this case, so every pattern will be flagged as
* "needs recompile". Otherwise the pattern graph is loaded from the meta data.
*
* @param patternlab
* @param {boolean} deletePatternDir When {@code true}, an empty graph is returned
* @return {PatternGraph}
*/
function loadPatternGraph(deletePatternDir) {
// Sanity check to prevent problems when code is refactored
if (deletePatternDir) {
return PatternGraph.empty();
}
return PatternGraph.loadFromFile(patternlab);
}
function cleanBuildDirectory(incrementalBuildsEnabled) {
if (incrementalBuildsEnabled) {
logger.info('Incremental builds enabled.');
} else {
// needs to be done BEFORE processing patterns
fs.removeSync(paths.public.patterns);
fs.emptyDirSync(paths.public.patterns);
}
}
function buildPatterns(deletePatternDir, additionalData) {
patternlab.events.emit('patternlab-build-pattern-start', patternlab);
//
// CHECK INCREMENTAL BUILD GRAPH
//
const graph = (patternlab.graph = loadPatternGraph(deletePatternDir));
const graphNeedsUpgrade = !PatternGraph.checkVersion(graph);
if (graphNeedsUpgrade) {
logger.info(
'Due to an upgrade, a complete rebuild is required and the public/patterns directory was deleted. ' +
'Incremental build is available again on the next successful run.'
);
// Ensure that the freshly built graph has the latest version again.
patternlab.graph.upgradeVersion();
}
// Flags
patternlab.incrementalBuildsEnabled = !(
deletePatternDir || graphNeedsUpgrade
);
//
// CLEAN BUILD DIRECTORY, maybe
//
cleanBuildDirectory(patternlab.incrementalBuildsEnabled);
patternlab.buildGlobalData(additionalData);
return patternlab
.processAllPatternsIterative(paths.source.patterns)
.then(() => {
patternlab.events.emit('patternlab-pattern-iteration-end', patternlab);
//now that all the main patterns are known, look for any links that might be within data and expand them
//we need to do this before expanding patterns & partials into extendedTemplates, otherwise we could lose the data -> partial reference
parseAllLinks(patternlab);
//dive again to recursively include partials, filling out the
//extendedTemplate property of the patternlab.patterns elements
return patternlab
.processAllPatternsRecursive(paths.source.patterns)
.then(() => {
//take the user defined head and foot and process any data and patterns that apply
const headPatternPromise = processMetaPattern(
`_00-head.${patternlab.config.patternExtension}`,
'userHead',
patternlab
);
const footPatternPromise = processMetaPattern(
`_01-foot.${patternlab.config.patternExtension}`,
'userFoot',
patternlab
);
return Promise.all([headPatternPromise, footPatternPromise])
.then(() => {
//cascade any patternStates
lineage_hunter.cascade_pattern_states(patternlab);
//set the pattern-specific header by compiling the general-header with data, and then adding it to the meta header
return render(
Pattern.createEmpty({ extendedTemplate: patternlab.header }),
{
cacheBuster: patternlab.cacheBuster,
}
)
.then(results => {
patternlab.data.patternLabHead = results;
// If deletePatternDir == true or graph needs to be updated
// rebuild all patterns
let patternsToBuild = null;
// If deletePatternDir == true or graph needs to be updated
// rebuild all patterns
patternsToBuild = null;
if (patternlab.incrementalBuildsEnabled) {
// When the graph was loaded from file, some patterns might have been moved/deleted between runs
// so the graph data become out of sync
patternlab.graph.sync().forEach(n => {
logger.info('[Deleted/Moved] ' + n);
});
// TODO Find created or deleted files
const now = new Date().getTime();
markModifiedPatterns(now, patternlab);
patternsToBuild = patternlab.graph.compileOrder();
} else {
// build all patterns, mark all to be rebuilt
patternsToBuild = patternlab.patterns;
for (const p of patternsToBuild) {
p.compileState = CompileState.NEEDS_REBUILD;
}
}
//render all patterns last, so lineageR works
return patternsToBuild
.reduce((previousPromise, pattern) => {
return previousPromise.then(() =>
patternlab.renderSinglePattern(pattern)
);
}, Promise.resolve())
.then(() => {
// Saves the pattern graph when all files have been compiled
PatternGraph.storeToFile(patternlab);
if (patternlab.config.exportToGraphViz) {
PatternGraph.exportToDot(
patternlab,
'dependencyGraph.dot'
);
logger.info(
`Exported pattern graph to ${path.join(
config.paths.public.root,
'dependencyGraph.dot'
)}`
);
}
//export patterns if necessary
pattern_exporter.export_patterns(patternlab);
})
.catch(reason => {
console.log(reason);
logger.error('Error rendering patterns');
});
})
.catch(reason => {
console.log(reason);
logger.error('Error rendering pattern lab header');
});
})
.catch(reason => {
console.log(reason);
logger.error('Error processing meta patterns');
});
})
.catch(reason => {
console.log(reason);
logger.error('Error processing patterns recursively');
});
})
.catch(reason => {
console.log(reason);
logger.error('Error in buildPatterns()');
});
}
return {
/**
* logs current version
*
* @returns {void} current patternlab-node version as defined in package.json, as console output
*/
version: function() {
return patternlab.logVersion();
},
/**
* return current version
*
* @returns {string} current patternlab-node version as defined in package.json, as string
*/
v: function() {
return patternlab.getVersion();
},
/**
* build patterns, copy assets, and construct ui
*
* @param {object} options an object used to control build behavior
* @returns {Promise} a promise fulfilled when build is complete
*/
build: function(options) {
if (patternlab && patternlab.isBusy) {
logger.info(
'Pattern Lab is busy building a previous run - returning early.'
);
return Promise.resolve();
}
patternlab.isBusy = true;
return buildPatterns(options.cleanPublic, options.data).then(() => {
return new ui_builder().buildFrontend(patternlab).then(() => {
assetCopier().copyAssets(
patternlab.config.paths,
patternlab,
options
);
this.events.on('patternlab-pattern-change', () => {
if (!patternlab.isBusy) {
options.cleanPublic = false;
return this.build(options);
}
return Promise.resolve();
});
this.events.on('patternlab-global-change', () => {
if (!patternlab.isBusy) {
options.cleanPublic = true; //rebuild everything
return this.build(options);
}
return Promise.resolve();
});
patternlab.isBusy = false;
});
});
},
/**
* logs usage
*
* @returns {void} pattern lab API usage, as console output
*/
help: function() {
help();
},
/**
* build patterns only, leaving existing public files intact
*
* @param {object} options an object used to control build behavior
* @returns {Promise} a promise fulfilled when build is complete
*/
patternsonly: function(options) {
if (patternlab && patternlab.isBusy) {
logger.info(
'Pattern Lab is busy building a previous run - returning early.'
);
return Promise.resolve();
}
patternlab.isBusy = true;
return buildPatterns(options.cleanPublic, options.data).then(() => {
patternlab.isBusy = false;
});
},
/**
* fetches starterkit repos from pattern-lab github org that contain 'starterkit' in their name
*
* @returns {Promise} Returns an Array<{name,url}> for the starterkit repos
*/
liststarterkits: function() {
return patternlab.listStarterkits();
},
/**
* load starterkit already available via `node_modules/`
*
* @param {string} starterkitName name of starterkit
* @param {boolean} clean whether or not to delete contents of source/ before load
* @returns {void}
*/
loadstarterkit: function(starterkitName, clean) {
patternlab.loadStarterKit(starterkitName, clean);
},
/**
* install plugin already available via `node_modules/`
*
* @param {string} pluginName name of plugin
* @returns {void}
*/
installplugin: function(pluginName) {
patternlab.installPlugin(pluginName);
},
/**
* returns all file extensions supported by installed PatternEngines
*
* @returns {Array<string>} all supported file extensions
*/
getSupportedTemplateExtensions: function() {
return patternlab.getSupportedTemplateExtensions();
},
/**
* build patterns, copy assets, and construct ui, watch source files, and serve locally
*
* @param {object} options an object used to control build, copy, and serve behavior
* @returns {Promise} TODO: validate
*/
serve: function(options) {
options.watch = true;
return this.build(options).then(function() {
serve(patternlab);
return Promise.resolve();
});
},
events: patternlab.events,
};
};
patternlab_module.getDefaultConfig = getDefaultConfig;
module.exports = patternlab_module;