Skip to content

Commit

Permalink
feat: check application package
Browse files Browse the repository at this point in the history
  • Loading branch information
demetris-manikas committed Apr 25, 2016
1 parent 260ca0b commit 6bba099
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface AppMetadata extends Metadata {
*/
readonly description: string

readonly main?: string

readonly author: AuthorMetadata

/*
Expand Down
24 changes: 24 additions & 0 deletions src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const __awaiter = require("./awaiter")

const pack = BluebirdPromise.promisify(packager)

const asar = require("asar")

export interface PackagerOptions {
arch?: string

Expand Down Expand Up @@ -155,14 +157,36 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>

this.beforePack(options)
await pack(options)
await this.sanityCheckPackage(appOutDir, options.asar)
}

private async statPackageFile(appOutDir: string, packageFile: string, isAsar: boolean): Promise<any> {
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<any> {
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.`)
}

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<Array<string>> {
Expand Down
25 changes: 25 additions & 0 deletions test/src/BuildTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6bba099

Please sign in to comment.