From ec614bfc120044c2c583f642e5e6131573b463da Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Wed, 10 Aug 2016 14:49:40 -0700 Subject: [PATCH] fix version inference regression --- NEWS.md | 1 + index.js | 54 ++++++++++++++++++++++++++++++++++------------------ package.json | 2 +- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/NEWS.md b/NEWS.md index 7ed5378c..7d4f18cc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ ### Fixed * [CLI] ensure --out has either a string or null value (#442) +* Use `get-package-info` (again) to support finding prebuilt in parent directories (#445) ## [7.5.1] - 2016-08-06 diff --git a/index.js b/index.js index cbd4b572..2189631d 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const debug = require('debug')('electron-packager') const download = require('electron-download') const extract = require('extract-zip') const fs = require('fs-extra') -const prop = require('lodash.get') +const getPackageInfo = require('get-package-info') const metadata = require('./package.json') const os = require('os') const path = require('path') @@ -49,28 +49,46 @@ function validateList (list, supported, name) { } function getNameAndVersion (opts, dir, cb) { - var pkg = require(path.join(dir, 'package.json')) + var props = [] + if (!opts.name) props.push(['productName', 'name']) + if (!opts.version) props.push(['dependencies.electron', 'devDependencies.electron']) // Name and version provided, no need to infer - if (opts.name && opts.version) return cb(null) + if (props.length === 0) return cb(null) - // Infer name from package.json - opts.name = opts.name || prop(pkg, 'productName') || prop(pkg, 'name') - - // Infer electron version from package.json - var electronVersion = prop(pkg, 'dependencies.electron') || prop(pkg, 'devDependencies.electron') - var electronPrebuiltVersion = prop(pkg, 'dependencies.electron-prebuilt') || prop(pkg, 'devDependencies.electron-prebuilt') - opts.version = opts.version || electronVersion || electronPrebuiltVersion - - if (!electronVersion && !electronPrebuiltVersion) return cb(null) + // Search package.json files to infer name and version from + getPackageInfo(props, dir, function (err, result) { + if (err) { + // `get-package-info` exploded looking for `electron`. Try `electron-prebuilt` instead + props.pop() + props.push(['dependencies.electron-prebuilt', 'devDependencies.electron-prebuilt']) + getPackageInfo(props, dir, function (err, result) { + if (err) return cb(err) + return inferNameAndVersionFromInstalled('electron-prebuilt', opts, result, cb) + }) + } else { + return inferNameAndVersionFromInstalled('electron', opts, result, cb) + } + }) +} - var packageName = electronVersion ? 'electron' : 'electron-prebuilt' - resolve(packageName, {basedir: dir}, function (err, res, pkg) { - if (err) return cb(err) - debug('Inferring target Electron version from `' + packageName + '` dependency or devDependency in package.json') - opts.version = pkg.version +function inferNameAndVersionFromInstalled (packageName, opts, result, cb) { + if (result.values.productName) { + debug('Inferring application name from productName or name in package.json') + opts.name = result.values.productName + } + if (result.values[`dependencies.${packageName}`]) { + resolve(packageName, { + basedir: path.dirname(result.source[`dependencies.${packageName}`].src) + }, function (err, res, pkg) { + if (err) return cb(err) + debug(`Inferring target Electron version from ${packageName} dependency or devDependency in package.json`) + opts.version = pkg.version + return cb(null) + }) + } else { return cb(null) - }) + } } function createSeries (opts, archs, platforms) { diff --git a/package.json b/package.json index b65fc8c5..ca45d765 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "electron-osx-sign": "^0.3.0", "extract-zip": "^1.0.3", "fs-extra": "^0.30.0", - "lodash.get": "^4.4.1", + "get-package-info": "^0.1.0", "minimist": "^1.1.1", "plist": "^1.1.0", "rcedit": "^0.5.1",