Skip to content

Commit

Permalink
feat: add bundled app validation to ensure that package.json and main…
Browse files Browse the repository at this point in the history
… entry point exist (#1257)
  • Loading branch information
malept committed Jul 12, 2021
1 parent 5aa9198 commit 452d9d6
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const debug = require('debug')('electron-packager')
const filenamify = require('filenamify')
const fs = require('fs-extra')
const metadata = require('../package.json')
const os = require('os')
const path = require('path')
Expand Down Expand Up @@ -85,6 +87,36 @@ module.exports = {
normalizePath: function normalizePath (pathToNormalize) {
return pathToNormalize.replace(/\\/g, '/')
},
/**
* Validates that the application directory contains a package.json file, and that there exists an
* appropriate main entry point file, per the rules of the "main" field in package.json.
*
* See: https://docs.npmjs.com/cli/v6/configuring-npm/package-json#main
*
* @param appDir - the directory specified by the user
* @param bundledAppDir - the directory where the appDir is copied to in the bundled Electron app
*/
validateElectronApp: async function validateElectronApp (appDir, bundledAppDir) {
debug('Validating bundled Electron app')
debug('Checking for a package.json file')

const bundledPackageJSONPath = path.join(bundledAppDir, 'package.json')
if (!(await fs.pathExists(bundledPackageJSONPath))) {
const originalPackageJSONPath = path.join(appDir, 'package.json')
throw new Error(`Application manifest was not found. Make sure "${originalPackageJSONPath}" exists and does not get ignored by your ignore option`)
}

debug('Checking for the main entry point file')
const packageJSON = await fs.readJson(bundledPackageJSONPath)
const mainScriptBasename = packageJSON.main || 'index.js'
const mainScript = path.resolve(bundledAppDir, mainScriptBasename)
if (!(await fs.pathExists(mainScript))) {
const originalMainScript = path.join(appDir, mainScriptBasename)
throw new Error(`The main entry point to your app was not found. Make sure "${originalMainScript}" exists and does not get ignored by your ignore option`)
}

debug('Validation complete')
},

hostInfo: function hostInfo () {
return `Electron Packager ${metadata.version}\n` +
Expand Down
1 change: 1 addition & 0 deletions src/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class App {

async buildApp () {
await this.copyTemplate()
await common.validateElectronApp(this.opts.dir, this.originalResourcesAppDir)
await this.asarApp()
}

Expand Down
26 changes: 26 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,29 @@ test.serial('electronZipDir: ZIP file does not exist', util.testSinglePlatform(a

await t.throwsAsync(async () => packager(opts), { message: /Electron ZIP file does not exist/ })
}))

test('validateElectronApp succeeds on a well-formed Electron app containing a main field', async t => {
await t.notThrowsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-success-with-main')))
})

test('validateElectronApp succeeds on a well-formed Electron app without a main field', async t => {
await t.notThrowsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-success-without-main')))
})

test('validateElectronApp fails on an Electron app without package.json', async t => {
await t.throwsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-failure-without-package-json')), {
message: `Application manifest was not found. Make sure "${path.join('original-dir', 'package.json')}" exists and does not get ignored by your ignore option`
})
})

test('validateElectronApp fails on an Electron app with a package.json with a main field missing main entry point', async t => {
await t.throwsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-failure-without-main-or-index')), {
message: `The main entry point to your app was not found. Make sure "${path.join('original-dir', 'index.js')}" exists and does not get ignored by your ignore option`
})
})

test('validateElectronApp fails on an Electron app with a package.json without a main field missing main entry point', async t => {
await t.throwsAsync(async () => await common.validateElectronApp('original-dir', util.fixtureSubdir('validate-failure-with-main-without-entry-point')), {
message: `The main entry point to your app was not found. Make sure "${path.join('original-dir', 'main.js')}" exists and does not get ignored by your ignore option`
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"main": "main.js"}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/validate-success-with-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"main": "main.js"}
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/validate-success-without-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 452d9d6

Please sign in to comment.