@@ -5,91 +5,96 @@ const _ = require('lodash');
5
5
6
6
const logger = require ( './log' ) ;
7
7
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' ) ;
13
9
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
29
11
30
- const readModuleFile = ( kit , subPath ) => {
12
+ const readModuleFile = ( uikitLocation , subPath ) => {
31
13
return fs . readFileSync (
32
- path . resolve ( path . join ( kit . modulePath , subPath ) ) ,
14
+ path . resolve ( path . join ( uikitLocation , subPath ) ) ,
33
15
'utf8'
34
16
) ;
35
17
} ;
36
18
37
19
/**
38
20
* 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
42
24
* [4] Reads files from uikit that apply to every template
43
25
* @param {object } patternlab
44
26
*/
45
27
module . exports = patternlab => {
46
28
const paths = patternlab . config . paths ;
47
29
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
+ }
54
56
55
- if ( ! configEntry ) {
57
+ if ( ! uikitLocation ) {
56
58
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 .`
58
60
) ;
59
61
return ;
60
- }
62
+ } // [3]
61
63
62
64
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 ,
66
68
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 ,
70
72
header : readModuleFile (
71
- kit ,
73
+ uikitLocation ,
72
74
paths . source . patternlabFiles [ 'general-header' ]
73
75
) ,
74
76
footer : readModuleFile (
75
- kit ,
77
+ uikitLocation ,
76
78
paths . source . patternlabFiles [ 'general-footer' ]
77
79
) ,
78
80
patternSection : readModuleFile (
79
- kit ,
81
+ uikitLocation ,
80
82
paths . source . patternlabFiles . patternSection
81
83
) ,
82
84
patternSectionSubType : readModuleFile (
83
- kit ,
85
+ uikitLocation ,
84
86
paths . source . patternlabFiles . patternSectionSubtype
85
87
) ,
86
- viewAll : readModuleFile ( kit , paths . source . patternlabFiles . viewall ) ,
88
+ viewAll : readModuleFile (
89
+ uikitLocation ,
90
+ paths . source . patternlabFiles . viewall
91
+ ) ,
87
92
} ; // [4]
88
93
} catch ( ex ) {
89
94
logger . error ( ex ) ;
90
95
logger . error (
91
96
'\nERROR: missing an essential file from ' +
92
- kit . modulePath +
97
+ uikitLocation +
93
98
paths . source . patternlabFiles +
94
99
". Pattern Lab won't work without this file.\n"
95
100
) ;
0 commit comments