From 68a2dfdf26116763e27304abce1e29111984fdc5 Mon Sep 17 00:00:00 2001 From: Even Stensberg Date: Sat, 25 Feb 2017 20:03:57 +0100 Subject: [PATCH] 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 --- .eslintignore | 1 + .gitignore | 3 ++ __mocks__/inquirer/initialize.mock.js | 24 +++-------- lib/initialize.js | 6 +-- lib/utils/npm-exists.spec.js | 2 +- lib/utils/npm-packages-exists.js | 41 +++++++++++++++++++ ...ns.spec.js => npm-packages-exists.spec.js} | 6 +-- lib/utils/validate-addons.js | 30 -------------- 8 files changed, 57 insertions(+), 56 deletions(-) create mode 100644 lib/utils/npm-packages-exists.js rename lib/utils/{validate-addons.spec.js => npm-packages-exists.spec.js} (57%) delete mode 100644 lib/utils/validate-addons.js diff --git a/.eslintignore b/.eslintignore index a7cecca09b8..2eee14cbe2a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ **/__testfixtures__/* +coverage diff --git a/.gitignore b/.gitignore index ecf9d72c9a9..79960dd2d49 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ npm-debug.* # yarn log yarn-error.log + +# Jest Coverage +coverage diff --git a/__mocks__/inquirer/initialize.mock.js b/__mocks__/inquirer/initialize.mock.js index 79316222e5b..e90feccfab6 100644 --- a/__mocks__/inquirer/initialize.mock.js +++ b/__mocks__/inquirer/initialize.mock.js @@ -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)); @@ -40,7 +27,6 @@ function init(pkg, answer) { module.exports = { - exists, - validateAddons, + npmPackagesExists, init }; diff --git a/lib/initialize.js b/lib/initialize.js index efb7e805460..60276d2b00f 100644 --- a/lib/initialize.js +++ b/lib/initialize.js @@ -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'); @@ -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 { } prompt|validateAddons - returns either an inquirer prompt with +* @returns { } prompt|npmPackagesExists - returns either an inquirer prompt with * the initial questions we provide, or validates the packages given */ @@ -20,6 +20,6 @@ module.exports = function initializeInquirer(pkg) { return prompt(Rx.Observable.from(questions), initialConfig); } else { - return validateAddons(pkg); + return npmPackagesExists(pkg); } }; diff --git a/lib/utils/npm-exists.spec.js b/lib/utils/npm-exists.spec.js index a526b4efb39..b192127a4be 100644 --- a/lib/utils/npm-exists.spec.js +++ b/lib/utils/npm-exists.spec.js @@ -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', () => { diff --git a/lib/utils/npm-packages-exists.js b/lib/utils/npm-packages-exists.js new file mode 100644 index 00000000000..8748b1e47ac --- /dev/null +++ b/lib/utils/npm-packages-exists.js @@ -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 } 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 { } 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); + }); +} diff --git a/lib/utils/validate-addons.spec.js b/lib/utils/npm-packages-exists.spec.js similarity index 57% rename from lib/utils/validate-addons.spec.js rename to lib/utils/npm-packages-exists.spec.js index 430b44c4c7e..dd839b0fb61 100644 --- a/lib/utils/validate-addons.spec.js +++ b/lib/utils/npm-packages-exists.spec.js @@ -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()); }); diff --git a/lib/utils/validate-addons.js b/lib/utils/validate-addons.js deleted file mode 100644 index 3fe6efa32b1..00000000000 --- a/lib/utils/validate-addons.js +++ /dev/null @@ -1,30 +0,0 @@ -const exists = require('./npm-exists'); -const resolvePackages = require('./resolve-packages'); - -/* -* @function validateAddons -* -* Checks if a package is registered on npm and throws if it is not -* -* @param { Array } addons - packages included when running the init command -* @returns { } resolvePackages - Returns a call to the addon -*/ - -module.exports = function validateAddons(addons) { - Error.stackTraceLimit = 4; - return addons.map( pkg => { - //eslint-disable-next-line - exists(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); - }); - }); -};