Adds JSON parse, invalid and missing main field errors
This commit is contained in:
parent
b08393a663
commit
58d9d2cf41
1 changed files with 35 additions and 16 deletions
|
@ -234,7 +234,13 @@ function loadApplicationPackage (packagePath) {
|
||||||
packagePath = path.resolve(packagePath)
|
packagePath = path.resolve(packagePath)
|
||||||
const packageJsonPath = path.join(packagePath, 'package.json')
|
const packageJsonPath = path.join(packagePath, 'package.json')
|
||||||
if (fs.existsSync(packageJsonPath)) {
|
if (fs.existsSync(packageJsonPath)) {
|
||||||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
|
let packageJson
|
||||||
|
try {
|
||||||
|
packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
|
||||||
|
} catch (e) {
|
||||||
|
showErrorMessage('Unable to parse package.json, it contains errors.\n\n' +
|
||||||
|
`${e.toString()} in ${packageJsonPath}`)
|
||||||
|
}
|
||||||
if (packageJson.version) app.setVersion(packageJson.version)
|
if (packageJson.version) app.setVersion(packageJson.version)
|
||||||
|
|
||||||
if (packageJson.productName) {
|
if (packageJson.productName) {
|
||||||
|
@ -242,21 +248,39 @@ function loadApplicationPackage (packagePath) {
|
||||||
} else if (packageJson.name) {
|
} else if (packageJson.name) {
|
||||||
app.setName(packageJson.name)
|
app.setName(packageJson.name)
|
||||||
}
|
}
|
||||||
|
if (!packageJson.main) {
|
||||||
|
showErrorMessage('App is missing \'main\' property in package.json.\n\n' +
|
||||||
|
`See: ${packageJsonPath}`)
|
||||||
|
}
|
||||||
app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
|
app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
|
||||||
app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
|
app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
|
||||||
app.setAppPath(packagePath)
|
app.setAppPath(packagePath)
|
||||||
}
|
}
|
||||||
|
const Module = require('module')
|
||||||
|
try {
|
||||||
|
Module._resolveFilename(packagePath, module, true)
|
||||||
|
} catch (e) {
|
||||||
|
showErrorMessage('Unable to find Electron app.\n\n' +
|
||||||
|
`See: ${packagePath}`)
|
||||||
|
}
|
||||||
// Run the app.
|
// Run the app.
|
||||||
require('module')._load(packagePath, module, true)
|
Module._load(packagePath, module, true)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code === 'MODULE_NOT_FOUND') {
|
if (e.code === 'MODULE_NOT_FOUND') {
|
||||||
|
showErrorMessage('Unable to open Electron app.\n\n' +
|
||||||
|
`${e.toString()}`)
|
||||||
|
} else {
|
||||||
|
console.error('App threw an error when running', e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function showErrorMessage (message) {
|
||||||
app.focus()
|
app.focus()
|
||||||
dialog.showMessageBox({
|
dialog.showMessageBox({
|
||||||
message: 'Error opening app',
|
message: 'Error opening app',
|
||||||
detail: 'Unable to open or find an Electron app.\n\n' +
|
detail: message,
|
||||||
`${e.toString()}`,
|
|
||||||
buttons: ['OK', 'Learn More']
|
buttons: ['OK', 'Learn More']
|
||||||
}, (response) => {
|
}, (response) => {
|
||||||
if (response === 1) {
|
if (response === 1) {
|
||||||
|
@ -264,11 +288,6 @@ function loadApplicationPackage (packagePath) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
} else {
|
|
||||||
console.error('App threw an error when running', e)
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadApplicationByUrl (appUrl) {
|
function loadApplicationByUrl (appUrl) {
|
||||||
|
|
Loading…
Reference in a new issue