forked from pattern-lab/patternlab-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddPattern.js
More file actions
47 lines (39 loc) · 1.39 KB
/
addPattern.js
File metadata and controls
47 lines (39 loc) · 1.39 KB
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
'use strict';
const _ = require('lodash');
const logger = require('./log');
module.exports = function(pattern, patternlab) {
//add the link to the global object
if (!patternlab.data.link) {
patternlab.data.link = {};
}
patternlab.data.link[pattern.patternPartial] =
'/patterns/' + pattern.patternLink;
//only push to array if the array doesn't contain this pattern
let isNew = true;
for (let i = 0; i < patternlab.patterns.length; i++) {
//so we need the identifier to be unique, which patterns[i].relPath is
if (pattern.relPath === patternlab.patterns[i].relPath) {
//if relPath already exists, overwrite that element
patternlab.patterns[i] = pattern;
patternlab.partials[pattern.patternPartial] =
pattern.extendedTemplate || pattern.template;
isNew = false;
break;
}
}
// if the pattern is new, we must register it with various data structures!
if (isNew) {
logger.debug(`found new pattern ${pattern.patternPartial}`);
// do global registration
if (pattern.isPattern) {
patternlab.partials[pattern.patternPartial] =
pattern.extendedTemplate || pattern.template;
// do plugin-specific registration
pattern.registerPartial();
} else {
patternlab.partials[pattern.patternPartial] = pattern.patternDesc;
}
patternlab.patterns.push(pattern);
patternlab.graph.add(pattern);
}
};