-
Notifications
You must be signed in to change notification settings - Fork 404
Loading uikit resources via the Node resolver #1246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a70231e
Use NodeJS require module to resolve packages and files/folders in it
ringods 77c3033
Moving the resolver functions to the core package
ringods 5e0e50c
Rewrote loading the UI kits starting from the config
ringods bb3b42f
Adding `package` property to allow a uikit to be used twice with a di…
ringods a567608
Remove obsolete comment
ringods 061348c
Prevent uikit warnings on our own packages
ringods 2adb96d
Fix the resource lookup for starterkits
ringods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,91 +5,110 @@ const _ = require('lodash'); | |
|
||
const logger = require('./log'); | ||
|
||
let findModules = require('./findModules'); //eslint-disable-line prefer-const | ||
let fs = require('fs-extra'); // eslint-disable-line | ||
|
||
const uiKitMatcher = /^uikit-(.*)$/; | ||
const nodeModulesPath = path.join(process.cwd(), 'node_modules'); | ||
|
||
/** | ||
* Given a path: return the uikit name if the path points to a valid uikit | ||
* module directory, or false if it doesn't. | ||
* @param filePath | ||
* @returns UIKit name if exists or FALSE | ||
*/ | ||
const isUIKitModule = filePath => { | ||
const baseName = path.basename(filePath); | ||
const engineMatch = baseName.match(uiKitMatcher); | ||
const { resolvePackageFolder } = require('./resolver'); | ||
|
||
if (engineMatch) { | ||
return engineMatch[1]; | ||
} | ||
return false; | ||
}; | ||
let fs = require('fs-extra'); // eslint-disable-line | ||
|
||
const readModuleFile = (kit, subPath) => { | ||
const readModuleFile = (uikitLocation, subPath) => { | ||
return fs.readFileSync( | ||
path.resolve(path.join(kit.modulePath, subPath)), | ||
path.resolve(path.join(uikitLocation, subPath)), | ||
'utf8' | ||
); | ||
}; | ||
|
||
/** | ||
* Loads uikits, connecting configuration and installed modules | ||
* [1] Looks in node_modules for uikits. | ||
* [2] Filter out our uikit-polyfills package. | ||
* [3] Only continue if uikit is enabled in patternlab-config.json | ||
* [1] Lists the enabled uikits from patternlab-config.json | ||
* [2] Try to resolve the location of the uikit in the package dependencies | ||
* [3] Warn when the uikit couldn't be loaded | ||
* [4] Reads files from uikit that apply to every template | ||
* @param {object} patternlab | ||
*/ | ||
module.exports = patternlab => { | ||
const paths = patternlab.config.paths; | ||
|
||
const uikits = findModules(nodeModulesPath, isUIKitModule) // [1] | ||
.filter(kit => kit.name !== 'polyfills'); // [2] | ||
uikits.forEach(kit => { | ||
const configEntry = _.find(_.filter(patternlab.config.uikits, 'enabled'), { | ||
name: `uikit-${kit.name}`, | ||
}); // [3] | ||
|
||
if (!configEntry) { | ||
logger.warning( | ||
`Could not find uikit with name uikit-${kit.name} defined within patternlab-config.json, or it is not enabled.` | ||
); | ||
return; | ||
const uikitConfigs = _.filter(patternlab.config.uikits, 'enabled'); // [1] | ||
uikitConfigs.forEach(uikitConfig => { | ||
let uikitLocation = null; | ||
if ('package' in uikitConfig) { | ||
try { | ||
uikitLocation = resolvePackageFolder(uikitConfig.package); | ||
} catch (ex) { | ||
logger.warning( | ||
`Could not find uikit with package name ${uikitConfig.package}. Did you add it to the 'dependencies' section in your 'package.json' file?` | ||
); | ||
return; | ||
} | ||
} else { | ||
// For backwards compatibility, name to package calculation is: | ||
// 1. name -> name | ||
// 2. name -> uikit-name | ||
// 3. name -> @pattern-lab/name | ||
// 4. name -> @pattern-lab/uikit-name | ||
for (const packageName of [ | ||
uikitConfig.name, | ||
`uikit-${uikitConfig.name}`, | ||
`@pattern-lab/${uikitConfig.name}`, | ||
`@pattern-lab/uikit-${uikitConfig.name}`, | ||
]) { | ||
try { | ||
uikitLocation = resolvePackageFolder(packageName); // [2] | ||
} catch (ex) { | ||
// Ignore | ||
} | ||
if (uikitLocation != null) { | ||
uikitConfig.package = packageName; | ||
logger.info(`Found uikit package ${packageName}`); | ||
break; | ||
} | ||
} | ||
if (uikitLocation == null) { | ||
logger.warning( | ||
`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.` | ||
); | ||
return; | ||
} else { | ||
logger.warning( | ||
`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.` | ||
); | ||
} // [3] | ||
Comment on lines
+70
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good message as info for the user |
||
} | ||
|
||
try { | ||
patternlab.uikits[`uikit-${kit.name}`] = { | ||
name: `uikit-${kit.name}`, | ||
modulePath: kit.modulePath, | ||
patternlab.uikits[uikitConfig.name] = { | ||
name: uikitConfig.name, | ||
package: uikitConfig.package, | ||
modulePath: uikitLocation, | ||
enabled: true, | ||
outputDir: configEntry.outputDir, | ||
excludedPatternStates: configEntry.excludedPatternStates, | ||
excludedTags: configEntry.excludedTags, | ||
outputDir: uikitConfig.outputDir, | ||
excludedPatternStates: uikitConfig.excludedPatternStates, | ||
excludedTags: uikitConfig.excludedTags, | ||
header: readModuleFile( | ||
kit, | ||
uikitLocation, | ||
paths.source.patternlabFiles['general-header'] | ||
), | ||
footer: readModuleFile( | ||
kit, | ||
uikitLocation, | ||
paths.source.patternlabFiles['general-footer'] | ||
), | ||
patternSection: readModuleFile( | ||
kit, | ||
uikitLocation, | ||
paths.source.patternlabFiles.patternSection | ||
), | ||
patternSectionSubType: readModuleFile( | ||
kit, | ||
uikitLocation, | ||
paths.source.patternlabFiles.patternSectionSubtype | ||
), | ||
viewAll: readModuleFile(kit, paths.source.patternlabFiles.viewall), | ||
viewAll: readModuleFile( | ||
uikitLocation, | ||
paths.source.patternlabFiles.viewall | ||
), | ||
}; // [4] | ||
} catch (ex) { | ||
logger.error(ex); | ||
logger.error( | ||
'\nERROR: missing an essential file from ' + | ||
kit.modulePath + | ||
uikitLocation + | ||
paths.source.patternlabFiles + | ||
". Pattern Lab won't work without this file.\n" | ||
); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
/** | ||
* @func resolveFileInPackage | ||
* Resolves a file inside a package | ||
*/ | ||
const resolveFileInPackage = (packageName, ...pathElements) => { | ||
return require.resolve(path.join(packageName, ...pathElements)); | ||
}; | ||
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked the spread syntax. It is available since Node V8.x |
||
|
||
/** | ||
* @func resolvePackageFolder | ||
* Resolves the location of a package on disc | ||
*/ | ||
const resolvePackageFolder = packageName => { | ||
return path.dirname(resolveFileInPackage(packageName, 'package.json')); | ||
}; | ||
|
||
/** | ||
* @func resolveDirInPackage | ||
* Resolves a file inside a package | ||
*/ | ||
const resolveDirInPackage = (packageName, ...pathElements) => { | ||
return path.join(resolvePackageFolder(packageName), ...pathElements); | ||
}; | ||
|
||
module.exports = { | ||
resolveFileInPackage, | ||
resolveDirInPackage, | ||
resolvePackageFolder, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good fallback