Skip to content

Commit 31bc3f4

Browse files
committed
Use NodeJS require module to resolve packages and files/folders in it
1 parent 7050836 commit 31bc3f4

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

packages/cli/bin/install-edition.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const {
1010
wrapAsync,
1111
writeJsonAsync,
1212
getJSONKey,
13+
resolveFileInPackage,
14+
resolveDirInPackage,
1315
} = require('./utils');
1416

1517
// https://github.com/TehShrike/deepmerge#overwrite-array
@@ -31,7 +33,7 @@ const installEdition = (edition, config, projectDir) => {
3133
const sourceDir = config.paths.source.root;
3234
yield checkAndInstallPackage(edition); // 1
3335
yield copyAsync(
34-
path.resolve('./node_modules', edition, 'source', '_meta'),
36+
resolveDirInPackage(edition, 'source', '_meta'),
3537
path.resolve(sourceDir, '_meta')
3638
); // 2
3739
pkg.dependencies = Object.assign(
@@ -45,16 +47,15 @@ const installEdition = (edition, config, projectDir) => {
4547
// 4.1
4648
case '@pattern-lab/edition-node-gulp': {
4749
yield copyAsync(
48-
path.resolve('./node_modules', edition, 'gulpfile.js'),
50+
resolveFileInPackage(edition, 'gulpfile.js'),
4951
path.resolve(sourceDir, '../', 'gulpfile.js')
5052
);
5153
break;
5254
}
5355
// 4.2
5456
case '@pattern-lab/edition-node': {
55-
const editionPath = path.resolve('./node_modules', edition);
56-
const editionConfigPath = path.resolve(
57-
editionPath,
57+
const editionConfigPath = resolveFileInPackage(
58+
edition,
5859
'patternlab-config.json'
5960
);
6061

@@ -67,7 +68,7 @@ const installEdition = (edition, config, projectDir) => {
6768
);
6869

6970
yield copyAsync(
70-
path.join(editionPath, path.sep, 'helpers', path.sep, 'test.js'),
71+
resolveFileInPackage(edition, 'helpers', 'test.js'),
7172
path.resolve(sourceDir, '../', 'helpers/test.js')
7273
);
7374

@@ -76,9 +77,8 @@ const installEdition = (edition, config, projectDir) => {
7677
}
7778
// 4.3
7879
case '@pattern-lab/edition-twig': {
79-
const editionPath = path.resolve('./node_modules', edition);
80-
const editionConfigPath = path.resolve(
81-
editionPath,
80+
const editionConfigPath = resolveFileInPackage(
81+
edition,
8282
'patternlab-config.json'
8383
);
8484
const editionConfig = require(editionConfigPath);
@@ -90,7 +90,7 @@ const installEdition = (edition, config, projectDir) => {
9090
);
9191

9292
yield copyAsync(
93-
path.resolve(editionPath, 'alter-twig.php'),
93+
resolveFileInPackage(edition, 'alter-twig.php'),
9494
path.resolve(sourceDir, '../', 'alter-twig.php')
9595
);
9696

packages/cli/bin/install-plugin.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

3-
const path = require('path');
4-
53
const _ = require('lodash');
64

7-
const checkAndInstallPackage = require('./utils').checkAndInstallPackage;
8-
const wrapAsync = require('./utils').wrapAsync;
5+
const {
6+
checkAndInstallPackage,
7+
wrapAsync,
8+
resolveFileInPackage,
9+
} = require('./utils');
910

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

1819
// Get the options from the plugin, if any
19-
const pluginPathConfig = path.resolve(
20-
path.join(process.cwd(), 'node_modules', name, 'config.json')
21-
);
20+
const pluginPathConfig = resolveFileInPackage(name, 'config.json');
2221
try {
2322
const pluginConfigJSON = require(pluginPathConfig);
2423
if (!_.has(config.plugins[name].options)) {

packages/cli/bin/install-starterkit.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ const {
66
wrapAsync,
77
checkAndInstallPackage,
88
readJsonAsync,
9+
resolveFileInPackage,
10+
resolveDirInPackage,
911
} = require('./utils');
1012

1113
const installStarterkit = (starterkit, config) =>
1214
wrapAsync(function*() {
1315
const sourceDir = config.paths.source.root;
1416
const name = starterkit.value || starterkit;
1517
yield checkAndInstallPackage(name);
16-
const kitPath = path.resolve('./node_modules', name);
17-
yield copyAsync(path.resolve(kitPath, 'dist'), path.resolve(sourceDir));
18+
yield copyAsync(resolveDirInPackage(name, 'dist'), path.resolve(sourceDir));
1819
let kitConfig;
19-
const kitConfigPath = path.resolve(kitPath, 'patternlab-config.json');
20+
const kitConfigPath = resolveFileInPackage(name, 'patternlab-config.json');
2021
if (fs.existsSync(kitConfigPath)) {
2122
kitConfig = yield readJsonAsync(kitConfigPath);
2223
}

packages/cli/bin/utils.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const copyWithPattern = (cwd, pattern, dest) =>
124124

125125
/**
126126
* @func fetchPackage
127-
* @desc Fetches and saves packages from npm into node_modules and adds a reference in the package.json under dependencies
127+
* @desc Fetches packages from an npm package registry and adds a reference in the package.json under dependencies
128128
* @param {string} packageName - The package name
129129
*/
130130
const fetchPackage = packageName =>
@@ -182,6 +182,30 @@ const writeJsonAsync = (filePath, data) =>
182182
yield fs.outputJSON(filePath, data, { spaces: 2 });
183183
});
184184

185+
/**
186+
* @func resolveFileInPackage
187+
* Resolves a file inside a package
188+
*/
189+
const resolveFileInPackage = (packageName, ...pathElements) => {
190+
return require.resolve(path.join(packageName, ...pathElements));
191+
};
192+
193+
/**
194+
* @func resolveDirInPackage
195+
* Resolves a file inside a package
196+
*/
197+
const resolveDirInPackage = (packageName, ...pathElements) => {
198+
return path.dirname(resolveFileInPackage(packageName, pathElements));
199+
};
200+
201+
/**
202+
* @func resolvePackageFolder
203+
* Resolves the location of a package on disc
204+
*/
205+
const resolvePackageFolder = packageName => {
206+
return path.dirname(resolveDirInPackage(packageName, 'package.json'));
207+
};
208+
185209
/**
186210
* @func getJSONKey
187211
* Installs package, then returns the value for the given JSON file's key within
@@ -193,7 +217,7 @@ const getJSONKey = (packageName, key, fileName = 'package.json') =>
193217
wrapAsync(function*() {
194218
yield checkAndInstallPackage(packageName);
195219
const jsonData = yield fs.readJson(
196-
path.resolve('node_modules', packageName, fileName)
220+
resolveFileInPackage(packageName, fileName)
197221
);
198222
return jsonData[key];
199223
});
@@ -213,4 +237,7 @@ module.exports = {
213237
checkAndInstallPackage,
214238
noop,
215239
getJSONKey,
240+
resolveFileInPackage,
241+
resolveDirInPackage,
242+
resolvePackageFolder,
216243
};

packages/core/src/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ const patternlab_module = function(config) {
205205
},
206206

207207
/**
208-
* Installs plugin already available via `node_modules/`
208+
* Installs plugin already available as a package dependency
209209
*
210210
* @memberof patternlab
211211
* @name installplugin
@@ -215,7 +215,6 @@ const patternlab_module = function(config) {
215215
*/
216216
installplugin: function(pluginName) {
217217
//get the config
218-
const configPath = path.resolve(process.cwd(), 'patternlab-config.json');
219218
const plugin_manager = new pm();
220219

221220
plugin_manager.install_plugin(pluginName);
@@ -234,7 +233,7 @@ const patternlab_module = function(config) {
234233
},
235234

236235
/**
237-
* Loads starterkit already available via `node_modules/`
236+
* Loads starterkit already available as a package dependency
238237
*
239238
* @memberof patternlab
240239
* @name loadstarterkit

0 commit comments

Comments
 (0)