Skip to content

Commit 4a2fe28

Browse files
Merge pull request #1066 from pattern-lab/πŸ”Œ
πŸ”Œ
2 parents d2aa1be + 02e6858 commit 4a2fe28

24 files changed

+243
-149
lines changed

β€Žpackages/cli/bin/install-plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const installPlugin = (plugin, config) =>
2222
try {
2323
const pluginConfigJSON = require(pluginPathConfig);
2424
if (!_.has(config.plugins[name].options)) {
25-
_.set(config, `plugins[${name}]['options]`, pluginConfigJSON);
25+
_.set(config, `plugins[${name}][options]`, pluginConfigJSON);
2626
}
2727
} catch (ex) {
2828
//a config.json file is not required at this time

β€Žpackages/core/src/index.js

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const patternlab_module = function(config) {
9292
* @see {@link ./events.md|all events}
9393
* @returns {Promise} a promise fulfilled when build is complete
9494
*/
95-
build: function(options) {
95+
build: async function(options) {
9696
// process.on('unhandledRejection', (reason, p) => {
9797
// console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
9898
// // application specific logging, throwing an error, or other logic here
@@ -109,53 +109,51 @@ const patternlab_module = function(config) {
109109
}
110110
patternlab.isBusy = true;
111111

112-
return buildPatterns(options.cleanPublic, patternlab, options.data).then(
113-
() => {
114-
return new ui_builder().buildFrontend(patternlab).then(() => {
115-
copier()
116-
.copyAndWatch(patternlab.config.paths, patternlab, options)
117-
.then(() => {
118-
patternlab.isBusy = false;
119-
// only wire up this listener and the one inside serve.js
120-
// figure out how to detect if serve was called. we should not assume it was
121-
if (
122-
patternlab.serverReady //check for server presence
123-
? this.events.listenerCount(
124-
events.PATTERNLAB_PATTERN_CHANGE
125-
) === 1 //if the server is started, it has already setup one listener
126-
: !this.events.listenerCount(
127-
events.PATTERNLAB_PATTERN_CHANGE
128-
) // else, check for the presnce of none
129-
) {
130-
this.events.on(events.PATTERNLAB_PATTERN_CHANGE, () => {
131-
if (!patternlab.isBusy) {
132-
return this.build(options).then(() => {
133-
patternlab.isBusy = false;
134-
});
135-
}
136-
return Promise.resolve();
137-
});
138-
}
112+
return await buildPatterns(
113+
options.cleanPublic,
114+
patternlab,
115+
options.data
116+
).then(() => {
117+
return new ui_builder().buildFrontend(patternlab).then(() => {
118+
copier()
119+
.copyAndWatch(patternlab.config.paths, patternlab, options)
120+
.then(() => {
121+
patternlab.isBusy = false;
122+
// only wire up this listener and the one inside serve.js
123+
// figure out how to detect if serve was called. we should not assume it was
124+
if (
125+
patternlab.serverReady //check for server presence
126+
? this.events.listenerCount(
127+
events.PATTERNLAB_PATTERN_CHANGE
128+
) === 1 //if the server is started, it has already setup one listener
129+
: !this.events.listenerCount(events.PATTERNLAB_PATTERN_CHANGE) // else, check for the presnce of none
130+
) {
131+
this.events.on(events.PATTERNLAB_PATTERN_CHANGE, () => {
132+
if (!patternlab.isBusy) {
133+
return this.build(options).then(() => {
134+
patternlab.isBusy = false;
135+
});
136+
}
137+
return Promise.resolve();
138+
});
139+
}
139140

140-
if (
141-
!this.events.listenerCount(events.PATTERNLAB_GLOBAL_CHANGE)
142-
) {
143-
this.events.on(events.PATTERNLAB_GLOBAL_CHANGE, () => {
144-
if (!patternlab.isBusy) {
145-
return this.build(
146-
Object.assign({}, options, { cleanPublic: true }) // rebuild everything
147-
);
148-
}
149-
return Promise.resolve();
150-
});
151-
}
152-
})
153-
.then(() => {
154-
this.events.emit(events.PATTERNLAB_BUILD_END, patternlab);
155-
});
156-
});
157-
}
158-
);
141+
if (!this.events.listenerCount(events.PATTERNLAB_GLOBAL_CHANGE)) {
142+
this.events.on(events.PATTERNLAB_GLOBAL_CHANGE, () => {
143+
if (!patternlab.isBusy) {
144+
return this.build(
145+
Object.assign({}, options, { cleanPublic: true }) // rebuild everything
146+
);
147+
}
148+
return Promise.resolve();
149+
});
150+
}
151+
})
152+
.then(() => {
153+
this.events.emit(events.PATTERNLAB_BUILD_END, patternlab);
154+
});
155+
});
156+
});
159157
},
160158

161159
/**
@@ -194,7 +192,7 @@ const patternlab_module = function(config) {
194192
installplugin: function(pluginName) {
195193
//get the config
196194
const configPath = path.resolve(process.cwd(), 'patternlab-config.json');
197-
const plugin_manager = new pm(config, configPath);
195+
const plugin_manager = new pm();
198196

199197
plugin_manager.install_plugin(pluginName);
200198
},
@@ -236,19 +234,21 @@ const patternlab_module = function(config) {
236234
* @param {bool} [options.watch=true] whether or not Pattern Lab should watch configured `source/` directories for changes to rebuild
237235
* @returns {Promise} a promise fulfilled when build is complete
238236
*/
239-
patternsonly: function(options) {
237+
patternsonly: async function(options) {
240238
if (patternlab && patternlab.isBusy) {
241239
logger.info(
242240
'Pattern Lab is busy building a previous run - returning early.'
243241
);
244242
return Promise.resolve();
245243
}
246244
patternlab.isBusy = true;
247-
return buildPatterns(options.cleanPublic, patternlab, options.data).then(
248-
() => {
249-
patternlab.isBusy = false;
250-
}
251-
);
245+
return await buildPatterns(
246+
options.cleanPublic,
247+
patternlab,
248+
options.data
249+
).then(() => {
250+
patternlab.isBusy = false;
251+
});
252252
},
253253

254254
/**

β€Žpackages/core/src/lib/buildPatterns.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const CompileState = require('./object_factory').CompileState;
1414
const processMetaPattern = require('./processMetaPattern');
1515
const pe = require('./pattern_exporter');
1616
const lh = require('./lineage_hunter');
17+
const pm = require('./plugin_manager');
18+
const pluginMananger = new pm();
1719
const markModifiedPatterns = require('./markModifiedPatterns');
1820
const parseAllLinks = require('./parseAllLinks');
1921
const render = require('./render');
@@ -24,8 +26,12 @@ let pattern_exporter = new pe(); // eslint-disable-line
2426

2527
const lineage_hunter = new lh();
2628

27-
module.exports = (deletePatternDir, patternlab, additionalData) => {
28-
patternlab.events.emit(events.PATTERNLAB_BUILD_START, patternlab);
29+
module.exports = async (deletePatternDir, patternlab, additionalData) => {
30+
await pluginMananger.raiseEvent(
31+
patternlab,
32+
events.PATTERNLAB_BUILD_START,
33+
patternlab
34+
);
2935

3036
const paths = patternlab.config.paths;
3137

@@ -63,8 +69,9 @@ module.exports = (deletePatternDir, patternlab, additionalData) => {
6369

6470
return patternlab
6571
.processAllPatternsIterative(paths.source.patterns)
66-
.then(() => {
67-
patternlab.events.emit(
72+
.then(async () => {
73+
await pluginMananger.raiseEvent(
74+
patternlab,
6875
events.PATTERNLAB_PATTERN_ITERATION_END,
6976
patternlab
7077
);
@@ -140,11 +147,12 @@ module.exports = (deletePatternDir, patternlab, additionalData) => {
140147
}
141148
}
142149
//render all patterns last, so lineageR works
143-
const allPatternsPromise = patternsToBuild.map(pattern =>
144-
compose(
145-
pattern,
146-
patternlab
147-
)
150+
const allPatternsPromise = patternsToBuild.map(
151+
async pattern =>
152+
await compose(
153+
pattern,
154+
patternlab
155+
)
148156
);
149157
//copy non-pattern files like JavaScript
150158
const allJS = patternsToBuild.map(pattern => {

β€Žpackages/core/src/lib/compose.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ const logger = require('./log');
88
const parseLink = require('./parseLink');
99
const render = require('./render');
1010
const uikitExcludePattern = require('./uikitExcludePattern');
11+
const pm = require('./plugin_manager');
12+
const pluginMananger = new pm();
1113

1214
const Pattern = require('./object_factory').Pattern;
1315
const CompileState = require('./object_factory').CompileState;
1416

15-
module.exports = function(pattern, patternlab) {
17+
module.exports = async function(pattern, patternlab) {
1618
// Pattern does not need to be built and recompiled more than once
1719
if (!pattern.isPattern || pattern.compileState === CompileState.CLEAN) {
1820
return Promise.resolve(false);
@@ -30,7 +32,8 @@ module.exports = function(pattern, patternlab) {
3032
pattern.patternLineageEExists =
3133
pattern.patternLineageExists || pattern.patternLineageRExists;
3234

33-
patternlab.events.emit(
35+
await pluginMananger.raiseEvent(
36+
patternlab,
3437
events.PATTERNLAB_PATTERN_BEFORE_DATA_MERGE,
3538
patternlab,
3639
pattern
@@ -167,36 +170,40 @@ module.exports = function(pattern, patternlab) {
167170
allFooterData.cacheBuster = patternlab.cacheBuster;
168171
allFooterData.patternLabFoot = footerPartial;
169172

170-
return render(patternlab.userFoot, allFooterData).then(footerHTML => {
171-
///////////////
172-
// WRITE FILES
173-
///////////////
174-
175-
patternlab.events.emit(
176-
events.PATTERNLAB_PATTERN_WRITE_BEGIN,
177-
patternlab,
178-
pattern
179-
);
180-
181-
//write the compiled template to the public patterns directory
182-
patternlab.writePatternFiles(
183-
headHTML,
184-
pattern,
185-
footerHTML,
186-
uikit.outputDir
187-
);
188-
189-
patternlab.events.emit(
190-
events.PATTERNLAB_PATTERN_WRITE_END,
191-
patternlab,
192-
pattern
193-
);
194-
195-
// Allows serializing the compile state
196-
patternlab.graph.node(pattern).compileState = pattern.compileState =
197-
CompileState.CLEAN;
198-
logger.info('Built pattern: ' + pattern.patternPartial);
199-
});
173+
return render(patternlab.userFoot, allFooterData).then(
174+
async footerHTML => {
175+
///////////////
176+
// WRITE FILES
177+
///////////////
178+
await pluginMananger.raiseEvent(
179+
patternlab,
180+
events.PATTERNLAB_PATTERN_WRITE_BEGIN,
181+
patternlab,
182+
pattern
183+
);
184+
185+
//write the compiled template to the public patterns directory
186+
patternlab.writePatternFiles(
187+
headHTML,
188+
pattern,
189+
footerHTML,
190+
uikit.outputDir
191+
);
192+
193+
await pluginMananger.raiseEvent(
194+
patternlab,
195+
events.PATTERNLAB_PATTERN_WRITE_END,
196+
patternlab,
197+
pattern
198+
);
199+
200+
// Allows serializing the compile state
201+
patternlab.graph.node(
202+
pattern
203+
).compileState = pattern.compileState = CompileState.CLEAN;
204+
logger.info('Built pattern: ' + pattern.patternPartial);
205+
}
206+
);
200207
})
201208
.catch(reason => {
202209
console.log(reason);

β€Žpackages/core/src/lib/patternlab.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ module.exports = class PatternLab {
6464
// Make ye olde event emitter
6565
this.events = new PatternLabEventEmitter();
6666

67+
this.hooks = {};
68+
this.hooks[events.PATTERNLAB_PATTERN_WRITE_END] = [];
69+
6770
// Make a place for the pattern graph to sit
6871
this.graph = null;
6972

β€Žpackages/core/src/lib/plugin_manager.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,27 @@ const plugin_manager = function() {
3939
*/
4040
function initializePlugins(patternlab) {
4141
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
42-
const foundPlugins = findModules(nodeModulesPath, plugin_manager.is_plugin);
42+
const foundPlugins = findModules(nodeModulesPath, isPlugin);
4343
foundPlugins.forEach(plugin => {
4444
logger.info(`Found plugin: plugin-${plugin.name}`);
4545
logger.info(`Attempting to load and initialize plugin.`);
46-
const pluginModule = plugin_manager.load_plugin(plugin.modulePath);
46+
const pluginModule = loadPlugin(plugin.modulePath);
4747
pluginModule(patternlab);
4848
});
4949
}
5050

51+
async function raiseEvent(patternlab, eventName, ...args) {
52+
patternlab.events.emit(eventName, args);
53+
54+
await (async function() {
55+
const hookHandlers = (patternlab.hooks[eventName] || []).map(h =>
56+
h(args)
57+
);
58+
59+
const results = await Promise.all(hookHandlers);
60+
})();
61+
}
62+
5163
return {
5264
intialize_plugins: patternlab => {
5365
initializePlugins(patternlab);
@@ -58,6 +70,9 @@ const plugin_manager = function() {
5870
is_plugin: filePath => {
5971
return isPlugin(filePath);
6072
},
73+
raiseEvent: async (patternlab, eventName, ...args) => {
74+
await raiseEvent(patternlab, eventName, args);
75+
},
6176
};
6277
};
6378

0 commit comments

Comments
Β (0)