-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
132 lines (115 loc) · 3.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var pluginName = 'plugin-node-data-inheritance';
var path = require('path');
var fs = require('fs-extra');
var glob = require('glob');
function getPatternByName (patternlab, patternName) {
for (var i = 0; i < patternlab.patterns.length; i++) {
if (patternlab.patterns[i].name === patternName) {
return patternlab.patterns[i];
}
}
return null;
}
function arrayReplaceRecursive (arr) {
var i = 0
var p = ''
var argl = arguments.length
var retObj
if (argl < 2) {
throw new Error('There should be at least 2 arguments passed to arrayReplaceRecursive()')
}
if (Object.prototype.toString.call(arr) === '[object Array]') {
retObj = [];
for (p in arr) {
retObj.push(arr[p]);
}
} else {
retObj = {};
for (p in arr) {
retObj[p] = arr[p];
}
}
for (i = 1; i < argl; i++) {
for (p in arguments[i]) {
if (retObj[p] && typeof retObj[p] === 'object') {
retObj[p] = arrayReplaceRecursive(retObj[p], arguments[i][p]);
} else {
retObj[p] = arguments[i][p];
}
}
}
return retObj;
}
function generatePatternJson (patternlab, pattern) {
if (pattern.patternLineages) {
for (var i = 0; i < pattern.patternLineages.length; i++) {
var regex = new RegExp(/\//, 'g');
var thePart = pattern.patternLineages[i].lineagePath.replace(regex, '\\').split('\\').pop().split('.')[0];
var currentPattern = getPatternByName(patternlab, thePart);
if (currentPattern) {
generatePatternJson(patternlab, currentPattern);
if (!pattern.jsonFileData) {
pattern.jsonFileData = currentPattern.jsonFileData;
} else {
pattern.jsonFileData = arrayReplaceRecursive(currentPattern.jsonFileData, pattern.jsonFileData);
}
}
}
}
}
function registerEvents (patternlab) {
patternlab.events.on('patternlab-pattern-before-data-merge', generatePatternJson);
}
function getPluginFrontendConfig () {
return {
'name': 'pattern-lab\/' + pluginName, 'templates': [],
'stylesheets': [],
'javascripts': ['patternlab-components\/pattern-lab\/' + pluginName +
'\/js\/' + pluginName + '.js'],
'onready': '',
'callback': ''
}
}
function pluginInit (patternlab) {
if (!patternlab) {
console.error('patternlab object not provided to plugin-init');
process.exit(1);
}
var pluginConfig = getPluginFrontendConfig();
var pluginConfigPathName = path.resolve(patternlab.config.paths.public.root,
'patternlab-components', 'packages');
try {
fs.outputFileSync(pluginConfigPathName + '/' + pluginName + '.json',
JSON.stringify(pluginConfig, null, 2));
} catch (ex) {
console.trace(
'plugin-node-tab: Error occurred while writing pluginFile configuration');
console.log(ex);
}
if (!patternlab.plugins) {
patternlab.plugins = []
}
patternlab.plugins.push(pluginConfig);
var pluginFiles = glob.sync(__dirname + '/dist/**/*');
if (pluginFiles && pluginFiles.length > 0) {
for (var i = 0; i < pluginFiles.length; i++) {
try {
var fileStat = fs.statSync(pluginFiles[i]);
if (fileStat.isFile()) {
var relativePath = path.relative(__dirname, pluginFiles[i]).replace('dist', '');
var writePath = path.join(patternlab.config.paths.public.root,
'patternlab-components', 'pattern-lab', pluginName, relativePath);
var tabJSFileContents = fs.readFileSync(pluginFiles[i], 'utf8');
fs.outputFileSync(writePath, tabJSFileContents);
}
} catch (ex) {
console.trace(
'plugin-node-tab: Error occurred while copying pluginFile',
pluginFiles[i]);
console.log(ex);
}
}
}
registerEvents(patternlab); patternlab.config[pluginName] = true;
}
module.exports = pluginInit