Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

pattern-lab / patternlab-node / 1326

27 Apr 2018 - 20:04 coverage increased (+5.8%) to 69.394%
1326

Pull #840

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
feat(uikits): filter out excluded pattern states from uikit output
Pull Request #840: Feature/uikit refactor

419 of 670 branches covered (62.54%)

Branch coverage included in aggregate %.

185 of 346 new or added lines in 20 files covered. (53.47%)

40 existing lines in 9 files now uncovered.

1642 of 2300 relevant lines covered (71.39%)

71.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.03
/packages/engine-liquid/lib/engine_liquid.js
1
/*
2
 * Liquid pattern engine for patternlab-node - v2.X.X - 2017
3
 *
4
 * Cameron Roe
5
 * Licensed under the MIT license.
6
 *
7
 *
8
 */
9

10
'use strict';
11

12
const fs = require('fs-extra');
16×
13
const path = require('path');
16×
14
const isDirectory = source => fs.lstatSync(source).isDirectory();
16×
15
const getDirectories = source =>
16×
16
  fs
16×
17
    .readdirSync(source)
UNCOV
18
    .map(name => path.join(source, name))
!
19
    .filter(isDirectory);
20

21
const { lstatSync, readdirSync } = require('fs');
16×
22
const { join } = require('path');
16×
23

24
var utils = require('./util_liquid');
16×
25
var Liquid = require('liquidjs');
16×
26

27
let engine = Liquid({
16×
28
  dynamicPartials: false,
29
});
30

31
// This holds the config from from core. The core has to call
32
// usePatternLabConfig() at load time for this to be populated.
33
let patternLabConfig = {};
16×
34

