-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Update get-package-info #505
Changes from 7 commits
be3385d
e5f10ee
8f9427e
d735ad2
72ab0e7
a6a4616
2ad8aae
7853847
96bddad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,50 +23,84 @@ function getMetadata (opts, dir, cb) { | |
var props = [] | ||
if (!opts.name) props.push(['productName', 'name']) | ||
if (!opts['app-version']) props.push('version') | ||
if (!opts.version) props.push(['dependencies.electron', 'devDependencies.electron']) | ||
if (!opts.version) { | ||
props.push([ | ||
'dependencies.electron', | ||
'devDependencies.electron', | ||
'dependencies.electron-prebuilt', | ||
'devDependencies.electron-prebuilt' | ||
]) | ||
} | ||
|
||
// Name and version provided, no need to infer | ||
if (props.length === 0) 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) | ||
getPackageInfo(props, dir, (err, result) => { | ||
if (err && err.missingProps) { | ||
let requiredProps = ['productName', 'dependencies.electron'] | ||
let missingProps = err.missingProps.map(prop => { | ||
return Array.isArray(prop) ? prop[0] : prop | ||
}) | ||
} else { | ||
return inferNameAndVersionFromInstalled('electron', opts, result, cb) | ||
|
||
// Callback w/ error if there are required props that are missing | ||
if (missingProps.filter(prop => requiredProps.find(reqProp => prop === reqProp)).length !== 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd extract the conditional into a method for readability (and also lets you get rid of the comment. |
||
let messages = missingProps.map(missingProp => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps extract the map function into a top-level function named |
||
let hash, propName | ||
if (missingProp === 'productName') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you need to add a test for this conditional. |
||
hash = 'name' | ||
propName = 'application name' | ||
} | ||
|
||
if (missingProp === 'dependencies.electron') { | ||
hash = 'version' | ||
propName = 'Electron version' | ||
} | ||
|
||
return `Unable to determine ${propName}. Please specify an ${propName}\n\n` + | ||
'For more information, please see\n' + | ||
`https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#${hash}\n` | ||
}) | ||
|
||
debug(err.message) | ||
err.message = messages.join('\n') + '\n' | ||
return cb(err) | ||
} else { | ||
// Missing props not required, can continue w/ partial result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
result = err.result | ||
} | ||
} else if (err) { | ||
return cb(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would an IO error include an invalid |
||
} | ||
}) | ||
} | ||
|
||
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.productName) { | ||
debug(`Inferring application name from ${result.source.productName.prop} in ${result.source.productName.src}`) | ||
opts.name = result.values.productName | ||
} | ||
|
||
if (result.values.version) { | ||
debug('Inferring app-version from version in package.json') | ||
opts['app-version'] = result.values.version | ||
} | ||
if (result.values.version) { | ||
debug(`Inferring app-version from version in ${result.source.version.src}`) | ||
opts['app-version'] = result.values.version | ||
} | ||
|
||
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 | ||
if (result.values['dependencies.electron']) { | ||
let {prop, src} = result.source['dependencies.electron'] | ||
return getVersion(opts, prop.split('.')[1], src, cb) | ||
} else { | ||
return cb(null) | ||
}) | ||
} else { | ||
} | ||
}) | ||
} | ||
|
||
function getVersion (opts, packageName, src, cb) { | ||
resolve(packageName, { | ||
basedir: path.dirname(src) | ||
}, (err, res, pkg) => { | ||
if (err) return cb(err) | ||
debug(`Inferring target Electron version from ${packageName} in ${src}`) | ||
opts.version = pkg.version | ||
return cb(null) | ||
} | ||
}) | ||
} | ||
|
||
function createSeries (opts, archs, platforms) { | ||
|
@@ -198,15 +232,7 @@ module.exports = function packager (opts, cb) { | |
debug(`Target Architectures: ${archs.join(', ')}`) | ||
|
||
getMetadata(opts, path.resolve(process.cwd(), opts.dir) || process.cwd(), function (err) { | ||
if (err) { | ||
err.message = 'Unable to determine application name or Electron version. ' + | ||
'Please specify an application name and Electron version.\n\n' + | ||
'For more infomation, please see \n' + | ||
'https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#name or \n' + | ||
'https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#version\n\n' + | ||
err.message | ||
return cb(err) | ||
} | ||
if (err) return cb(err) | ||
|
||
debug(`Application name: ${opts.name}`) | ||
debug(`Target Electron version: ${opts.version}`) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,13 @@ | |
const common = require('../common') | ||
const config = require('./config.json') | ||
const fs = require('fs-extra') | ||
const os = require('os') | ||
const packager = require('..') | ||
const path = require('path') | ||
const test = require('tape') | ||
const util = require('./util') | ||
const waterfall = require('run-waterfall') | ||
const pkgUp = require('pkg-up') | ||
|
||
// Generates a path to the generated app that reflects the name given in the options. | ||
// Returns the Helper.app location on darwin since the top-level .app is already tested for the | ||
|
@@ -265,16 +267,57 @@ function createInferElectronTest (opts) { | |
} | ||
} | ||
|
||
function copyFixtureToTempDir (fixtureSubdir, cb) { | ||
let tmpdir = path.join(os.tmpdir(), fixtureSubdir) | ||
let fixtureDir = path.join(__dirname, 'fixtures', fixtureSubdir) | ||
waterfall([ | ||
cb => { | ||
let tmpdirPkg = pkgUp.sync(path.join(tmpdir, '..')) | ||
if (tmpdirPkg) return cb(new Error(`Found package.json in parent of temp directory, which will interfere with test results. Please remove package.json at ${tmpdirPkg}`)) | ||
cb() | ||
}, | ||
cb => fs.emptyDir(tmpdir, cb), | ||
(cb1, cb2) => fs.copy(fixtureDir, tmpdir, cb2 || cb1), // inconsistent cb arguments from fs.emptyDir | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weird b/c fs.emptyDir doesn't return a consistent # of arguments, depending on whether the directory already exists or not. I've made an issue on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For reference: jprichardson/node-fs-extra#287 |
||
cb => cb(null, tmpdir) | ||
], cb) | ||
} | ||
|
||
function createInferFailureTest (opts, fixtureSubdir) { | ||
return function (t) { | ||
t.timeoutAfter(config.timeout) | ||
|
||
delete opts.version | ||
opts.dir = path.join(__dirname, 'fixtures', fixtureSubdir) | ||
copyFixtureToTempDir(fixtureSubdir, (err, dir) => { | ||
if (err) return t.end(err) | ||
|
||
packager(opts, function (err, paths) { | ||
t.ok(err, 'error thrown') | ||
t.end() | ||
delete opts.version | ||
opts.dir = dir | ||
|
||
packager(opts, function (err, paths) { | ||
t.ok(err, 'error thrown') | ||
t.end() | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
function createInferMissingVersionTest (opts) { | ||
return (t) => { | ||
t.timeoutAfter(config.timeout) | ||
copyFixtureToTempDir('infer-missing-version-only', (err, dir) => { | ||
if (err) return t.end(err) | ||
|
||
delete opts.version | ||
opts.dir = dir | ||
let packageJSON = require(path.join(opts.dir, 'package.json')) | ||
|
||
packager(opts, (err, paths) => { | ||
if (!err) { | ||
var version = fs.readFileSync(path.join(paths[0], 'version'), 'utf8') | ||
t.equal(`v${packageJSON.devDependencies['electron']}`, version.toString(), 'The version should be inferred from installed `electron` version') | ||
} | ||
|
||
t.end(err) | ||
}) | ||
}) | ||
} | ||
} | ||
|
@@ -287,6 +330,10 @@ function createInferWithBadFieldsTest (opts) { | |
return createInferFailureTest(opts, 'infer-bad-fields') | ||
} | ||
|
||
function createInferWithMalformedJSONTest (opts) { | ||
return createInferFailureTest(opts, 'infer-malformed-json') | ||
} | ||
|
||
function createTmpdirTest (opts) { | ||
return function (t) { | ||
t.timeoutAfter(config.timeout) | ||
|
@@ -406,6 +453,8 @@ util.testSinglePlatform('infer test using `electron-prebuilt` package', createIn | |
util.testSinglePlatform('infer test using `electron` package', createInferElectronTest) | ||
util.testSinglePlatform('infer missing fields test', createInferMissingFieldsTest) | ||
util.testSinglePlatform('infer with bad fields test', createInferWithBadFieldsTest) | ||
util.testSinglePlatform('infer with malformed JSON test', createInferWithMalformedJSONTest) | ||
util.testSinglePlatform('infer with missing version only test', createInferMissingVersionTest) | ||
util.testSinglePlatform('defaults test', createDefaultsTest) | ||
util.testSinglePlatform('out test', createOutTest) | ||
util.testSinglePlatform('prune test', (baseOpts) => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
"productName": "InferMalformedJSON", |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"main": "main.js", | ||
"productName": "MainJS", | ||
"devDependencies": { | ||
"electron": "1.3.1" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function has gotten rather large. Can it be split up?