-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathexpandPartials.js
78 lines (66 loc) · 2.77 KB
/
expandPartials.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
'use strict';
const logger = require('./log');
const ph = require('./parameter_hunter');
const jsonCopy = require('./json_copy');
const getPartial = require('./get');
const parameter_hunter = new ph();
module.exports = function (currentPattern, patternlab) {
const processRecursive = require('./processRecursive');
//find how many partials there may be for the given pattern
const foundPatternPartials = currentPattern.findPartials();
// expand any partials present in this pattern; that is, drill down into
// the template and replace their calls in this template with rendered
// results
if (
currentPattern.engine.expandPartials &&
foundPatternPartials !== null &&
foundPatternPartials.length > 0
) {
logger.debug(`found partials for ${currentPattern.patternPartial}`);
// determine if the template contains any pattern parameters. if so they
// must be immediately consumed
return parameter_hunter
.find_parameters(currentPattern, patternlab)
.then(() => {
//do something with the regular old partials
foundPatternPartials.forEach((foundPartial) => {
const partial = currentPattern.findPartial(foundPartial);
const partialPattern = getPartial(partial, patternlab);
//recurse through nested partials to fill out this extended template.
return processRecursive(partialPattern.relPath, patternlab)
.then(() => {
//eslint-disable-line no-loop-func
//complete assembly of extended template
//create a copy of the partial so as to not pollute it after the getPartial call.
const cleanPartialPattern = jsonCopy(
partialPattern,
`partial pattern ${partial}`
);
//this is what we came here for
logger.debug(
`within ${currentPattern.patternPartial}, replacing extendedTemplate partial ${foundPartial} with ${cleanPartialPattern.patternPartial}'s extendedTemplate`
);
currentPattern.extendedTemplate =
currentPattern.extendedTemplate.replace(
foundPartial,
cleanPartialPattern.extendedTemplate
);
// update the extendedTemplate in the partials object in case this
// pattern is consumed later
patternlab.partials[currentPattern.patternPartial] =
currentPattern.extendedTemplate;
return Promise.resolve();
})
.catch((reason) => {
console.log(reason);
logger.error(reason);
});
});
})
.catch((reason) => {
console.log(reason);
logger.error(reason);
});
}
return Promise.resolve();
};