Skip to content

Commit

Permalink
feat: check asar existence and integrity (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
demetris-manikas authored and develar committed May 18, 2016
1 parent 4088b13 commit 4a9af55
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { copy } from "fs-extra-p"
import { statOrNull, use } from "./util"
import { Packager } from "./packager"
import deepAssign = require("deep-assign")
import { statFile } from "asar"
import { listPackage, statFile } from "asar"
import ElectronPackagerOptions = ElectronPackager.ElectronPackagerOptions

//noinspection JSUnusedLocalSymbols
Expand Down Expand Up @@ -201,13 +201,16 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
return this.devMetadata.build["build-version"] || process.env.TRAVIS_BUILD_NUMBER || process.env.APPVEYOR_BUILD_NUMBER || process.env.CIRCLE_BUILD_NUM || process.env.BUILD_NUMBER
}

private getResourcesDir(appOutDir: string): string {
return this.platform === Platform.OSX ? this.getOSXResourcesDir(appOutDir) : path.join(appOutDir, "resources")
}

private getOSXResourcesDir(appOutDir: string): string {
return path.join(appOutDir, this.appName + ".app", "Contents", "Resources")
}

private async statFileInPackage(appOutDir: string, packageFile: string, isAsar: boolean): Promise<any> {
private async statFileInPackage(resourcesDir: string, packageFile: string, isAsar: boolean): Promise<any> {
const relativeFile = path.relative(this.info.appDir, path.resolve(this.info.appDir, packageFile))
const resourcesDir = this.platform === Platform.OSX ? this.getOSXResourcesDir(appOutDir) : path.join(appOutDir, "resources")
if (isAsar) {
try {
return statFile(path.join(resourcesDir, "app.asar"), relativeFile) != null
Expand All @@ -223,17 +226,38 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
}
}

private async sanityCheckPackage(appOutDir: string, asar: boolean): Promise<any> {
private async sanityCheckAsar(asarFile: string): Promise<any> {
const outStat = await statOrNull(asarFile)

if (outStat == null) {
throw new Error(`Package file ${asarFile} was not created.`)
}

try {
listPackage(asarFile)
}
catch (e) {
throw new Error(`Package file ${asarFile} is corrupted.`)
}
}

private async sanityCheckPackage(appOutDir: string, isAsar: 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 resourcesDir = this.getResourcesDir(appOutDir)
if (isAsar) {
await this.sanityCheckAsar(path.join(resourcesDir, "app.asar"))
}

const mainFile = this.metadata.main || "index.js"
const mainFileExists = await this.statFileInPackage(appOutDir, mainFile, asar)
const mainFileExists = await this.statFileInPackage(resourcesDir, mainFile, isAsar)
if (!mainFileExists) {
throw new Error(`Application entry file ${mainFile} could not be found in package. Seems like a wrong configuration.`)
}
Expand Down

0 comments on commit 4a9af55

Please sign in to comment.