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

fix(infer): make the error message clearer when infer cannot find a package.json file #1079

Merged
merged 2 commits into from
Nov 19, 2019
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
37 changes: 25 additions & 12 deletions src/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ function errorMessageForProperty (prop) {
hash = 'electronversion'
propDescription = 'Electron version'
break
case 'version':
hash = 'appversion'
propDescription = 'application version'
break
default:
hash = ''
propDescription = '[Unknown Property]'
propDescription = `[Unknown Property (${prop})]`
}

return `Unable to determine ${propDescription}. Please specify an ${propDescription}\n\n` +
Expand Down Expand Up @@ -111,6 +115,23 @@ async function handleMetadata (opts, result) {
}
}

function handleMissingProperties (opts, err) {
const missingProps = err.missingProps.map(prop => {
return Array.isArray(prop) ? prop[0] : prop
})

if (isMissingRequiredProperty(missingProps)) {
const messages = missingProps.map(errorMessageForProperty)

debug(err.message)
err.message = messages.join('\n') + '\n'
throw err
} else {
// Missing props not required, can continue w/ partial result
return handleMetadata(opts, err.result)
}
}

module.exports = async function getMetadataFromPackageJSON (platforms, opts, dir) {
const props = []
if (!opts.name) props.push(['productName', 'name'])
Expand Down Expand Up @@ -141,19 +162,11 @@ module.exports = async function getMetadataFromPackageJSON (platforms, opts, dir
return handleMetadata(opts, result)
} catch (err) {
if (err.missingProps) {
const missingProps = err.missingProps.map(prop => {
return Array.isArray(prop) ? prop[0] : prop
})

if (isMissingRequiredProperty(missingProps)) {
const messages = missingProps.map(errorMessageForProperty)

if (err.missingProps.length === props.length) {
debug(err.message)
err.message = messages.join('\n') + '\n'
throw err
err.message = `Could not locate a package.json file in "${path.resolve(opts.dir)}" or its parent directories for an Electron app.`
} else {
// Missing props not required, can continue w/ partial result
return handleMetadata(opts, err.result)
return handleMissingProperties(opts, err)
}
}

Expand Down
1 change: 1 addition & 0 deletions test/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ test('infer win32metadata', util.testSinglePlatform(async (t, opts) => {
}))
test('do not infer win32metadata if it already exists', util.testSinglePlatform(async (t, opts) => {
opts.win32metadata = { CompanyName: 'Existing' }
opts.appVersion = '1.0.0'
const expected = { ...opts.win32metadata }

return testInferWin32metadata(t, opts, expected, 'win32metadata did not update with package.json values')
Expand Down