Skip to content

Commit d45e7b9

Browse files
committed
feat(docs): Add jsdoc output to public API and events
1 parent 2b6354a commit d45e7b9

File tree

11 files changed

+1116
-90
lines changed

11 files changed

+1116
-90
lines changed

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./scripts/api.handlebars
2+
./scripts/events.handlebars

core/index.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const packageInfo = require('../package.json');
1515
const path = require('path');
1616
const updateNotifier = require('update-notifier');
1717

18+
const events = require('./events');
1819
const logger = require('./lib/log');
1920
const PatternGraph = require('./lib/pattern_graph').PatternGraph;
2021
const CompileState = require('./lib/object_factory').CompileState;
@@ -43,9 +44,9 @@ updateNotifier({
4344
}).notify();
4445

4546
/**
46-
* Returns the standardized default config
47+
* Returns the standardized default config used to run Pattern Lab. This method can be called statically or after instantiation.
4748
*
48-
* @return {object} Returns the object representation of the patternlab-config.json
49+
* @return {object} Returns the object representation of the `patternlab-config.json`
4950
*/
5051
const getDefaultConfig = function() {
5152
return defaultConfig;
@@ -175,7 +176,7 @@ const patternlab_module = function(config) {
175176
}
176177

177178
function buildPatterns(deletePatternDir, additionalData) {
178-
patternlab.events.emit('patternlab-build-pattern-start', patternlab);
179+
patternlab.events.emit(events.PATTERNLAB_BUILD_PATTERN_START, patternlab);
179180

180181
//
181182
// CHECK INCREMENTAL BUILD GRAPH
@@ -207,7 +208,7 @@ const patternlab_module = function(config) {
207208
.processAllPatternsIterative(paths.source.patterns)
208209
.then(() => {
209210
patternlab.events.emit(
210-
'patternlab-pattern-iteration-end',
211+
events.PATTERNLAB_PATTERN_ITERATION_END,
211212
patternlab
212213
);
213214

@@ -332,27 +333,30 @@ const patternlab_module = function(config) {
332333

333334
return {
334335
/**
335-
* logs current version
336+
* Logs current version to standard output
336337
*
337-
* @returns {void} current patternlab-node version as defined in package.json, as console output
338+
* @returns {void} current patternlab-node version as defined in `package.json`
338339
*/
339340
version: function() {
340341
return patternlab.logVersion();
341342
},
342343

343344
/**
344-
* return current version
345+
* Returns current version
345346
*
346-
* @returns {string} current patternlab-node version as defined in package.json, as string
347+
* @returns {string} current patternlab-node version as defined in `package.json`, as string
347348
*/
348349
v: function() {
349350
return patternlab.getVersion();
350351
},
351352

352353
/**
353-
* build patterns, copy assets, and construct ui
354+
* Builds patterns, copies assets, and constructs user interface
354355
*
355356
* @param {object} options an object used to control build behavior
357+
* @param {bool} options.cleanPublic whether or not to delete the configured output location (usually `public/`) before build
358+
* @param {object} options.data additional data to be merged with global data prior to build
359+
* @param {bool} options.watch whether or not Pattern Lab should watch configured `source/` directories for changes to rebuild
356360
* @returns {Promise} a promise fulfilled when build is complete
357361
*/
358362
build: function(options) {
@@ -393,16 +397,16 @@ const patternlab_module = function(config) {
393397
},
394398

395399
/**
396-
* Returns the standardized default config
400+
* Returns the standardized default config used to run Pattern Lab. This method can be called statically or after instantiation.
397401
*
398-
* @return {object} Returns the object representation of the patternlab-config.json
402+
* @return {object} Returns the object representation of the `patternlab-config.json`
399403
*/
400404
getDefaultConfig: function() {
401405
return getDefaultConfig();
402406
},
403407

404408
/**
405-
* returns all file extensions supported by installed PatternEngines
409+
* Returns all file extensions supported by installed PatternEngines
406410
*
407411
* @returns {Array<string>} all supported file extensions
408412
*/
@@ -411,7 +415,7 @@ const patternlab_module = function(config) {
411415
},
412416

413417
/**
414-
* logs usage
418+
* Logs usage to standard output
415419
*
416420
* @returns {void} pattern lab API usage, as console output
417421
*/
@@ -420,7 +424,7 @@ const patternlab_module = function(config) {
420424
},
421425

422426
/**
423-
* install plugin already available via `node_modules/`
427+
* Installs plugin already available via `node_modules/`
424428
*
425429
* @param {string} pluginName name of plugin
426430
* @returns {void}
@@ -430,7 +434,7 @@ const patternlab_module = function(config) {
430434
},
431435

432436
/**
433-
* fetches starterkit repos from pattern-lab github org that contain 'starterkit' in their name
437+
* Fetches starterkit repositories from pattern-lab github org that contain 'starterkit' in their name
434438
*
435439
* @returns {Promise} Returns an Array<{name,url}> for the starterkit repos
436440
*/
@@ -439,7 +443,7 @@ const patternlab_module = function(config) {
439443
},
440444

441445
/**
442-
* load starterkit already available via `node_modules/`
446+
* Loads starterkit already available via `node_modules/`
443447
*
444448
* @param {string} starterkitName name of starterkit
445449
* @param {boolean} clean whether or not to delete contents of source/ before load
@@ -450,9 +454,11 @@ const patternlab_module = function(config) {
450454
},
451455

452456
/**
453-
* build patterns only, leaving existing public files intact
457+
* Builds patterns only, leaving existing user interface files intact
454458
*
455459
* @param {object} options an object used to control build behavior
460+
* @param {bool} options.cleanPublic whether or not to delete the configured output location (usually `public/`) before build
461+
* @param {object} options.data additional data to be merged with global data prior to build
456462
* @returns {Promise} a promise fulfilled when build is complete
457463
*/
458464
patternsonly: function(options) {
@@ -469,10 +475,13 @@ const patternlab_module = function(config) {
469475
},
470476

471477
/**
472-
* build patterns, copy assets, and construct ui, watch source files, and serve locally
478+
* Build patterns, copies assets, and constructs user interface. Watches configured `source/` directories, and serves all output locally
473479
*
474-
* @param {object} options an object used to control build, copy, and serve behavior
475-
* @returns {Promise} TODO: validate
480+
* @param {object} options an object used to control build behavior
481+
* @param {bool} options.cleanPublic whether or not to delete the configured output location (usually `public/`) before build
482+
* @param {object} options.data additional data to be merged with global data prior to build
483+
* @param {bool} options.watch **ALWAYS OVERRIDDEN to `true`** whether or not Pattern Lab should watch configured `source/` directories for changes to rebuild
484+
* @returns {Promise} a promise fulfilled when build is complete
476485
*/
477486
serve: function(options) {
478487
options.watch = true;

core/lib/asset_copy.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
const _ = require('lodash');
33
const path = require('path');
44
const process = require('process');
5+
6+
const events = require('./events');
57
const logger = require('./log');
68

79
let copy = require('recursive-copy'); // eslint-disable-line prefer-const
@@ -43,7 +45,7 @@ const asset_copier = () => {
4345
const copyFile = (p, dest, options) => {
4446
copy(p, dest, options).on(copy.events.COPY_FILE_COMPLETE, () => {
4547
logger.debug(`Moved ${p} to ${dest}`);
46-
options.emitter.emit('patternlab-asset-change', {
48+
options.emitter.emit(events.PATTERNLAB_PATTERN_ASSET_CHANGE, {
4749
file: p,
4850
dest: dest,
4951
});
@@ -163,17 +165,17 @@ const asset_copier = () => {
163165
//watch for changes and rebuild
164166
globalWatcher
165167
.on('addDir', p => {
166-
patternlab.events.emit('patternlab-global-change', {
168+
patternlab.events.emit(events.PATTERNLAB_GLOBAL_CHANGE, {
167169
file: p,
168170
});
169171
})
170172
.on('add', p => {
171-
patternlab.events.emit('patternlab-global-change', {
173+
patternlab.events.emit(events.PATTERNLAB_GLOBAL_CHANGE, {
172174
file: p,
173175
});
174176
})
175177
.on('change', p => {
176-
patternlab.events.emit('patternlab-global-change', {
178+
patternlab.events.emit(events.PATTERNLAB_GLOBAL_CHANGE, {
177179
file: p,
178180
});
179181
});
@@ -207,17 +209,17 @@ const asset_copier = () => {
207209
//watch for changes and rebuild
208210
patternWatcher
209211
.on('addDir', p => {
210-
patternlab.events.emit('patternlab-pattern-change', {
212+
patternlab.events.emit(events.PATTERNLAB_PATTERN_CHANGE, {
211213
file: p,
212214
});
213215
})
214216
.on('add', p => {
215-
patternlab.events.emit('patternlab-pattern-change', {
217+
patternlab.events.emit(events.PATTERNLAB_PATTERN_CHANGE, {
216218
file: p,
217219
});
218220
})
219221
.on('change', p => {
220-
patternlab.events.emit('patternlab-pattern-change', {
222+
patternlab.events.emit(events.PATTERNLAB_PATTERN_CHANGE, {
221223
file: p,
222224
});
223225
});

core/lib/events.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
/**
4+
* All Pattern Lab Events
5+
* @module Events
6+
*/
7+
8+
/**
9+
* @alias module:Events
10+
*/
11+
const EVENTS = Object.freeze({
12+
/**
13+
* @desc Emitted before any logic run inside `build()`, which is the entry point for single builds, pattern-only builds, run singly or when watched.
14+
* @property {object} patternlab - global data store
15+
*
16+
*/
17+
PATTERNLAB_BUILD_PATTERN_START: 'patternlab-build-pattern-start',
18+
19+
/**
20+
* @desc Emitted after patterns are iterated over to gather data about them. Right before Pattern Lab processes and renders patterns into HTML
21+
* @property {object} patternlab - global data store
22+
*/
23+
PATTERNLAB_PATTERN_ITERATION_END: 'patternlab-pattern-iteration-end',
24+
25+
/**
26+
* @desc Emitted after global `data.json` and `listitems.json` are read, and the supporting Pattern Lab templates are loaded into memory (header, footer, patternSection, patternSectionSubType, viewall). Right before patterns are iterated over to gather data about them.
27+
* @property {object} patternlab - global data store
28+
*/
29+
PATTERNLAB_BUILD_GLOBAL_DATA_END: 'patternlab-build-global-data-end',
30+
31+
/**
32+
* @desc Emitted before all data is merged prior to a Pattern's render. Global `data.json` is merged with any pattern `.json`. Global `listitems.json` is merged with any pattern `.listitems.json`.
33+
* @property {object} patternlab - global data store
34+
* @property {Pattern} pattern - current pattern
35+
* @see {@link https://github.com/pattern-lab/patternlab-node/blob/master/core/lib/object_factory.js#L16|Pattern}
36+
*/
37+
PATTERNLAB_PATTERN_BEFORE_DATA_MERGE: 'patternlab-pattern-before-data-merge',
38+
39+
/**
40+
* @desc Emitted before a pattern's template, HTML, and encoded HTML files are written to their output location
41+
* @property {object} patternlab - global data store
42+
* @property {Pattern} pattern - current pattern
43+
* @see {@link https://github.com/pattern-lab/patternlab-node/blob/master/core/lib/object_factory.js#L16|Pattern}
44+
*/
45+
PATTERNLAB_PATTERN_WRITE_BEGIN: 'patternlab-pattern-write-begin',
46+
47+
/**
48+
* @desc Emitted after a pattern's template, HTML, and encoded HTML files are written to their output location
49+
* @property {object} patternlab - global data store
50+
* @property {Pattern} pattern - current pattern
51+
* @see {@link https://github.com/pattern-lab/patternlab-node/blob/master/core/lib/object_factory.js#L16|Pattern}
52+
*/
53+
PATTERNLAB_PATTERN_WRITE_END: 'patternlab-pattern-write-end',
54+
55+
/**
56+
* @desc Invoked when a watched asset changes. Assets include anything in `source/` that is not under `['root', 'patterns', 'data', 'meta', 'annotations', 'patternlabFiles']` which are blacklisted for specific copying.
57+
* @property {object} fileInfo - `{file: 'path/to/file.css', dest: 'path/to/destination'}`
58+
*/
59+
PATTERNLAB_PATTERN_ASSET_CHANGE: 'patternlab-pattern-asset-change',
60+
61+
/**
62+
* @desc Invoked when a watched global file changes. These are files within the directories specified in `['data', 'meta']`paths.
63+
* @property {object} fileInfo - `{file: 'path/to/file.ext'}`
64+
*/
65+
PATTERNLAB_GLOBAL_CHANGE: 'patternlab-global-change',
66+
67+
/**
68+
* @desc Invoked when a pattern changes.
69+
* @property {object} fileInfo - `{file: 'path/to/file.ext'}`
70+
*/
71+
PATTERNLAB_PATTERN_CHANGE: 'patternlab-pattern-change',
72+
});
73+
74+
module.exports = EVENTS;

core/lib/patternlab.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const cleanHtml = require('js-beautify').html;
88
const inherits = require('util').inherits;
99
const pm = require('./plugin_manager');
1010
const packageInfo = require('../../package.json');
11+
const events = require('./events');
1112
const buildListItems = require('./buildListItems');
1213
const dataLoader = require('./data_loader')();
1314
const logger = require('./log');
@@ -230,7 +231,7 @@ module.exports = class PatternLab {
230231

231232
buildListItems(this);
232233

233-
this.events.emit('patternlab-build-global-data-end', this);
234+
this.events.emit(events.PATTERNLAB_BUILD_GLOBAL_DATA_END, this);
234235
}
235236

236237
setCacheBust() {
@@ -360,7 +361,11 @@ module.exports = class PatternLab {
360361
pattern.patternLineageEExists =
361362
pattern.patternLineageExists || pattern.patternLineageRExists;
362363

363-
this.events.emit('patternlab-pattern-before-data-merge', this, pattern);
364+
this.events.emit(
365+
events.PATTERNLAB_PATTERN_BEFORE_DATA_MERGE,
366+
this,
367+
pattern
368+
);
364369

365370
//render the pattern, but first consolidate any data we may have
366371
let allData;
@@ -485,12 +490,16 @@ module.exports = class PatternLab {
485490
// WRITE FILES
486491
///////////////
487492

488-
self.events.emit('patternlab-pattern-write-begin', self, pattern);
493+
self.events.emit(
494+
events.PATTERNLAB_PATTERN_WRITE_BEGIN,
495+
self,
496+
pattern
497+
);
489498

490499
//write the compiled template to the public patterns directory
491500
self.writePatternFiles(headHTML, pattern, footerHTML);
492501

493-
self.events.emit('patternlab-pattern-write-end', self, pattern);
502+
self.events.emit(events.PATTERNLAB_PATTERN_WRITE_END, self, pattern);
494503

495504
// Allows serializing the compile state
496505
self.graph.node(pattern).compileState = pattern.compileState =

0 commit comments

Comments
 (0)