diff --git a/package.json b/package.json index 6898d9aa0cc..caa8fbfcbec 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "homepage": "https://github.com/electron-userland/electron-builder", "dependencies": { "7zip-bin": "^0.0.4", + "asar": "^0.11.0", "bluebird": "^3.3.5", "chalk": "^1.1.3", "command-line-args": "^2.1.6", diff --git a/src/metadata.ts b/src/metadata.ts index 5f90454249a..d51b5257487 100755 --- a/src/metadata.ts +++ b/src/metadata.ts @@ -24,6 +24,8 @@ export interface AppMetadata extends Metadata { */ readonly description: string + readonly main?: string + readonly author: AuthorMetadata /* diff --git a/src/platformPackager.ts b/src/platformPackager.ts index 5dfebfa0143..84906279594 100644 --- a/src/platformPackager.ts +++ b/src/platformPackager.ts @@ -15,6 +15,8 @@ const __awaiter = require("./awaiter") const pack = BluebirdPromise.promisify(packager) +const asar = require("asar") + export interface PackagerOptions { arch?: string @@ -155,14 +157,7 @@ export abstract class PlatformPackager this.beforePack(options) await pack(options) - - const outStat = await statOrNull(appOutDir) - if (outStat == null) { - throw new Error(`Output directory ${appOutDir} does not exists. Seems like a wrong configuration.`) - } - else if (!outStat.isDirectory()) { - throw new Error(`Output directory ${appOutDir} is not a directory. Seems like a wrong configuration.`) - } + await this.sanityCheckPackage(appOutDir, options.asar) } protected getExtraResources(arch: string): Promise> { @@ -187,7 +182,7 @@ export abstract class PlatformPackager protected async copyExtraResources(appOutDir: string, arch: string): Promise> { let resourcesDir = appOutDir if (this.platform === Platform.OSX) { - resourcesDir = path.join(resourcesDir, this.appName + ".app", "Contents", "Resources") + resourcesDir = this.getOSXResourcesDir(appOutDir) } return await BluebirdPromise.map(await this.getExtraResources(arch), it => copy(path.join(this.projectDir, it), path.join(resourcesDir, it))) } @@ -212,6 +207,39 @@ export abstract class PlatformPackager protected computeBuildNumber(): string { return this.devMetadata.build["build-version"] || process.env.TRAVIS_BUILD_NUMBER || process.env.APPVEYOR_BUILD_NUMBER || process.env.CIRCLE_BUILD_NUM } + + private getOSXResourcesDir(appOutDir: string): string { + return path.join(appOutDir, this.appName + ".app", "Contents", "Resources") + } + + private async statPackageFile(appOutDir: string, packageFile: string, isAsar: boolean): Promise { + const fpath = path.resolve("/", packageFile) + const resourcesDir = this.platform === Platform.OSX ? this.getOSXResourcesDir(appOutDir) : path.join(appOutDir, "resources") + if (isAsar) { + const appPackage = path.join(resourcesDir, "app.asar") + return asar.listPackage(appPackage).indexOf(fpath) !== -1 + } else { + const outStat = await statOrNull(path.join(resourcesDir, fpath)) + return outStat != null && outStat.isFile() + } + } + + private async sanityCheckPackage(appOutDir: string, asar: boolean): Promise { + const outStat = await statOrNull(appOutDir) + if (outStat == null) { + throw new Error(`Output directory ${appOutDir} does not exists. Seems like a wrong configuration.`) + } + else if (!outStat.isDirectory()) { + throw new Error(`Output directory ${appOutDir} is not a directory. Seems like a wrong configuration.`) + } + + const main = this.metadata.main || "index.js" + const mainExists = await this.statPackageFile(appOutDir, main, asar) + if (!mainExists) { + throw new Error(`Application entry file ` + main + ` could not be found in package. Seems like a wrong configuration.`) + } + } + } function checkConflictingOptions(options: any) { diff --git a/test/src/BuildTest.ts b/test/src/BuildTest.ts index 84153cf946d..4fa46107893 100755 --- a/test/src/BuildTest.ts +++ b/test/src/BuildTest.ts @@ -45,6 +45,31 @@ test("build in the app package.json", t => t.throws(assertPack("test-app", allPl }, true) }), /'build' in the application package\.json .+/)) +test("invalid main in the app package.json", t => t.throws(assertPack("test-app", { + platform: [Platform.fromString(process.platform)], + dist: true +}, { + tempDirCreated: projectDir => modifyPackageJson(projectDir, data => { + data.main = "main.js" + }, true) +}), /Application entry file main.js could not be found in package+/)) + +test("invalid main in the app package.json (no asar)", t => t.throws(assertPack("test-app", { + platform: [Platform.fromString(process.platform)], + dist: true +}, { + tempDirCreated: projectDir => { + return BluebirdPromise.all([ + modifyPackageJson(projectDir, data => { + data.main = "main.js" + }, true), + modifyPackageJson(projectDir, data => { + data.build.asar = false + }) + ]) + } +}), /Application entry file main.js could not be found in package+/)) + test("version from electron-prebuilt dependency", () => assertPack("test-app-one", { platform: [Platform.fromString(process.platform)], dist: false