|
1 |
| -"use strict"; |
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const pluginName = 'plugin-node-tab'; |
2 | 4 |
|
3 | 5 | var fs = require('fs-extra'),
|
4 | 6 | glob = require('glob'),
|
5 |
| - path = require('path'); |
| 7 | + path = require('path'), |
| 8 | + tab_loader = require('./src/tab-loader'); |
| 9 | + |
| 10 | +function onPatternIterate(patternlab, pattern) { |
| 11 | + tab_loader(patternlab, pattern); |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Define what events you wish to listen to here |
| 16 | + * @param patternlab - global data store which has the handle to the event emitter |
| 17 | + */ |
| 18 | +function registerEvents(patternlab){ |
| 19 | + |
| 20 | + //TODO: list all possible events |
| 21 | + patternlab.events.on('patternlab-pattern-write-end', onPatternIterate); |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | +* A single place to define the frontend configuration |
| 26 | + */ |
| 27 | +function getPluginFrontendConfig() { |
| 28 | + return { |
| 29 | + 'name':'pattern-lab\/' + pluginName, |
| 30 | + 'templates':[], |
| 31 | + 'stylesheets':[], |
| 32 | + 'javascripts':['patternlab-components\/pattern-lab\/' + pluginName + '\/js\/' + pluginName + '.js'], |
| 33 | + 'onready':'PluginTab.init()', |
| 34 | + 'callback':'' |
| 35 | + } |
| 36 | +} |
6 | 37 |
|
7 |
| -function plugin_init(patternlab) { |
| 38 | +/** |
| 39 | +* The entry point for the plugin. You should not have to alter this code. |
| 40 | +* Instead, alter getPluginFrontendConfig() and registerEvents() methods |
| 41 | + */ |
| 42 | +function pluginInit(patternlab) { |
8 | 43 |
|
9 | 44 | if(!patternlab) {
|
10 | 45 | console.error('patternlab object not provided to plugin-init');
|
11 | 46 | process.exit(1);
|
12 | 47 | }
|
13 | 48 |
|
14 | 49 | //write the plugin json to public/patternlab-components
|
15 |
| - var pluginConfig = { |
16 |
| - "name":"pattern-lab\/plugin-node-tab", |
17 |
| - "templates":[], |
18 |
| - "stylesheets":[], |
19 |
| - "javascripts":["\/js\/plugin-tab.js"], |
20 |
| - "onready":"PluginTab.init()", |
21 |
| - "callback":"" |
22 |
| - } |
23 |
| - |
| 50 | + var pluginConfig = getPluginFrontendConfig(); |
24 | 51 | var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages');
|
25 | 52 | try {
|
26 |
| - fs.outputFileSync(pluginConfigPathName + '/plugin-tab.json', JSON.stringify(pluginConfig, null, 2)); |
| 53 | + fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', JSON.stringify(pluginConfig, null, 2)); |
27 | 54 | } catch (ex) {
|
28 |
| - console.trace('Error occurred while writing pluginFile configuration'); |
| 55 | + console.trace('plugin-node-tab: Error occurred while writing pluginFile configuration'); |
29 | 56 | console.log(ex);
|
30 | 57 | }
|
31 | 58 |
|
| 59 | + //add the plugin config to the patternlab-object |
| 60 | + if (!patternlab.plugins) { |
| 61 | + patternlab.plugins = []; |
| 62 | + } |
| 63 | + patternlab.plugins.push(pluginConfig); |
| 64 | + |
32 | 65 | //write the plugin dist folder to public/pattern-lab
|
33 |
| - var pluginFiles = glob.sync(__dirname +'/dist/**/*'); |
| 66 | + var pluginFiles = glob.sync(__dirname + '/dist/**/*'); |
34 | 67 |
|
35 | 68 | if (pluginFiles && pluginFiles.length > 0) {
|
36 | 69 | for (let i = 0; i < pluginFiles.length; i++) {
|
37 | 70 | try {
|
38 | 71 | var fileStat = fs.statSync(pluginFiles[i]);
|
39 | 72 | if (fileStat.isFile()) {
|
40 | 73 | var relativePath = path.relative(__dirname, pluginFiles[i]).replace('dist', ''); //dist is dropped
|
41 |
| - var writePath = path.join(patternlab.config.paths.public.root,'patternlab-components', 'pattern-lab', 'plugin-node-tab', relativePath); |
| 74 | + var writePath = path.join(patternlab.config.paths.public.root,'patternlab-components', 'pattern-lab', pluginName, relativePath); |
42 | 75 | fs.copySync(pluginFiles[i], writePath);
|
43 | 76 | }
|
44 | 77 | } catch (ex) {
|
45 |
| - console.trace('Error occurred while copying pluginFile', pluginFiles[i]); |
| 78 | + console.trace('plugin-node-tab: Error occurred while copying pluginFile', pluginFiles[i]); |
46 | 79 | console.log(ex);
|
47 | 80 | }
|
48 | 81 | }
|
49 | 82 | }
|
50 | 83 |
|
| 84 | + //setup listeners if not already active |
| 85 | + if (patternlab.config[pluginName] !== undefined && !patternlab.config[pluginName]) { |
| 86 | + |
| 87 | + //register events |
| 88 | + registerEvents(patternlab); |
| 89 | + |
| 90 | + //set the plugin key to true to indicate it is installed and ready |
| 91 | + patternlab.config[pluginName] = true; |
| 92 | + } |
| 93 | + |
51 | 94 | }
|
52 | 95 |
|
53 |
| -module.exports = plugin_init; |
| 96 | +module.exports = pluginInit; |
0 commit comments