@@ -5,91 +5,96 @@ const _ = require('lodash');
55
66const 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 = / ^ u i k i t - ( .* ) $ / ;
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 */
4527module . 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 ) ;
0 commit comments