Skip to content

Commit b2df7ee

Browse files
committed
Rewrote loading the UI kits starting from the config
1 parent 78a9cb1 commit b2df7ee

File tree

3 files changed

+76
-164
lines changed

3 files changed

+76
-164
lines changed

packages/core/src/lib/loaduikits.js

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,96 @@ const _ = require('lodash');
55

66
const logger = require('./log');
77

8-
let findModules = require('./findModules'); //eslint-disable-line prefer-const
9-
let fs = require('fs-extra'); // eslint-disable-line
10-
11-
const uiKitMatcher = /^uikit-(.*)$/;
12-
const nodeModulesPath = path.join(process.cwd(), 'node_modules');
8+
const { resolvePackageFolder } = require('./resolver');
139

14-
/**
15-
* Given a path: return the uikit name if the path points to a valid uikit
16-
* module directory, or false if it doesn't.
17-
* @param filePath
18-
* @returns UIKit name if exists or FALSE
19-
*/
20-
const isUIKitModule = filePath => {
21-
const baseName = path.basename(filePath);
22-
const engineMatch = baseName.match(uiKitMatcher);
23-
24-
if (engineMatch) {
25-
return engineMatch[1];
26-
}
27-
return false;
28-
};
10+
let fs = require('fs-extra'); // eslint-disable-line
2911

