Skip to content

Commit

Permalink
fix: simplify validate-addons logic & npm-exists (#67)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
evenstensberg authored Feb 25, 2017
1 parent fe62523 commit 68a2dfd
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 56 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**/__testfixtures__/*
coverage
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ npm-debug.*

# yarn log
yarn-error.log

# Jest Coverage
coverage
24 changes: 5 additions & 19 deletions __mocks__/inquirer/initialize.mock.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
/* eslint node/no-unsupported-features: 0 */
'use strict';
const Rx = require('rx');
const got = require('got');
const questions = require('../../lib/utils/initial-questions');
const exists = require('../../lib/utils/npm-exists');
const initialConfig = require('../../lib/utils/initial-config');

//eslint-disable-next-line
const prompt = require('./prompt.mock');
const initialConfig = jest.genMockFromModule('../../lib/utils/initial-config');


function exists(pkg) {
const hostname = 'https://www.npmjs.org';
const pkgUrl = `${hostname}/package/${pkg}`;
return got(pkgUrl, {method: 'HEAD'})
.then( () => {
return true;
})
.catch(() => {
return false;
});
}

//
async function validateAddons(addon) {
async function npmPackagesExists(addon) {
let arr = [];
for(let k of addon) {
arr.push(await exists(k));
Expand All @@ -40,7 +27,6 @@ function init(pkg, answer) {


module.exports = {
exists,
validateAddons,
npmPackagesExists,
init
};
6 changes: 3 additions & 3 deletions lib/initialize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const questions = require('./utils/initial-questions');
const validateAddons = require('./utils/validate-addons');
const npmPackagesExists = require('./utils/npm-packages-exists');
const prompt = require('./inquirer-prompt');
const initialConfig = require('./utils/initial-config');
const Rx = require('rx');
Expand All @@ -11,7 +11,7 @@ const Rx = require('rx');
* if we are running the init command with no arguments or if we got dependencies
*
* @param { Object } pkg - packages included when running the init command
* @returns { <Function> } prompt|validateAddons - returns either an inquirer prompt with
* @returns { <Function> } prompt|npmPackagesExists - returns either an inquirer prompt with
* the initial questions we provide, or validates the packages given
*/

Expand All @@ -20,6 +20,6 @@ module.exports = function initializeInquirer(pkg) {
return prompt(Rx.Observable.from(questions), initialConfig);
}
else {
return validateAddons(pkg);
return npmPackagesExists(pkg);
}
};
2 changes: 1 addition & 1 deletion lib/utils/npm-exists.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint node/no-unsupported-features: 0 */
'use strict';
const {exists} = require('../../__mocks__/inquirer/initialize.mock');
const exists = require('./npm-exists');

describe('exists', () => {

Expand Down
41 changes: 41 additions & 0 deletions lib/utils/npm-packages-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const npmExists = require('./npm-exists');
const resolvePackages = require('./resolve-packages');


/*
* @function npmPackagesExists
*
* Loops through an array and checks if a package is registered
* on npm and throws an error if it is not from @checkEachPackage
*
* @param { Array <String> } pkg - Array of packages to check existence of
* @returns { Array } resolvePackages - Returns an process to install the pkg
*/

module.exports = function npmPackagesExists(addons) {
return addons.map( pkg => checkEachPackage(pkg));
};

/*
* @function checkEachPackage
*
* Checks if a package is registered on npm and throws if it is not
*
* @param { Object } pkg - pkg to check existence of
* @returns { <Function|Error> } resolvePackages - Returns an process to install the pkg
*/

function checkEachPackage(pkg) {
return npmExists(pkg).then( (moduleExists) => {
if(!moduleExists) {
Error.stackTraceLimit = 0;
throw new TypeError('Package isn\'t registered on npm.');
}
if (moduleExists) {
return resolvePackages(pkg);
}
}).catch(err => {
console.error(err.stack || err);
process.exit(0);
});
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint node/no-unsupported-features: 0 */
'use strict';

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

it('should validate multiple packages if supplied', async () => {
let itValidatesAddon = await validateAddons(['webpack-addons-ylvis', 'webpack-addons-noop']);
let itValidatesAddon = await npmPackagesExists(['webpack-addons-ylvis', 'webpack-addons-noop']);
// BUG: We are making the values strings, so the tests pass
expect(itValidatesAddon.toString()).toBe([true, false].toString());
});
Expand Down
30 changes: 0 additions & 30 deletions lib/utils/validate-addons.js

This file was deleted.

0 comments on commit 68a2dfd

Please sign in to comment.