Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions packages/cli/bin/install-edition.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const {
writeJsonAsync,
getJSONKey,
} = require('./utils');
const {
resolveFileInPackage,
resolveDirInPackage,
} = require('@pattern-lab/core/src/lib/resolver');

// https://github.com/TehShrike/deepmerge#overwrite-array
const overwriteMerge = (destinationArray, sourceArray, options) => sourceArray;
Expand All @@ -35,7 +31,7 @@ const installEdition = (edition, config, projectDir) => {
const sourceDir = config.paths.source.root;
yield checkAndInstallPackage(edition); // 1
yield copyAsync(
resolveDirInPackage(edition, 'source', '_meta'),
path.resolve('./node_modules', edition, 'source', '_meta'),
path.resolve(sourceDir, '_meta')
); // 2
pkg.dependencies = Object.assign(
Expand All @@ -49,15 +45,16 @@ const installEdition = (edition, config, projectDir) => {
// 4.1
case '@pattern-lab/edition-node-gulp': {
yield copyAsync(
resolveFileInPackage(edition, 'gulpfile.js'),
path.resolve('./node_modules', edition, 'gulpfile.js'),
path.resolve(sourceDir, '../', 'gulpfile.js')
);
break;
}
// 4.2
case '@pattern-lab/edition-node': {
const editionConfigPath = resolveFileInPackage(
edition,
const editionPath = path.resolve('./node_modules', edition);
const editionConfigPath = path.resolve(
editionPath,
'patternlab-config.json'
);

Expand All @@ -70,7 +67,7 @@ const installEdition = (edition, config, projectDir) => {
);

yield copyAsync(
resolveFileInPackage(edition, 'helpers', 'test.js'),
path.join(editionPath, path.sep, 'helpers', path.sep, 'test.js'),
path.resolve(sourceDir, '../', 'helpers/test.js')
);

Expand All @@ -79,8 +76,9 @@ const installEdition = (edition, config, projectDir) => {
}
// 4.3
case '@pattern-lab/edition-twig': {
const editionConfigPath = resolveFileInPackage(
edition,
const editionPath = path.resolve('./node_modules', edition);
const editionConfigPath = path.resolve(
editionPath,
'patternlab-config.json'
);
const editionConfig = require(editionConfigPath);
Expand All @@ -92,7 +90,7 @@ const installEdition = (edition, config, projectDir) => {
);

yield copyAsync(
resolveFileInPackage(edition, 'alter-twig.php'),
path.resolve(editionPath, 'alter-twig.php'),
path.resolve(sourceDir, '../', 'alter-twig.php')
);

Expand Down
10 changes: 7 additions & 3 deletions packages/cli/bin/install-plugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const path = require('path');

const _ = require('lodash');

const { checkAndInstallPackage, wrapAsync } = require('./utils');
const { resolveFileInPackage } = require('@pattern-lab/core/src/lib/resolver');
const checkAndInstallPackage = require('./utils').checkAndInstallPackage;
const wrapAsync = require('./utils').wrapAsync;

const installPlugin = (plugin, config) =>
wrapAsync(function*() {
Expand All @@ -14,7 +16,9 @@ const installPlugin = (plugin, config) =>
_.set(config, `plugins[${name}]['initialized']`, false);

// Get the options from the plugin, if any
const pluginPathConfig = resolveFileInPackage(name, 'config.json');
const pluginPathConfig = path.resolve(
path.join(process.cwd(), 'node_modules', name, 'config.json')
);
try {
const pluginConfigJSON = require(pluginPathConfig);
if (!_.has(config.plugins[name].options)) {
Expand Down
9 changes: 3 additions & 6 deletions packages/cli/bin/install-starterkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ const {
checkAndInstallPackage,
readJsonAsync,
} = require('./utils');
const {
resolveFileInPackage,
resolveDirInPackage,
} = require('@pattern-lab/core/src/lib/resolver');

const installStarterkit = (starterkit, config) =>
wrapAsync(function*() {
const sourceDir = config.paths.source.root;
const name = starterkit.value || starterkit;
yield checkAndInstallPackage(name);
yield copyAsync(resolveDirInPackage(name, 'dist'), path.resolve(sourceDir));
const kitPath = path.resolve('./node_modules', name);
yield copyAsync(path.resolve(kitPath, 'dist'), path.resolve(sourceDir));
let kitConfig;
const kitConfigPath = resolveFileInPackage(name, 'patternlab-config.json');
const kitConfigPath = path.resolve(kitPath, 'patternlab-config.json');
if (fs.existsSync(kitConfigPath)) {
kitConfig = yield readJsonAsync(kitConfigPath);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const path = require('path');
const chalk = require('chalk');
const EventEmitter = require('events').EventEmitter;
const hasYarn = require('has-yarn');
const { resolveFileInPackage } = require('@pattern-lab/core/src/lib/resolver');

/**
* @name log
Expand Down Expand Up @@ -125,7 +124,7 @@ const copyWithPattern = (cwd, pattern, dest) =>

/**
* @func fetchPackage
* @desc Fetches packages from an npm package registry and adds a reference in the package.json under dependencies
* @desc Fetches and saves packages from npm into node_modules and adds a reference in the package.json under dependencies
* @param {string} packageName - The package name
*/
const fetchPackage = packageName =>
Expand Down Expand Up @@ -194,7 +193,7 @@ const getJSONKey = (packageName, key, fileName = 'package.json') =>
wrapAsync(function*() {
yield checkAndInstallPackage(packageName);
const jsonData = yield fs.readJson(
resolveFileInPackage(packageName, fileName)
path.resolve('node_modules', packageName, fileName)
);
return jsonData[key];
});
Expand Down
1 change: 0 additions & 1 deletion packages/core/patternlab-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
"uikits": [
{
"name": "uikit-workshop",
"package": "@pattern-lab/uikit-workshop",
"outputDir": "",
"enabled": true,
"excludedPatternStates": [],
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const patternlab_module = function(config) {
},

/**
* Installs plugin already available as a package dependency
* Installs plugin already available via `node_modules/`
*
* @memberof patternlab
* @name installplugin
Expand All @@ -214,6 +214,8 @@ const patternlab_module = function(config) {
* @returns {void}
*/
installplugin: function(pluginName) {
//get the config
const configPath = path.resolve(process.cwd(), 'patternlab-config.json');
const plugin_manager = new pm();

plugin_manager.install_plugin(pluginName);
Expand All @@ -232,7 +234,7 @@ const patternlab_module = function(config) {
},

/**
* Loads starterkit already available as a package dependency
* Loads starterkit already available via `node_modules/`
*
* @memberof patternlab
* @name loadstarterkit
Expand Down
117 changes: 49 additions & 68 deletions packages/core/src/lib/loaduikits.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,110 +5,91 @@ const _ = require('lodash');

const logger = require('./log');

const { resolvePackageFolder } = require('./resolver');

let findModules = require('./findModules'); //eslint-disable-line prefer-const
let fs = require('fs-extra'); // eslint-disable-line

const readModuleFile = (uikitLocation, subPath) => {
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);

if (engineMatch) {
return engineMatch[1];
}
return false;
};

const readModuleFile = (kit, subPath) => {
return fs.readFileSync(
path.resolve(path.join(uikitLocation, subPath)),
path.resolve(path.join(kit.modulePath, subPath)),
'utf8'
);
};

/**
* Loads uikits, connecting configuration and installed modules
* [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
* [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
* [4] Reads files from uikit that apply to every template
* @param {object} patternlab
*/
module.exports = patternlab => {
const paths = patternlab.config.paths;

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]
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;
}

try {
patternlab.uikits[uikitConfig.name] = {
name: uikitConfig.name,
package: uikitConfig.package,
modulePath: uikitLocation,
patternlab.uikits[`uikit-${kit.name}`] = {
name: `uikit-${kit.name}`,
modulePath: kit.modulePath,
enabled: true,
outputDir: uikitConfig.outputDir,
excludedPatternStates: uikitConfig.excludedPatternStates,
excludedTags: uikitConfig.excludedTags,
outputDir: configEntry.outputDir,
excludedPatternStates: configEntry.excludedPatternStates,
excludedTags: configEntry.excludedTags,
header: readModuleFile(
uikitLocation,
kit,
paths.source.patternlabFiles['general-header']
),
footer: readModuleFile(
uikitLocation,
kit,
paths.source.patternlabFiles['general-footer']
),
patternSection: readModuleFile(
uikitLocation,
kit,
paths.source.patternlabFiles.patternSection
),
patternSectionSubType: readModuleFile(
uikitLocation,
kit,
paths.source.patternlabFiles.patternSectionSubtype
),
viewAll: readModuleFile(
uikitLocation,
paths.source.patternlabFiles.viewall
),
viewAll: readModuleFile(kit, paths.source.patternlabFiles.viewall),
}; // [4]
} catch (ex) {
logger.error(ex);
logger.error(
'\nERROR: missing an essential file from ' +
uikitLocation +
kit.modulePath +
paths.source.patternlabFiles +
". Pattern Lab won't work without this file.\n"
);
Expand Down
33 changes: 0 additions & 33 deletions packages/core/src/lib/resolver.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/lib/starterkit_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const starterkit_manager = function(config) {
kitDirStats = fs.statSync(kitPath);
} catch (ex) {
logger.warning(
`${starterkitName} not found, use npm or another package manager to install it first.`
`${starterkitName} not found, use npm to install it first.`
);
logger.warning(`${starterkitName} not loaded.`);
return;
Expand Down
Loading