30-
const readModuleFile = (kit, subPath) => {
12+
const readModuleFile = (uikitLocation, subPath) => {
3113
return fs.readFileSync(
32-
path.resolve(path.join(kit.modulePath, subPath)),
14+
path.resolve(path.join(uikitLocation, subPath)),
3315
'utf8'
3416
);
3517
};
3618

3719
/**
3820
* Loads uikits, connecting configuration and installed modules
39-
* [1] Looks in node_modules for uikits.
40-
* [2] Filter out our uikit-polyfills package.
41-
* [3] Only continue if uikit is enabled in patternlab-config.json
21+
* [1] Lists the enabled uikits from patternlab-config.json
22+
* [2] Try to resolve the location of the uikit in the package dependencies
23+
* [3] Warn when the uikit couldn't be loaded
4224
* [4] Reads files from uikit that apply to every template
4325
* @param {object} patternlab
4426
*/
4527
module.exports = patternlab => {
4628
const paths = patternlab.config.paths;
4729

48-
const uikits = findModules(nodeModulesPath, isUIKitModule) // [1]
49-
.filter(kit => kit.name !== 'polyfills'); // [2]
50-
uikits.forEach(kit => {
51-
const configEntry = _.find(_.filter(patternlab.config.uikits, 'enabled'), {
52-
name: `uikit-${kit.name}`,
53-
}); // [3]
30+
const uikitConfigs = _.filter(patternlab.config.uikits, 'enabled'); // [1]
31+
uikitConfigs.forEach(uikitConfig => {
32+
let uikitLocation = null;
33+
try {
34+
resolvePackageFolder(uikitConfig.name); // [2]
35+
} catch (ex) {
36+
// Ignore
37+
}
38+
if (!uikitLocation) {
39+
try {
40+
uikitLocation = resolvePackageFolder(
41+
`@pattern-lab/${uikitConfig.name}`
42+
);
43+
} catch (ex) {
44+
// Ignore
45+
}
46+
}
47+
if (!uikitLocation) {
48+
try {
49+
uikitLocation = resolvePackageFolder(
50+
`@pattern-lab/uikit-${uikitConfig.name}`
51+
);
52+
} catch (ex) {
53+
// Ignore
54+
}
55+
}
5456

55-
if (!configEntry) {
57+
if (!uikitLocation) {
5658
logger.warning(
57-
`Could not find uikit with name uikit-${kit.name} defined within patternlab-config.json, or it is not enabled.`
59+
`Could not find uikit with package name ${uikitConfig.name}, @pattern-lab/${uikitConfig.name} or @pattern-lab/uikit-${uikitConfig.name} defined within patternlab-config.json in the package dependencies.`
5860
);
5961
return;
60-
}
62+
} // [3]
6163

6264
try {
63-
patternlab.uikits[`uikit-${kit.name}`] = {
64-
name: `uikit-${kit.name}`,
65-
modulePath: kit.modulePath,
65+
patternlab.uikits[uikitConfig.name] = {
66+
name: uikitConfig.name,
67+
modulePath: uikitLocation,
6668
enabled: true,
67-
outputDir: configEntry.outputDir,
68-
excludedPatternStates: configEntry.excludedPatternStates,
69-
excludedTags: configEntry.excludedTags,
69+
outputDir: uikitConfig.outputDir,
70+
excludedPatternStates: uikitConfig.excludedPatternStates,
71+
excludedTags: uikitConfig.excludedTags,
7072
header: readModuleFile(
71-
kit,
73+
uikitLocation,
7274
paths.source.patternlabFiles['general-header']
7375
),
7476
footer: readModuleFile(
75-
kit,
77+
uikitLocation,
7678
paths.source.patternlabFiles['general-footer']
7779
),
7880
patternSection: readModuleFile(
79-
kit,
81+
uikitLocation,
8082
paths.source.patternlabFiles.patternSection
8183
),
8284
patternSectionSubType: readModuleFile(
83-
kit,
85+
uikitLocation,
8486
paths.source.patternlabFiles.patternSectionSubtype
8587
),
86-
viewAll: readModuleFile(kit, paths.source.patternlabFiles.viewall),
88+
viewAll: readModuleFile(
89+
uikitLocation,
90+
paths.source.patternlabFiles.viewall
91+
),
8792
}; // [4]
8893
} catch (ex) {
8994
logger.error(ex);
9095
logger.error(
9196
'\nERROR: missing an essential file from ' +
92-
kit.modulePath +
97+
uikitLocation +
9398
paths.source.patternlabFiles +
9499
". Pattern Lab won't work without this file.\n"
95100
);

packages/core/test/loaduitkits_tests.js

Lines changed: 12 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -8,102 +8,27 @@ const loaduikits = rewire('../src/lib/loaduikits');
88

99
const testConfig = require('./util/patternlab-config.json');
1010

11-
const findModulesMock = function() {
12-
return [
13-
{
14-
name: 'foo',
15-
modulePath: 'node_modules/@pattern-lab/uikit-foo',
16-
},
17-
{
18-
name: 'bar',
19-
modulePath: 'node_modules/@pattern-lab/uikit-bar',
20-
},
21-
{
22-
name: 'baz',
23-
modulePath: 'node_modules/@pattern-lab/uikit-baz',
24-
},
25-
{
26-
name: 'polyfills',
27-
modulePath: 'node_modules/@pattern-lab/uikit-polyfills',
28-
},
29-
];
30-
};
31-
32-
const fsMock = {
33-
readFileSync: function(path, encoding) {
34-
return 'file';
35-
},
36-
};
37-
38-
loaduikits.__set__({
39-
findModules: findModulesMock,
40-
fs: fsMock,
41-
});
42-
43-
logger;
44-
45-
tap.test('loaduitkits - does not warn on uikit-polyfills', test => {
46-
//arrange
47-
const patternlab = {
48-
config: testConfig,
49-
uikits: {},
50-
};
51-
52-
patternlab.config.logLevel = 'warning';
53-
logger.log.on('warning', msg => test.notOk(msg.includes('uikit-polyfills')));
54-
55-
const uikitFoo = {
56-
name: 'uikit-foo',
57-
enabled: true,
58-
outputDir: 'foo',
59-
excludedPatternStates: ['legacy'],
60-
excludedTags: ['baz'],
61-
};
62-
63-
patternlab.config.uikits = [uikitFoo];
64-
65-
//act
66-
loaduikits(patternlab).then(() => {
67-
logger.warning = () => {};
68-
test.done();
69-
});
70-
});
71-
7211
tap.test('loaduikits - maps fields correctly', function(test) {
7312
//arrange
7413
const patternlab = {
7514
config: testConfig,
7615
uikits: {},
7716
};
7817

79-
const uikitFoo = {
80-
name: 'uikit-foo',
81-
enabled: true,
82-
outputDir: 'foo',
83-
excludedPatternStates: ['legacy'],
84-
excludedTags: ['baz'],
85-
};
86-
87-
patternlab.config.uikits = [uikitFoo];
88-
8918
//act
9019
loaduikits(patternlab).then(() => {
9120
//assert
92-
test.equals(patternlab.uikits['uikit-foo'].name, uikitFoo.name);
93-
test.equals(
94-
patternlab.uikits['uikit-foo'].modulePath,
95-
'node_modules/@pattern-lab/uikit-foo'
96-
);
97-
test.ok(patternlab.uikits['uikit-foo'].enabled);
98-
test.equals(patternlab.uikits['uikit-foo'].outputDir, uikitFoo.outputDir);
99-
test.equals(
100-
patternlab.uikits['uikit-foo'].excludedPatternStates,
101-
uikitFoo.excludedPatternStates
102-
);
103-
test.equals(
104-
patternlab.uikits['uikit-foo'].excludedTags,
105-
uikitFoo.excludedTags
21+
test.equals(patternlab.uikits['uikit-workshop'].name, 'uikit-workshop');
22+
test.contains(
23+
patternlab.uikits['uikit-workshop'].modulePath,
24+
'packages/uikit-workshop'
10625
);
26+
test.ok(patternlab.uikits['uikit-workshop'].enabled);
27+
test.equals(patternlab.uikits['uikit-workshop'].outputDir, 'test/');
28+
test.deepEquals(patternlab.uikits['uikit-workshop'].excludedPatternStates, [
29+
'legacy',
30+
]);
31+
test.deepEquals(patternlab.uikits['uikit-workshop'].excludedTags, ['baz']);
10732
test.end();
10833
});
10934
});
@@ -115,36 +40,11 @@ tap.test('loaduikits - only adds files for enabled uikits', function(test) {
11540
uikits: {},
11641
};
11742

118-
patternlab.config.uikits = [
119-
{
120-
name: 'uikit-foo',
121-
enabled: true,
122-
outputDir: 'foo',
123-
excludedPatternStates: ['legacy'],
124-
excludedTags: ['baz'],
125-
},
126-
{
127-
name: 'uikit-bar',
128-
enabled: true,
129-
outputDir: 'bar',
130-
excludedPatternStates: ['development'],
131-
excludedTags: ['baz', 'foo'],
132-
},
133-
{
134-
name: 'uikit-baz',
135-
enabled: false,
136-
outputDir: 'baz',
137-
excludedPatternStates: [''],
138-
excludedTags: [],
139-
},
140-
];
141-
14243
//act
14344
loaduikits(patternlab).then(() => {
14445
//assert
145-
test.ok(patternlab.uikits['uikit-foo']);
146-
test.ok(patternlab.uikits['uikit-bar']);
147-
test.notOk(patternlab.uikits['uikit-baz']);
46+
test.ok(patternlab.uikits['uikit-workshop']);
47+
test.notOk(patternlab.uikits['uikit-polyfills']);
14848
test.end();
14949
});
15050
});

packages/core/test/util/patternlab-config.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"meta": "./test/files/_meta/",
88
"styleguide": "./test/files/styleguide/",
99
"patternlabFiles": {
10-
"general-header": "./test/files/partials/general-header.mustache",
11-
"general-footer": "./test/files/partials/general-footer.mustache",
12-
"patternSection": "./test/files/partials/patternSection.mustache",
10+
"general-header": "views/partials/general-header.mustache",
11+
"general-footer": "views/partials/general-footer.mustache",
12+
"patternSection": "views/partials/patternSection.mustache",
1313
"patternSectionSubtype":
14-
"./test/files/partials/patternSectionSubtype.mustache",
15-
"viewall": "./test/files/viewall.mustache"
14+
"views/partials/patternSectionSubtype.mustache",
15+
"viewall": "views/viewall.mustache"
1616
},
1717
"js": "./test/files/js",
1818
"images": "./test/files/images",
@@ -76,8 +76,15 @@
7676
"uikits": [
7777
{
7878
"name": "uikit-workshop",
79-
"outputDir": "packages/core/test/",
79+
"outputDir": "test/",
8080
"enabled": true,
81+
"excludedPatternStates": ["legacy"],
82+
"excludedTags": ["baz"]
83+
},
84+
{
85+
"name": "uikit-polyfills",
86+
"outputDir": "test/",
87+
"enabled": false,
8188
"excludedPatternStates": [],
8289
"excludedTags": []
8390
}

0 commit comments

Comments
 (0)