Skip to content

Commit 04a835e

Browse files
committed
Adding package property to allow a uikit to be used twice with a different name.
1 parent b2df7ee commit 04a835e

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

packages/core/src/lib/loaduikits.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,54 @@ module.exports = patternlab => {
3030
const uikitConfigs = _.filter(patternlab.config.uikits, 'enabled'); // [1]
3131
uikitConfigs.forEach(uikitConfig => {
3232
let uikitLocation = null;
33-
try {
34-
resolvePackageFolder(uikitConfig.name); // [2]
35-
} catch (ex) {
36-
// Ignore
37-
}
38-
if (!uikitLocation) {
33+
if ('package' in uikitConfig) {
3934
try {
40-
uikitLocation = resolvePackageFolder(
41-
`@pattern-lab/${uikitConfig.name}`
42-
);
35+
uikitLocation = resolvePackageFolder(uikitConfig.package);
4336
} catch (ex) {
44-
// Ignore
45-
}
46-
}
47-
if (!uikitLocation) {
48-
try {
49-
uikitLocation = resolvePackageFolder(
50-
`@pattern-lab/uikit-${uikitConfig.name}`
37+
logger.warning(
38+
`Could not find uikit with package name ${uikitConfig.package}. Did you add it to the 'dependencies' section in your 'package.json' file?`
5139
);
52-
} catch (ex) {
53-
// Ignore
40+
return;
41+
}
42+
} else {
43+
// For backwards compatibility, name to package calculation is:
44+
// 1. name -> name
45+
// 2. name -> uikit-name
46+
// 3. name -> @pattern-lab/name
47+
// 4. name -> @pattern-lab/uikit-name
48+
for (const packageName of [
49+
uikitConfig.name,
50+
`uikit-${uikitConfig.name}`,
51+
`@pattern-lab/${uikitConfig.name}`,
52+
`@pattern-lab/uikit-${uikitConfig.name}`,
53+
]) {
54+
try {
55+
uikitLocation = resolvePackageFolder(packageName); // [2]
56+
} catch (ex) {
57+
// Ignore
58+
}
59+
if (uikitLocation != null) {
60+
uikitConfig.package = packageName;
61+
logger.info(`Found uikit package ${packageName}`);
62+
break;
63+
}
5464
}
65+
if (uikitLocation == null) {
66+
logger.warning(
67+
`Could not find uikit with package name ${uikitConfig.name}, uikit-${uikitConfig.name}, @pattern-lab/${uikitConfig.name} or @pattern-lab/uikit-${uikitConfig.name} defined within patternlab-config.json in the package dependencies.`
68+
);
69+
return;
70+
} else {
71+
logger.warning(
72+
`Please update the configuration of UIKit ${uikitConfig.name} with property 'package: ${uikitConfig.package}' in patternlab-config.json. Lookup by 'name' is deprecated and will be removed in the future.`
73+
);
74+
} // [3]
5575
}
5676

57-
if (!uikitLocation) {
58-
logger.warning(
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.`
60-
);
61-
return;
62-
} // [3]
63-
6477
try {
6578
patternlab.uikits[uikitConfig.name] = {
6679
name: uikitConfig.name,
80+
package: uikitConfig.package,
6781
modulePath: uikitLocation,
6882
enabled: true,
6983
outputDir: uikitConfig.outputDir,

packages/core/test/loaduitkits_tests.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ const loaduikits = rewire('../src/lib/loaduikits');
88

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

11+
tap.test('loaduikits - does warn on missing package property', test => {
12+
//arrange
13+
const patternlab = {
14+
config: testConfig,
15+
uikits: {},
16+
};
17+
18+
patternlab.config.logLevel = 'warning';
19+
logger.log.on('warning', msg => test.ok(msg.includes('package:')));
20+
21+
//act
22+
loaduikits(patternlab).then(() => {
23+
logger.warning = () => {};
24+
test.done();
25+
});
26+
});
27+
1128
tap.test('loaduikits - maps fields correctly', function(test) {
1229
//arrange
1330
const patternlab = {
@@ -19,6 +36,10 @@ tap.test('loaduikits - maps fields correctly', function(test) {
1936
loaduikits(patternlab).then(() => {
2037
//assert
2138
test.equals(patternlab.uikits['uikit-workshop'].name, 'uikit-workshop');
39+
test.equals(
40+
patternlab.uikits['uikit-workshop'].package,
41+
'@pattern-lab/uikit-workshop'
42+
);
2243
test.contains(
2344
patternlab.uikits['uikit-workshop'].modulePath,
2445
'packages/uikit-workshop'

0 commit comments

Comments
 (0)