Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor peerDependencies validation check. #749

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand All @@ -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}`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down