@@ -5,91 +5,110 @@ 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' ) ;
13-
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 ) ;
8+ const { resolvePackageFolder } = require ( './resolver' ) ;
239
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]
54-
55- if ( ! configEntry ) {
56- logger . warning (
57- `Could not find uikit with name uikit-${ kit . name } defined within patternlab-config.json, or it is not enabled.`
58- ) ;
59- return ;
30+ const uikitConfigs = _ . filter ( patternlab . config . uikits , 'enabled' ) ; // [1]
31+ uikitConfigs . forEach ( uikitConfig => {
32+ let uikitLocation = null ;
33+ if ( 'package' in uikitConfig ) {
34+ try {
35+ uikitLocation = resolvePackageFolder ( uikitConfig . package ) ;
36+ } catch ( ex ) {
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?`
39+ ) ;
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+ }
64+ }
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]
6075 }
6176
6277 try {
63- patternlab . uikits [ `uikit-${ kit . name } ` ] = {
64- name : `uikit-${ kit . name } ` ,
65- modulePath : kit . modulePath ,
78+ patternlab . uikits [ uikitConfig . name ] = {
79+ name : uikitConfig . name ,
80+ package : uikitConfig . package ,
81+ modulePath : uikitLocation ,
6682 enabled : true ,
67- outputDir : configEntry . outputDir ,
68- excludedPatternStates : configEntry . excludedPatternStates ,
69- excludedTags : configEntry . excludedTags ,
83+ outputDir : uikitConfig . outputDir ,
84+ excludedPatternStates : uikitConfig . excludedPatternStates ,
85+ excludedTags : uikitConfig . excludedTags ,
7086 header : readModuleFile (
71- kit ,
87+ uikitLocation ,
7288 paths . source . patternlabFiles [ 'general-header' ]
7389 ) ,
7490 footer : readModuleFile (
75- kit ,
91+ uikitLocation ,
7692 paths . source . patternlabFiles [ 'general-footer' ]
7793 ) ,
7894 patternSection : readModuleFile (
79- kit ,
95+ uikitLocation ,
8096 paths . source . patternlabFiles . patternSection
8197 ) ,
8298 patternSectionSubType : readModuleFile (
83- kit ,
99+ uikitLocation ,
84100 paths . source . patternlabFiles . patternSectionSubtype
85101 ) ,
86- viewAll : readModuleFile ( kit , paths . source . patternlabFiles . viewall ) ,
102+ viewAll : readModuleFile (
103+ uikitLocation ,
104+ paths . source . patternlabFiles . viewall
105+ ) ,
87106 } ; // [4]
88107 } catch ( ex ) {
89108 logger . error ( ex ) ;
90109 logger . error (
91110 '\nERROR: missing an essential file from ' +
92- kit . modulePath +
111+ uikitLocation +
93112 paths . source . patternlabFiles +
94113 ". Pattern Lab won't work without this file.\n"
95114 ) ;
0 commit comments