From 6bba0995dccc35941f169d4b4f4b636ab9d04a64 Mon Sep 17 00:00:00 2001 From: Demetris Manikas Date: Sat, 23 Apr 2016 13:28:40 +0300 Subject: [PATCH] feat: check application package --- src/metadata.ts | 2 ++ src/platformPackager.ts | 24 ++++++++++++++++++++++++ test/src/BuildTest.ts | 25 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+) 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..b36ceafae39 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,7 +157,23 @@ export abstract class PlatformPackager this.beforePack(options) await pack(options) + await this.sanityCheckPackage(appOutDir, options.asar) + } + + private async statPackageFile(appOutDir: string, packageFile: string, isAsar: boolean): Promise { + let fpath = path.resolve("/", packageFile) + + if (isAsar) { + let appPackage = path.join(appOutDir, "resources", "app.asar") + return asar.listPackage(appPackage).indexOf(fpath) !== -1 + } else { + fpath = path.join(appOutDir, "resources", "app", fpath) + let outStat = await statOrNull(appOutDir) + 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.`) @@ -163,6 +181,12 @@ export abstract class PlatformPackager else if (!outStat.isDirectory()) { throw new Error(`Output directory ${appOutDir} is not a directory. Seems like a wrong configuration.`) } + + let main = this.metadata.main || "index.js" + let 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.`) + } } protected getExtraResources(arch: string): Promise> { 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