Skip to content

Commit 68a2dfd

Browse files
fix: simplify validate-addons logic & npm-exists (#67)
* hotfix: simplify validate-addons logic & npm-exists Simplifies the usage of validate-addons and replaces the manual mock of nom-exists with the actual. fix: refactor function convention and add jsdcos fix: rename validateAddons to npmPackagesExists Renames the validateAddons packages to npmPackagesExists. Synced with master to get latest changes from origin. Also added lint ignore to coverage folder. fix: remove coverage folder from commit fix: rename test header * fix: remove coverage from build fix: remove coverage folder
1 parent fe62523 commit 68a2dfd

File tree

8 files changed

+57
-56
lines changed

8 files changed

+57
-56
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/__testfixtures__/*
2+
coverage

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ npm-debug.*
1111

1212
# yarn log
1313
yarn-error.log
14+
15+
# Jest Coverage
16+
coverage

__mocks__/inquirer/initialize.mock.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
/* eslint node/no-unsupported-features: 0 */
22
'use strict';
33
const Rx = require('rx');
4-
const got = require('got');
54
const questions = require('../../lib/utils/initial-questions');
5+
const exists = require('../../lib/utils/npm-exists');
6+
const initialConfig = require('../../lib/utils/initial-config');
7+
68
//eslint-disable-next-line
79
const prompt = require('./prompt.mock');
8-
const initialConfig = jest.genMockFromModule('../../lib/utils/initial-config');
9-
10-
11-
function exists(pkg) {
12-
const hostname = 'https://www.npmjs.org';
13-
const pkgUrl = `${hostname}/package/${pkg}`;
14-
return got(pkgUrl, {method: 'HEAD'})
15-
.then( () => {
16-
return true;
17-
})
18-
.catch(() => {
19-
return false;
20-
});
21-
}
2210

23-
//
24-
async function validateAddons(addon) {
11+
async function npmPackagesExists(addon) {
2512
let arr = [];
2613
for(let k of addon) {
2714
arr.push(await exists(k));
@@ -40,7 +27,6 @@ function init(pkg, answer) {
4027

4128

4229
module.exports = {
43-
exists,
44-
validateAddons,
30+
npmPackagesExists,
4531
init
4632
};

lib/initialize.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const questions = require('./utils/initial-questions');
2-
const validateAddons = require('./utils/validate-addons');
2+
const npmPackagesExists = require('./utils/npm-packages-exists');
33
const prompt = require('./inquirer-prompt');
44
const initialConfig = require('./utils/initial-config');
55
const Rx = require('rx');
@@ -11,7 +11,7 @@ const Rx = require('rx');
1111
* if we are running the init command with no arguments or if we got dependencies
1212
*
1313
* @param { Object } pkg - packages included when running the init command
14-
* @returns { <Function> } prompt|validateAddons - returns either an inquirer prompt with
14+
* @returns { <Function> } prompt|npmPackagesExists - returns either an inquirer prompt with
1515
* the initial questions we provide, or validates the packages given
1616
*/
1717

@@ -20,6 +20,6 @@ module.exports = function initializeInquirer(pkg) {
2020
return prompt(Rx.Observable.from(questions), initialConfig);
2121
}
2222
else {
23-
return validateAddons(pkg);
23+
return npmPackagesExists(pkg);
2424
}
2525
};

lib/utils/npm-exists.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint node/no-unsupported-features: 0 */
22
'use strict';
3-
const {exists} = require('../../__mocks__/inquirer/initialize.mock');
3+
const exists = require('./npm-exists');
44

55
describe('exists', () => {
66

lib/utils/npm-packages-exists.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const npmExists = require('./npm-exists');
2+
const resolvePackages = require('./resolve-packages');
3+
4+
5+
/*
6+
* @function npmPackagesExists
7+
*
8+
* Loops through an array and checks if a package is registered
9+
* on npm and throws an error if it is not from @checkEachPackage
10+
*
11+
* @param { Array <String> } pkg - Array of packages to check existence of
12+
* @returns { Array } resolvePackages - Returns an process to install the pkg
13+
*/
14+
15+
module.exports = function npmPackagesExists(addons) {
16+
return addons.map( pkg => checkEachPackage(pkg));
17+
};
18+
19+
/*
20+
* @function checkEachPackage
21+
*
22+
* Checks if a package is registered on npm and throws if it is not
23+
*
24+
* @param { Object } pkg - pkg to check existence of
25+
* @returns { <Function|Error> } resolvePackages - Returns an process to install the pkg
26+
*/
27+
28+
function checkEachPackage(pkg) {
29+
return npmExists(pkg).then( (moduleExists) => {
30+
if(!moduleExists) {
31+
Error.stackTraceLimit = 0;
32+
throw new TypeError('Package isn\'t registered on npm.');
33+
}
34+
if (moduleExists) {
35+
return resolvePackages(pkg);
36+
}
37+
}).catch(err => {
38+
console.error(err.stack || err);
39+
process.exit(0);
40+
});
41+
}

lib/utils/validate-addons.spec.js renamed to lib/utils/npm-packages-exists.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint node/no-unsupported-features: 0 */
22
'use strict';
33

4-
describe('validate-addons', () => {
4+
describe('npm-packages-exists', () => {
55
//eslint-disable-next-line
6-
const {validateAddons} = require('../../__mocks__/inquirer/initialize.mock');
6+
const {npmPackagesExists} = require('../../__mocks__/inquirer/initialize.mock');
77

88
it('should validate multiple packages if supplied', async () => {
9-
let itValidatesAddon = await validateAddons(['webpack-addons-ylvis', 'webpack-addons-noop']);
9+
let itValidatesAddon = await npmPackagesExists(['webpack-addons-ylvis', 'webpack-addons-noop']);
1010
// BUG: We are making the values strings, so the tests pass
1111
expect(itValidatesAddon.toString()).toBe([true, false].toString());
1212
});

lib/utils/validate-addons.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)