35
module.exports = {
16×
36
  engine: engine,
37
  engineName: 'liquid',
38
  engineFileExtension: ['.liquid', '.html'],
39
  isAsync: true,
40

41
  // // partial expansion is only necessary for Mustache templates that have
42
  // // style modifiers or pattern parameters (I think)
43
  // expandPartials: true,
44

45
  // regexes, stored here so they're only compiled once
46
  findPartialsRE: utils.partialsRE,
47
  findPartialsWithStyleModifiersRE: utils.partialsWithStyleModifiersRE,
48
  findPartialsWithPatternParametersRE: utils.partialsWithPatternParametersRE,
49
  findListItemsRE: utils.listItemsRE,
50
  findPartialRE: utils.partialRE,
51

52
  // render it
53
  renderPattern: function renderPattern(pattern, data, partials) {
54
    return engine
!
55
      .parseAndRender(pattern.template, data)
56
      .then(function(html) {
57
        return html;
!
58
      })
59
      .catch(function(ex) {
60
        console.log(40, ex);
!
61
      });
62
  },
63

64
  /**
65
   * Find regex matches within both pattern strings and pattern objects.
66
   *
67
   * @param {string|object} pattern Either a string or a pattern object.
68
   * @param {object} regex A JavaScript RegExp object.
69
   * @returns {array|null} An array if a match is found, null if not.
70
   */
71
  patternMatcher: function patternMatcher(pattern, regex) {
72
    var matches;
73
    if (typeof pattern === 'string') {
Branches [[0, 0], [0, 1]] missed. !
74
      matches = pattern.match(regex);
!
75
    } else if (
Branches [[1, 0], [1, 1]] missed. !
76
      typeof pattern === 'object' &&
Branches [[2, 0], [2, 1]] missed.
77
      typeof pattern.template === 'string'
78
    ) {
79
      matches = pattern.template.match(regex);
!
80
    }
81
    return matches;
!
82
  },
83

84
  // find and return any {{> template-name }} within pattern
85
  findPartials: function findPartials(pattern) {
86
    var matches = this.patternMatcher(pattern, this.findPartialsRE);
!
87
    return matches;
!
88
  },
89
  findPartialsWithStyleModifiers: function(pattern) {
90
    var matches = this.patternMatcher(
!
91
      pattern,
92
      this.findPartialsWithStyleModifiersRE
93
    );
94
    return matches;
!
95
  },
96

97
  // returns any patterns that match {{> value(foo:"bar") }} or {{>
98
  // value:mod(foo:"bar") }} within the pattern
99
  findPartialsWithPatternParameters: function(pattern) {
100
    var matches = this.patternMatcher(
!
101
      pattern,
102
      this.findPartialsWithPatternParametersRE
103
    );
104
    return matches;
!
105
  },
106
  findListItems: function(pattern) {
107
    var matches = this.patternMatcher(pattern, this.findListItemsRE);
!
108
    return matches;
!
109
  },
110

111
  // given a pattern, and a partial string, tease out the "pattern key" and
112
  // return it.
113
  findPartial_new: function(partialString) {
114
    var partial = partialString.replace(this.findPartialRE, '$1');
!
115
    return partial;
!
116
  },
117

118
  // GTP: the old implementation works better. We might not need
119
  // this.findPartialRE anymore if it works in all cases!
120
  findPartial: function(partialString) {
121
    //strip out the template cruft
122
    var foundPatternPartial = partialString
!
123
      .replace('{{> ', '')
124
      .replace(' }}', '')
125
      .replace('{{>', '')
126
      .replace('}}', '');
127

128
    // remove any potential pattern parameters. this and the above are rather brutish but I didn't want to do a regex at the time
129
    if (foundPatternPartial.indexOf('(') > 0) {
Branches [[3, 0], [3, 1]] missed. !
130
      foundPatternPartial = foundPatternPartial.substring(
!
131
        0,
132
        foundPatternPartial.indexOf('(')
133
      );
134
    }
135

136
    //remove any potential stylemodifiers.
137
    foundPatternPartial = foundPatternPartial.split(':')[0];
!
138

139
    return foundPatternPartial;
!
140
  },
141

142
  /**
143
   * Accept a Pattern Lab config object from the core and put it in
144
   * this module's closure scope so we can configure engine behavior.
145
   *
146
   * @param {object} config - the global config object from core
147
   */
148
  usePatternLabConfig: function(config) {
149
    patternLabConfig = config;
16×
150
    let patternsPath = patternLabConfig.paths.source.patterns;
16×
151

152
    if (patternsPath.slice(-1) === '/') {
Branches [[4, 1]] missed. 16×
153
      patternsPath = patternsPath.slice(0, -1);
16×
154
    }
155

156
    const allPaths = getDirectories(patternsPath).reduce((allDirs, dir) => {
16×
UNCOV
157
      return allDirs.concat(getDirectories(dir));
!
158
    }, []);
159

UNCOV
160
    engine = Liquid({
!
161
      dynamicPartials: false,
162
      root: allPaths,
163
    });
164
  },
165

166
  spawnFile: function(config, fileName) {
UNCOV
167
    const paths = config.paths;
!
UNCOV
168
    const metaFilePath = path.resolve(paths.source.meta, fileName);
!
169

UNCOV
170
    try {
!
UNCOV
171
      fs.statSync(metaFilePath);
!
172
    } catch (err) {
173
      //not a file, so spawn it from the included file
UNCOV
174
      const localMetaFilePath = path.resolve(__dirname, '_meta/', fileName);
!
UNCOV
175
      const metaFileContent = fs.readFileSync(
!
176
        path.resolve(__dirname, '..', '_meta/', fileName),
177
        'utf8'
178
      );
UNCOV
179
      fs.outputFileSync(metaFilePath, metaFileContent);
!
180
    }
181
  },
182

183
  /**
184
   * Checks to see if the _meta directory has engine-specific head and foot files,
185
   * spawning them if not found.
186
   *
187
   * @param {object} config - the global config object from core, since we won't
188
   * assume it's already present
189
   */
190
  spawnMeta: function(config) {
UNCOV
191
    this.spawnFile(config, '_00-head.liquid');
!
UNCOV
192
    this.spawnFile(config, '_01-foot.liquid');
!
193
  },
194
};
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2021 Coveralls, Inc