Skip to content

Commit 2f3d39a

Browse files
committed
feat(plugin-tab, core): initial plugin hook exploration
1 parent 9fc2f53 commit 2f3d39a

File tree

8 files changed

+78
-35
lines changed

8 files changed

+78
-35
lines changed

packages/core/src/lib/compose.js

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -167,36 +167,57 @@ module.exports = function(pattern, patternlab) {
167167
allFooterData.cacheBuster = patternlab.cacheBuster;
168168
allFooterData.patternLabFoot = footerPartial;
169169

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-
});
170+
return render(patternlab.userFoot, allFooterData).then(
171+
async footerHTML => {
172+
///////////////
173+
// WRITE FILES
174+
///////////////
175+
176+
patternlab.events.emit(
177+
events.PATTERNLAB_PATTERN_WRITE_BEGIN,
178+
patternlab,
179+
pattern
180+
);
181+
182+
//write the compiled template to the public patterns directory
183+
patternlab.writePatternFiles(
184+
headHTML,
185+
pattern,
186+
footerHTML,
187+
uikit.outputDir
188+
);
189+
190+
patternlab.events.emit(
191+
events.PATTERNLAB_PATTERN_WRITE_END,
192+
patternlab,
193+
pattern
194+
);
195+
196+
console.log(196);
197+
198+
(async function() {
199+
const hookHandlers = patternlab.hooks[
200+
events.PATTERNLAB_PATTERN_WRITE_END
201+
].forEach(h => h());
202+
console.log(203, hookHandlers.length);
203+
204+
const results = await Promise.all(hookHandlers);
205+
console.log(205, results);
206+
})();
207+
208+
console.log(208);
209+
210+
await patternlab.hooks[events.PATTERNLAB_PATTERN_WRITE_END];
211+
212+
console.log(212);
213+
214+
// Allows serializing the compile state
215+
patternlab.graph.node(
216+
pattern
217+
).compileState = pattern.compileState = CompileState.CLEAN;
218+
logger.info('Built pattern: ' + pattern.patternPartial);
219+
}
220+
);
200221
})
201222
.catch(reason => {
202223
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/development-edition-engine-handlebars/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@pattern-lab/engine-handlebars": "^2.0.1",
3333
"@pattern-lab/engine-mustache": "^2.0.1-alpha.0",
3434
"@pattern-lab/starterkit-mustache-demo": "^5.0.0",
35-
"@pattern-lab/uikit-workshop": "^1.0.3"
35+
"@pattern-lab/uikit-workshop": "^1.0.3",
36+
"@pattern-lab/plugin-tab": "2.0.3-beta.1"
3637
}
3738
}

packages/development-edition-engine-handlebars/patternlab-config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,14 @@
9090
"handlebars": {
9191
"extend": "helpers/*.js"
9292
}
93+
},
94+
"plugins": {
95+
"pattern-lab-plugin-tab": {
96+
"enabled": true,
97+
"initialized": false,
98+
"options": {
99+
"tabsToAdd": ["scss"]
100+
}
101+
}
93102
}
94103
}

packages/development-edition-engine-handlebars/source/_patterns/atoms/swatches/swatch.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
<span class="sg-label {{#if inverted}}sg-label__inverted{{/if}} sg-label__bottom">hex: {{color.hex}}</span>
44
<span class="sg-label {{#if inverted}}sg-label__inverted{{/if}} sg-label__bottom">cmyk: {{color.cmyk}}</span>
55
</div>
6+
swatch
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* {
2+
color: rebeccapurple
3+
}

packages/plugin-tab/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ function writeConfigToOutput(patternlab, pluginConfig) {
3434
}
3535
}
3636

37-
function onPatternIterate(patternlab, pattern) {
38-
tab_loader(patternlab, pattern);
37+
async function onPatternIterate(patternlab, pattern) {
38+
await tab_loader(patternlab, pattern);
3939
}
4040

4141
/**
@@ -48,6 +48,10 @@ function registerEvents(patternlab) {
4848
patternlab.events.on('patternlab-pattern-write-end', onPatternIterate);
4949
}
5050

51+
function registerEventHandlers(patternlab) {
52+
patternlab.hooks['patternlab-pattern-write-end'].push(onPatternIterate);
53+
}
54+
5155
/**
5256
* A single place to define the frontend configuration
5357
* This configuration is outputted to the frontend explicitly as well as included in the plugins object.
@@ -169,7 +173,8 @@ function pluginInit(patternlab) {
169173
!patternlab.config.plugins[pluginName].initialized
170174
) {
171175
//register events
172-
registerEvents(patternlab);
176+
//registerEvents(patternlab);
177+
registerEventHandlers(patternlab);
173178

174179
//set the plugin initialized flag to true to indicate it is installed and ready
175180
patternlab.config.plugins[pluginName].initialized = true;

packages/plugin-tab/src/tab-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const fs = require('fs-extra');
1111
* @param patternlab - the global data store
1212
* @param pattern - the pattern object being iterated over
1313
*/
14-
function findTab(patternlab, pattern) {
14+
async function findTab(patternlab, pattern) {
1515
//read the filetypes from the configuration
1616
const fileTypes =
1717
patternlab.config.plugins['@pattern-lab/plugin-tab'].options.tabsToAdd;

0 commit comments

Comments
 (0)