Skip to content

Commit d9fc327

Browse files
authored
util-context-installer: filter by package (#86)
* add option to filter by package name * add option to filter by package name * util-context-installer v0.3.0
1 parent da7efb8 commit d9fc327

File tree

7 files changed

+67
-19
lines changed

7 files changed

+67
-19
lines changed

packages/util-context-installer/HISTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# History
22

3+
## 0.3.0 (2022-04-07)
4+
* FEATURE: Add option to filter by specific package
5+
36
## 0.2.0 (2022-04-06)
47
* Change bin command
58
* Reorder CLI messaging

packages/util-context-installer/__mocks__/globby.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
const results = [
88
'toolkits/toolkit1/packages/package-a/package.json',
99
'toolkits/toolkit1/packages/package-b/package.json',
10-
'toolkits/toolkit2/packages/package-a/package.json',
11-
'toolkits/toolkit3/packages/package-a/package.json'
10+
'toolkits/toolkit2/packages/package-c/package.json',
11+
'toolkits/toolkit3/packages/package-d/package.json'
1212
];
1313

1414
async function globby(globPath) {
@@ -18,6 +18,12 @@ async function globby(globPath) {
1818
if (globPath.includes('path/to/fail/package/')) {
1919
return ['path/to/fail/package/package.json'];
2020
}
21+
if (globPath.includes('**/package-a')) {
22+
return ['toolkits/toolkit1/packages/package-a/package.json'];
23+
}
24+
if (globPath.includes('nothing/here/')) {
25+
return [];
26+
}
2127
return results;
2228
}
2329

packages/util-context-installer/__mocks__/read-pkg.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
'use strict';
66

7-
module.exports = async function (obj) {
7+
async function readPkg(obj) {
88
if (obj.cwd.includes('toolkits/toolkit1/packages/package-a')) {
99
return {name: 'package-a', brandContext: '^1.0.0'};
1010
}
@@ -13,17 +13,19 @@ module.exports = async function (obj) {
1313
return {name: 'package-b', brandContext: '^2.5.0'};
1414
}
1515

16-
if (obj.cwd.includes('toolkits/toolkit2/packages/package-a')) {
17-
return {name: 'package-a', brandContext: '^1.6.2'};
16+
if (obj.cwd.includes('toolkits/toolkit2/packages/package-c')) {
17+
return {name: 'package-c', brandContext: '^1.6.2'};
1818
}
1919

20-
if (obj.cwd.includes('toolkits/toolkit3/packages/package-a')) {
21-
return {name: 'package-a'};
20+
if (obj.cwd.includes('toolkits/toolkit3/packages/package-d')) {
21+
return {name: 'package-d'};
2222
}
2323

2424
if (obj.cwd.includes('path/to/fail/package')) {
2525
return {name: 'package', brandContext: 'not.valid.semver'};
2626
}
2727

2828
throw new Error('error');
29-
};
29+
}
30+
31+
module.exports = jest.fn(readPkg);

packages/util-context-installer/__tests__/unit/lib/js/installer.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const npmInstall = require('@springernature/util-package-installer');
77
const reporter = require('@springernature/util-cli-reporter');
88
const install = require('../../../../lib/js/installer');
99

10+
const readPkgMock = require('../../../../__mocks__/read-pkg');
11+
1012
console.log = jest.fn(); // silence log output from module under test
1113

1214
describe('util-package-installer', () => {
@@ -29,33 +31,57 @@ describe('util-package-installer', () => {
2931
dependenciesObjectSpy.mockRestore();
3032
cliReporterSpyWarning.mockRestore();
3133
cliReporterSpyFail.mockRestore();
34+
readPkgMock.mockClear();
3235
});
3336

3437
test('calls npmInstall.dependencies for each package found', async () => {
35-
expect.assertions(2);
38+
expect.assertions(5);
3639
await expect(
3740
install()
3841
).resolves.toEqual();
3942
expect(dependenciesObjectSpy).toHaveBeenCalledTimes(3);
43+
expect(readPkgMock).toHaveBeenNthCalledWith(1, {"cwd": path.resolve("toolkits/toolkit1/packages/package-a")})
44+
expect(readPkgMock).toHaveBeenNthCalledWith(2, {"cwd": path.resolve("toolkits/toolkit1/packages/package-b")})
45+
expect(readPkgMock).toHaveBeenNthCalledWith(3, {"cwd": path.resolve("toolkits/toolkit2/packages/package-c")});
4046
});
4147

42-
test('calls npmInstall.dependencies for each package found: error reading package.json', async () => {
48+
test('calls npmInstall.dependencies for each package found: filter by package name', async () => {
4349
expect.assertions(3);
50+
await expect(
51+
install(undefined, undefined, 'package-a')
52+
).resolves.toEqual();
53+
expect(dependenciesObjectSpy).toHaveBeenCalledTimes(1);
54+
expect(readPkgMock).toHaveBeenNthCalledWith(1, {"cwd": path.resolve("toolkits/toolkit1/packages/package-a")});
55+
});
56+
57+
test('calls npmInstall.dependencies for each package found: error reading package.json', async () => {
58+
expect.assertions(4);
4459
await expect(
4560
install('path/to/error/package/')
4661
).resolves.toEqual();
4762
expect(dependenciesObjectSpy).toHaveBeenCalledTimes(0);
4863
expect(cliReporterSpyWarning).toHaveBeenCalledTimes(1);
64+
expect(readPkgMock).toHaveBeenNthCalledWith(1, {"cwd": path.resolve("path/to/error/package/")})
4965
});
5066

5167
test('calls npmInstall.dependencies for each package found: error installing brand context', async () => {
52-
expect.assertions(3);
68+
expect.assertions(4);
5369

5470
await expect(
5571
install('path/to/fail/package/')
5672
).rejects.toThrow();
5773

5874
expect(dependenciesObjectSpy).toHaveBeenCalledTimes(1);
5975
expect(cliReporterSpyFail).toHaveBeenCalledTimes(1);
76+
expect(readPkgMock).toHaveBeenNthCalledWith(1, {"cwd": path.resolve("path/to/fail/package/")})
77+
});
78+
79+
test('does not call npmInstall.dependencies when no packages found', async () => {
80+
expect.assertions(3);
81+
await expect(
82+
install('nothing/here/')
83+
).resolves.toEqual();
84+
expect(dependenciesObjectSpy).toHaveBeenCalledTimes(0);
85+
expect(readPkgMock).toHaveBeenCalledTimes(0);
6086
});
6187
});

packages/util-context-installer/bin/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const argv = require('yargs')
55
.usage('Usage: $0 [options]')
6-
.example('$0 -p __dirname -c @springernature/brand-context', 'Install brand context inside packages')
6+
.example('$0 -p __dirname -c @springernature/brand-context -n name-of-package', 'Install brand context inside packages')
77
.alias('p', 'path')
88
.nargs('p', 1)
99
.describe('p', 'Install path. Start crawling from here')
@@ -12,6 +12,9 @@ const argv = require('yargs')
1212
.nargs('c', 1)
1313
.describe('c', 'Name of the brand context')
1414
.default('c', '@springernature/brand-context')
15+
.alias('n', 'name')
16+
.nargs('n', 1)
17+
.describe('n', 'Filter by a specific package')
1518
.help('h')
1619
.alias('h', 'help')
1720
.argv;
@@ -22,7 +25,8 @@ const install = require('../lib/js/installer');
2225
try {
2326
await install(
2427
(argv && argv.path) ? argv.path : null,
25-
(argv && argv.context) ? argv.context : null
28+
(argv && argv.context) ? argv.context : null,
29+
(argv && argv.name) ? argv.name : null
2630
);
2731
} catch (error) {
2832
console.error(error);

packages/util-context-installer/lib/js/installer.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ const getPackageJsonInfo = async packageJsonDir => {
6060

6161
/**
6262
* install brand-context dependency
63-
* @param {String} [installPath=__dirname] starting path for walking tree
63+
* @param {String} [startingPath=__dirname] starting path for walking tree
6464
* @param {String} [contextName='@springernature/brand-context'] name of the brand-context package
65+
* @param {String} packageName filter by particular package. Must be within startingPath directory tree
6566
* @return
6667
*/
67-
module.exports = async (installPath = __dirname, contextName = '@springernature/brand-context') => {
68+
module.exports = async (startingPath = __dirname, contextName = '@springernature/brand-context', packageName) => {
69+
const installPath = (packageName) ? path.join(startingPath, `/**/${packageName}/package.json`) : startingPath;
70+
6871
// Get list of package.json paths
6972
const paths = await globby(installPath, {
7073
expandDirectories: {
@@ -73,8 +76,10 @@ module.exports = async (installPath = __dirname, contextName = '@springernature/
7376
gitignore: true
7477
});
7578

76-
reporter.info('found', `${paths.length} packages`);
77-
reporter.info('installing', contextName);
79+
reporter.info('found', (paths.length === 0 || paths.length > 1) ? `${paths.length} packages` : path.dirname(paths[0]));
80+
if (paths.length > 0) {
81+
reporter.info('installing', contextName);
82+
}
7883

7984
// Loop through all paths and install brand-context
8085
await Promise.all(paths.map(async packageJsonPath => {
@@ -85,7 +90,9 @@ module.exports = async (installPath = __dirname, contextName = '@springernature/
8590
await installBrandContext(packageJsonDir, contextName, packageInfo.version);
8691
}
8792
})).then(() => {
88-
reporter.success('installation', 'complete');
93+
if (paths.length > 0) {
94+
reporter.success('installation', 'complete');
95+
}
8996
}).catch(error => {
9097
throw error;
9198
});

packages/util-context-installer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@springernature/util-context-installer",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Dynamically installs brand-context package to dependencies",
55
"main": "./lib/js/installer.js",
66
"repository": {

0 commit comments

Comments
 (0)