From 101555689523487acf04f4c99fdbe863d259e062 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Thu, 27 Aug 2020 11:39:05 -0400 Subject: [PATCH] Refactor peerDependencies validation check. Using `ember-cli-version-checker` for this will not work, since that only checks Ember addons (and `qunit` is not an addon). --- index.js | 47 +++++++++++++++++++++++++++++++++++++---------- package.json | 2 +- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index c2708606..794b5381 100644 --- a/index.js +++ b/index.js @@ -2,10 +2,39 @@ 'use strict'; const path = require('path'); -const VersionChecker = require('ember-cli-version-checker'); +const semver = require('semver'); const SilentError = require('silent-error'); const stripIndent = require('common-tags').stripIndent; +// avoid checking multiple times from the same location +let HAS_PEER_DEPS_INSTALLED = null; +function hasPeerDependenciesInstalled() { + if (HAS_PEER_DEPS_INSTALLED !== null) { + return HAS_PEER_DEPS_INSTALLED; + } + + let peerDependencies = require('./package').peerDependencies; + + for (let packageName in peerDependencies) { + let minimumVersion = peerDependencies[packageName].substring(1); + + try { + let packageVersion = require(`${packageName}/package`).version; + if (semver.lt(packageVersion, minimumVersion)) { + return (HAS_PEER_DEPS_INSTALLED = false); + } + } catch (error) { + if (error.code !== 'MODULE_NOT_FOUND') { + throw error; + } + + return (HAS_PEER_DEPS_INSTALLED = false); + } + } + + return (HAS_PEER_DEPS_INSTALLED = true); +} + module.exports = { name: 'ember-qunit', @@ -18,18 +47,16 @@ module.exports = { included() { this._super.included.apply(this, arguments); - let peerDependencies = require('./package').peerDependencies; - let checker = VersionChecker.forProject(this.project); - - if (!checker.check(peerDependencies)) { - let packageNames = Object.keys(peerDependencies).map( - (name) => peerDependencies[name] + if (!hasPeerDependenciesInstalled()) { + let peerDependencies = require('./package').peerDependencies; + let packages = Object.keys(peerDependencies).map( + (name) => `"${name}@${peerDependencies[name]}"` ); let hasYarnLock = this.project.has('yarn.lock'); - let installMessage = hasYarnLock - ? `yarn add --dev ${packageNames.join(' ')}` - : `npm install --dev ${packageNames.join(' ')}`; + let installMessage = `${ + hasYarnLock ? 'yarn add' : 'npm install' + } --dev ${packages.join(' ')}`; throw new SilentError( `ember-qunit now requires that \`qunit\` and \`@ember/test-helpers\` are \`devDependencies\` of the project. Please run:\n\t${installMessage}` diff --git a/package.json b/package.json index 276e412a..759746f2 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "common-tags": "^1.8.0", "ember-cli-babel": "^7.22.1", "ember-cli-test-loader": "^3.0.0", - "ember-cli-version-checker": "^5.1.1", + "semver": "^7.3.2", "silent-error": "^1.1.1" }, "devDependencies": {