├── .gitignore ├── LICENSE ├── README.md ├── dist └── js │ └── plugin-node-data-inheritance.js ├── index.js ├── package.json └── postinstall.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .editorconfig 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Altinn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Inheritance Plugin for Pattern Lab Node 2 | 3 | The Data Inheritance Plugin allows patterns to inherit data from patterns within its lineage. 4 | This means that data from included patterns is merged with the current pattern. The current pattern's data takes precedence. 5 | 6 | ## Installation 7 | 8 | To add the Data Inheritance Plugin to your project using [npm](http://npmjs.com/): 9 | 10 | npm install git+https://github.com/Altinn/plugin-node-data-inheritance.git 11 | 12 | Or add it directly to your project's `package.json` file and run `npm install`. 13 | 14 | Note: This plugin needs an event which is not (yet?) available in Patternlab node, if it is to be used / tested the following needs to be added to ln. 360 in patternlab.js (Patternlab core lib): 15 | 16 | patternlab.events.emit('patternlab-pattern-before-data-merge', patternlab, pattern); 17 | -------------------------------------------------------------------------------- /dist/js/plugin-node-data-inheritance.js: -------------------------------------------------------------------------------- 1 | var PluginDataInheritance = { 2 | 3 | /** 4 | * The function defined as the onready callback within the plugin configuration. 5 | */ 6 | init: function () { 7 | 8 | //placeholder that will be replaced during configuation 9 | //most plugins could probably just implement logic here instead. 10 | /*SNIPPETS*/ 11 | 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var pluginName = 'plugin-node-data-inheritance'; 2 | var path = require('path'); 3 | var fs = require('fs-extra'); 4 | var glob = require('glob'); 5 | 6 | function getPatternByName (patternlab, patternName) { 7 | for (var i = 0; i < patternlab.patterns.length; i++) { 8 | if (patternlab.patterns[i].name === patternName) { 9 | return patternlab.patterns[i]; 10 | } 11 | } 12 | 13 | return null; 14 | } 15 | 16 | function arrayReplaceRecursive (arr) { 17 | var i = 0 18 | var p = '' 19 | var argl = arguments.length 20 | var retObj 21 | 22 | if (argl < 2) { 23 | throw new Error('There should be at least 2 arguments passed to arrayReplaceRecursive()') 24 | } 25 | 26 | if (Object.prototype.toString.call(arr) === '[object Array]') { 27 | retObj = []; 28 | for (p in arr) { 29 | retObj.push(arr[p]); 30 | } 31 | } else { 32 | retObj = {}; 33 | for (p in arr) { 34 | retObj[p] = arr[p]; 35 | } 36 | } 37 | 38 | for (i = 1; i < argl; i++) { 39 | for (p in arguments[i]) { 40 | if (retObj[p] && typeof retObj[p] === 'object') { 41 | retObj[p] = arrayReplaceRecursive(retObj[p], arguments[i][p]); 42 | } else { 43 | retObj[p] = arguments[i][p]; 44 | } 45 | } 46 | } 47 | 48 | return retObj; 49 | } 50 | 51 | function generatePatternJson (patternlab, pattern) { 52 | if (pattern.patternLineages) { 53 | for (var i = 0; i < pattern.patternLineages.length; i++) { 54 | var regex = new RegExp(/\//, 'g'); 55 | var thePart = pattern.patternLineages[i].lineagePath.replace(regex, '\\').split('\\').pop().split('.')[0]; 56 | var currentPattern = getPatternByName(patternlab, thePart); 57 | if (currentPattern) { 58 | generatePatternJson(patternlab, currentPattern); 59 | if (!pattern.jsonFileData) { 60 | pattern.jsonFileData = currentPattern.jsonFileData; 61 | } else { 62 | pattern.jsonFileData = arrayReplaceRecursive(currentPattern.jsonFileData, pattern.jsonFileData); 63 | } 64 | } 65 | } 66 | } 67 | } 68 | 69 | function registerEvents (patternlab) { 70 | patternlab.events.on('patternlab-pattern-before-data-merge', generatePatternJson); 71 | } 72 | 73 | function getPluginFrontendConfig () { 74 | return { 75 | 'name': 'pattern-lab\/' + pluginName, 'templates': [], 76 | 'stylesheets': [], 77 | 'javascripts': ['patternlab-components\/pattern-lab\/' + pluginName + 78 | '\/js\/' + pluginName + '.js'], 79 | 'onready': '', 80 | 'callback': '' 81 | } 82 | } 83 | 84 | function pluginInit (patternlab) { 85 | if (!patternlab) { 86 | console.error('patternlab object not provided to plugin-init'); 87 | process.exit(1); 88 | } 89 | 90 | var pluginConfig = getPluginFrontendConfig(); 91 | var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root, 92 | 'patternlab-components', 'packages'); 93 | 94 | try { 95 | fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json', 96 | JSON.stringify(pluginConfig, null, 2)); 97 | } catch (ex) { 98 | console.trace( 99 | 'plugin-node-tab: Error occurred while writing pluginFile configuration'); 100 | console.log(ex); 101 | } 102 | 103 | if (!patternlab.plugins) { 104 | patternlab.plugins = [] 105 | } 106 | 107 | patternlab.plugins.push(pluginConfig); 108 | var pluginFiles = glob.sync(__dirname + '/dist/**/*'); 109 | if (pluginFiles && pluginFiles.length > 0) { 110 | for (var i = 0; i < pluginFiles.length; i++) { 111 | try { 112 | var fileStat = fs.statSync(pluginFiles[i]); 113 | if (fileStat.isFile()) { 114 | var relativePath = path.relative(__dirname, pluginFiles[i]).replace('dist', ''); 115 | var writePath = path.join(patternlab.config.paths.public.root, 116 | 'patternlab-components', 'pattern-lab', pluginName, relativePath); 117 | var tabJSFileContents = fs.readFileSync(pluginFiles[i], 'utf8'); 118 | fs.outputFileSync(writePath, tabJSFileContents); 119 | } 120 | } catch (ex) { 121 | console.trace( 122 | 'plugin-node-tab: Error occurred while copying pluginFile', 123 | pluginFiles[i]); 124 | console.log(ex); 125 | } 126 | } 127 | } 128 | 129 | registerEvents(patternlab); patternlab.config[pluginName] = true; 130 | } 131 | 132 | module.exports = pluginInit 133 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "plugin-node-data-inheritance", 3 | "version": "1.0.0-alpha", 4 | "description": "Custom data inheritance plugin for Patternlab", 5 | "main": "index.js", 6 | "dependencies": { 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git+https://github.com/Altinn/plugin-node-data-inheritance.git" 11 | }, 12 | "scripts": { "postinstall": "node ./postinstall.js" }, 13 | "author": "David Emanuelsen", 14 | "license": "MIT", 15 | "homepage": "https://github.com/Altinn/plugin-node-data-inheritance#readme" 16 | } 17 | -------------------------------------------------------------------------------- /postinstall.js: -------------------------------------------------------------------------------- 1 | console.log('plugin-node-data-inheritance installed successfully!') 2 | --------------------------------------------------------------------------------