|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const pluginName = 'plugin-node-tab'; |
| 4 | + |
| 5 | +const fs = require('fs-extra'), |
| 6 | + glob = require('glob'), |
| 7 | + path = require('path'), |
| 8 | + EOL = require('os').EOL, |
| 9 | + tab_loader = require('./src/tab-loader'); |
| 10 | + |
| 11 | +function onPatternIterate(patternlab, pattern) { |
| 12 | + tab_loader(patternlab, pattern); |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Define what events you wish to listen to here |
| 17 | + * For a full list of events - check out https://github.com/pattern-lab/patternlab-node/wiki/Creating-Plugins#events |
| 18 | + * @param patternlab - global data store which has the handle to the event emitter |
| 19 | + */ |
| 20 | +function registerEvents(patternlab) { |
| 21 | + //register our handler at the appropriate time of execution |
| 22 | + patternlab.events.on('patternlab-pattern-write-end', onPatternIterate); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | +* A single place to define the frontend configuration |
| 27 | +* This configuration is outputted to the frontend explicitly as well as included in the plugins object. |
| 28 | +* |
| 29 | +*/ |
| 30 | +function getPluginFrontendConfig() { |
| 31 | + return { |
| 32 | + 'name':'pattern-lab\/' + pluginName, |
| 33 | + 'templates':[], |
| 34 | + 'stylesheets':[], |
| 35 | + 'javascripts':['patternlab-components\/pattern-lab\/' + pluginName + '\/js\/' + pluginName + '.js'], |
| 36 | + 'onready':'PluginTab.init()', |
| 37 | + 'callback':'' |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | +* The entry point for the plugin. You should not have to alter this code much under many circumstances. |
| 43 | +* Instead, alter getPluginFrontendConfig() and registerEvents() methods |
| 44 | + */ |
| 45 | +function pluginInit(patternlab) { |
| 46 | + |
| 47 | + if (!patternlab) { |
| 48 | + console.error('patternlab object not provided to plugin-init'); |
| 49 | + process.exit(1); |
| 50 | + } |
| 51 | + |
| 52 | + let fileTypes = require('./package.json').fileTypes; |
| 53 | + |
| 54 | + //write the plugin json to public/patternlab-components |
| 55 | + var pluginConfig = getPluginFrontendConfig(); |
| 56 | + var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 'patternlab-components', 'packages'); |
| 57 | + try { |
| 58 | + fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', JSON.stringify(pluginConfig, null, 2)); |
| 59 | + } catch (ex) { |
| 60 | + console.trace('plugin-node-tab: Error occurred while writing pluginFile configuration'); |
| 61 | + console.log(ex); |
| 62 | + } |
| 63 | + |
| 64 | + //add the plugin config to the patternlab-object |
| 65 | + if (!patternlab.plugins) { |
| 66 | + patternlab.plugins = []; |
| 67 | + } |
| 68 | + patternlab.plugins.push(pluginConfig); |
| 69 | + |
| 70 | + //write the plugin dist folder to public/pattern-lab |
| 71 | + var pluginFiles = glob.sync(__dirname + '/dist/**/*'); |
| 72 | + |
| 73 | + if (pluginFiles && pluginFiles.length > 0) { |
| 74 | + |
| 75 | + let tab_frontend_snippet = fs.readFileSync(path.resolve(__dirname + '/src/snippet.js'), 'utf8'); |
| 76 | + |
| 77 | + for (let i = 0; i < pluginFiles.length; i++) { |
| 78 | + try { |
| 79 | + var fileStat = fs.statSync(pluginFiles[i]); |
| 80 | + if (fileStat.isFile()) { |
| 81 | + var relativePath = path.relative(__dirname, pluginFiles[i]).replace('dist', ''); //dist is dropped |
| 82 | + var writePath = path.join(patternlab.config.paths.public.root, 'patternlab-components', 'pattern-lab', pluginName, relativePath); |
| 83 | + |
| 84 | + //a message to future plugin authors: |
| 85 | + //depending on your plugin's job - you might need to alter the dist file instead of copying. |
| 86 | + //if you are simply copying dist files, you can probably do the below: |
| 87 | + //fs.copySync(pluginFiles[i], writePath); |
| 88 | + |
| 89 | + //in this case, we need to alter the dist file to loop through our tabs to load as defined in the package.json |
| 90 | + //we are also being a bit lazy here, since we only expect one file |
| 91 | + let tabJSFileContents = fs.readFileSync(pluginFiles[i], 'utf8'); |
| 92 | + var snippetString = ''; |
| 93 | + for (let j = 0; j < fileTypes.length; j++) { |
| 94 | + let tabSnippetLocal = tab_frontend_snippet.replace(/<<type>>/g, fileTypes[j]).replace(/<<typeUC>>/g, fileTypes[j].toUpperCase()); |
| 95 | + snippetString += tabSnippetLocal + EOL; |
| 96 | + } |
| 97 | + tabJSFileContents = tabJSFileContents.replace('/*SNIPPETS*/', snippetString); |
| 98 | + fs.outputFileSync(writePath, tabJSFileContents); |
| 99 | + } |
| 100 | + } catch (ex) { |
| 101 | + console.trace('plugin-node-tab: Error occurred while copying pluginFile', pluginFiles[i]); |
| 102 | + console.log(ex); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + //setup listeners if not already active |
| 108 | + if (patternlab.config[pluginName] !== undefined && !patternlab.config[pluginName]) { |
| 109 | + |
| 110 | + //register events |
| 111 | + registerEvents(patternlab); |
| 112 | + |
| 113 | + //set the plugin key to true to indicate it is installed and ready |
| 114 | + patternlab.config[pluginName] = true; |
| 115 | + } |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +module.exports = pluginInit; |
0 commit comments