Repository: electron-userland/electron-forge
Pull request #377 (2017-11-27 08:27:46)

fix(generic): assume invalid semver package manager versions are incompatible
* [x] I have read the [contribution documentation](https://github.com/electron-userland/electron-forge/blob/master/CONTRIBUTING.md) for this project.
* [x] I agree to follow the [code of conduct](https://github.com/electron/electron/blob/master/CODE_OF_CONDUCT.md) that this project follows, as appropriate.
* [x] The changes are appropriately documented (if applicable).
* [x] The changes have sufficient test coverage (if applicable).
* [x] The testsuite passes successfully on my local machine (if applicable).

**Summarize your changes:**

I replaced `isNightlyYarnVersion`, but I'm not sure if we need to emit a warning in that case.

ISSUES CLOSED: #376


Patch:
diff --git a/package.json b/package.json
index cdbefcd5f..b0002aad9 100644
--- a/package.json
+++ b/package.json
@@ -34,7 +34,7 @@
   "author": "Samuel Attard",
   "license": "MIT",
   "devDependencies": {
-    "asar": "^0.13.0",
+    "asar": "^0.14.0",
     "babel-eslint": "^7.0.0",
     "babel-plugin-istanbul": "^4.0.0",
     "babel-plugin-syntax-async-functions": "^6.13.0",
@@ -44,7 +44,7 @@
     "chai": "^4.0.0",
     "chai-as-promised": "^7.0.0",
     "commitizen": "^2.8.6",
-    "coveralls": "^2.11.15",
+    "coveralls": "^3.0.0",
     "cross-env": "^5.0.0",
     "cz-customizable": "^5.0.0",
     "esdoc": "^1.0.1",
@@ -58,11 +58,11 @@
     "generate-changelog": "^1.0.2",
     "gulp": "^3.9.1",
     "gulp-babel": "^6.1.2",
-    "mocha": "^3.2.0",
+    "mocha": "^4.0.1",
     "nodemon": "^1.11.0",
     "nyc": "^11.0.0",
     "proxyquire": "^1.7.10",
-    "sinon": "^3.0.0"
+    "sinon": "^4.1.2"
   },
   "babel": {
     "sourceMaps": "inline",
@@ -117,13 +117,13 @@
     "electron-forge-template-react": "^1.0.2",
     "electron-forge-template-react-typescript": "^1.0.3",
     "electron-forge-template-vue": "^1.0.2",
-    "electron-packager": "^9.1.0",
+    "electron-packager": "^10.1.0",
     "electron-rebuild": "^1.6.0",
     "form-data": "^2.1.4",
     "fs-extra": "^4.0.0",
-    "github": "^11.0.0",
+    "github": "^12.0.3",
     "glob": "^7.1.1",
-    "inquirer": "^3.0.1",
+    "inquirer": "^4.0.0",
     "lodash.template": "^4.4.0",
     "log-symbols": "^2.0.0",
     "node-fetch": "^1.6.3",
@@ -136,7 +136,7 @@
     "resolve-package": "^1.0.1",
     "s3": "^4.4.0",
     "semver": "^5.3.0",
-    "sudo-prompt": "^7.0.0",
+    "sudo-prompt": "^8.0.0",
     "tabtab": "^2.2.1",
     "username": "^3.0.0",
     "yarn-or-npm": "^2.0.2",
@@ -151,7 +151,7 @@
     }
   },
   "optionalDependencies": {
-    "electron-installer-debian": "^0.5.0",
+    "electron-installer-debian": "^0.7.1",
     "electron-installer-dmg": "^0.2.0",
     "electron-installer-flatpak": "^0.6.0",
     "electron-installer-redhat": "^0.5.0",
diff --git a/src/util/check-system.js b/src/util/check-system.js
index a9daf1898..333225576 100644
--- a/src/util/check-system.js
+++ b/src/util/check-system.js
@@ -1,8 +1,11 @@
 import { exec } from 'child_process';
+import debug from 'debug';
 import semver from 'semver';
 
 import { hasYarn, yarnOrNpmSpawn } from './yarn-or-npm';
 
+const d = debug('electron-forge:check-system');
+
 async function checkGitExists() {
   return new Promise((resolve) => {
     exec('git --version', (err) => {
@@ -27,15 +30,21 @@ const YARN_WHITELISTED_VERSIONS = {
   linux: '0.27.5',
 };
 
-export function isNightlyYarnVersion(version) {
-  return /((?:\d\.?)+)-\d.*/.test(version);
+export function validPackageManagerVersion(packageManager, version, whitelistedVersions, ora) {
+  try {
+    return semver.satisfies(version, whitelistedVersions);
+  } catch (e) {
+    ora.warn(`Could not check ${packageManager} version "${version}", assuming incompatible`);
+    d(`Exception while checking version: ${e}`);
+    return false;
+  }
 }
 
 function warnIfPackageManagerIsntAKnownGoodVersion(packageManager, version, whitelistedVersions, ora) {
   const osVersions = whitelistedVersions[process.platform];
   const versions = osVersions ? `${whitelistedVersions.all} || ${osVersions}` : whitelistedVersions.all;
   const versionString = version.toString();
-  if (isNightlyYarnVersion(versionString) || !semver.satisfies(versionString, versions)) {
+  if (!validPackageManagerVersion(packageManager, versionString, versions, ora)) {
     ora.warn(
       `You are using ${packageManager}, but not a known good version.\n` +
       `The known versions that work with Electron Forge are: ${versions}`


Test patch:
diff --git a/test/fast/system_spec.js b/test/fast/system_spec.js
index d2ff6eb19..b17ca8c71 100644
--- a/test/fast/system_spec.js
+++ b/test/fast/system_spec.js
@@ -1,6 +1,6 @@
 import { expect } from 'chai';
 
-import checkSystem, { isNightlyYarnVersion } from '../../src/util/check-system';
+import checkSystem, { validPackageManagerVersion } from '../../src/util/check-system';
 import { fakeOra } from '../../src/util/ora';
 
 describe('check-system', () => {
@@ -8,17 +8,17 @@ describe('check-system', () => {
     expect(await checkSystem(fakeOra())).to.be.equal(true);
   });
 
-  describe('isNightlyYarnVersion', () => {
-    it('should not match release versions', () => {
-      expect(isNightlyYarnVersion('0.10.0')).to.be.equal(false);
+  describe('validPackageManagerVersion', () => {
+    it('should consider whitelisted versions to be valid', () => {
+      expect(validPackageManagerVersion('NPM', '3.10.1', '^3.0.0', fakeOra())).to.be.equal(true);
     });
 
-    it('should not match rc/beta/alpha versions', () => {
-      expect(isNightlyYarnVersion('0.10.0-beta.1')).to.be.equal(false);
+    it('should consider Yarn nightly versions to be invalid', () => {
+      expect(validPackageManagerVersion('Yarn', '0.23.0-20170311.0515', '0.23.0', fakeOra())).to.be.equal(false);
     });
 
-    it('should match nightly versions', () => {
-      expect(isNightlyYarnVersion('0.23.0-20170311.0515')).to.be.equal(true);
+    it('should consider invalid semver versions to be invalid', () => {
+      expect(validPackageManagerVersion('Yarn', '0.22', '0.22.0', fakeOra())).to.be.equal(false);
     });
   });
 });