Skip to content

Commit

Permalink
feat: check application package
Browse files Browse the repository at this point in the history
PR #355
Closes #303
  • Loading branch information
demetris-manikas authored and develar committed Apr 25, 2016
1 parent 260ca0b commit 27faf73
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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
46 changes: 37 additions & 9 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,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>

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<Array<string>> {
Expand All @@ -187,7 +182,7 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
protected async copyExtraResources(appOutDir: string, arch: string): Promise<Array<string>> {
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)))
}
Expand All @@ -212,6 +207,39 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
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<any> {
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<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.`)
}

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) {
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 27faf73

Please sign in to comment.