@@ -5,110 +5,91 @@ const _ = require('lodash');
55
66const logger = require ( './log' ) ;
77
8- const { resolvePackageFolder } = require ( './resolver' ) ;
9-
8+ let findModules = require ( './findModules' ) ; //eslint-disable-line prefer-const
109let fs = require ( 'fs-extra' ) ; // eslint-disable-line
1110
12- const readModuleFile = ( uikitLocation , subPath ) => {
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 ) ;
23+
24+ if ( engineMatch ) {
25+ return engineMatch [ 1 ] ;
26+ }
27+ return false ;
28+ } ;
29+
30+ const readModuleFile = ( kit , subPath ) => {
1331 return fs . readFileSync (
14- path . resolve ( path . join ( uikitLocation , subPath ) ) ,
32+ path . resolve ( path . join ( kit . modulePath , subPath ) ) ,
1533 'utf8'
1634 ) ;
1735} ;
1836
1937/**
2038 * Loads uikits, connecting configuration and installed modules
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
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
2442 * [4] Reads files from uikit that apply to every template
2543 * @param {object } patternlab
2644 */
2745module . exports = patternlab => {
2846 const paths = patternlab . config . paths ;
2947
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]
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 ;
7560 }
7661
7762 try {
78- patternlab . uikits [ uikitConfig . name ] = {
79- name : uikitConfig . name ,
80- package : uikitConfig . package ,
81- modulePath : uikitLocation ,
63+ patternlab . uikits [ `uikit-${ kit . name } ` ] = {
64+ name : `uikit-${ kit . name } ` ,
65+ modulePath : kit . modulePath ,
8266 enabled : true ,
83- outputDir : uikitConfig . outputDir ,
84- excludedPatternStates : uikitConfig . excludedPatternStates ,
85- excludedTags : uikitConfig . excludedTags ,
67+ outputDir : configEntry . outputDir ,
68+ excludedPatternStates : configEntry . excludedPatternStates ,
69+ excludedTags : configEntry . excludedTags ,
8670 header : readModuleFile (
87- uikitLocation ,
71+ kit ,
8872 paths . source . patternlabFiles [ 'general-header' ]
8973 ) ,
9074 footer : readModuleFile (
91- uikitLocation ,
75+ kit ,
9276 paths . source . patternlabFiles [ 'general-footer' ]
9377 ) ,
9478 patternSection : readModuleFile (
95- uikitLocation ,
79+ kit ,
9680 paths . source . patternlabFiles . patternSection
9781 ) ,
9882 patternSectionSubType : readModuleFile (
99- uikitLocation ,
83+ kit ,
10084 paths . source . patternlabFiles . patternSectionSubtype
10185 ) ,
102- viewAll : readModuleFile (
103- uikitLocation ,
104- paths . source . patternlabFiles . viewall
105- ) ,
86+ viewAll : readModuleFile ( kit , paths . source . patternlabFiles . viewall ) ,
10687 } ; // [4]
10788 } catch ( ex ) {
10889 logger . error ( ex ) ;
10990 logger . error (
11091 '\nERROR: missing an essential file from ' +
111- uikitLocation +
92+ kit . modulePath +
11293 paths . source . patternlabFiles +
11394 ". Pattern Lab won't work without this file.\n"
11495 ) ;
0 commit comments