Skip to content

Commit 4a9af55

Browse files
demetris-manikasdevelar
authored andcommitted
feat: check asar existence and integrity (#401)
1 parent 4088b13 commit 4a9af55

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/platformPackager.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { copy } from "fs-extra-p"
99
import { statOrNull, use } from "./util"
1010
import { Packager } from "./packager"
1111
import deepAssign = require("deep-assign")
12-
import { statFile } from "asar"
12+
import { listPackage, statFile } from "asar"
1313
import ElectronPackagerOptions = ElectronPackager.ElectronPackagerOptions
1414

1515
//noinspection JSUnusedLocalSymbols
@@ -201,13 +201,16 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
201201
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
202202
}
203203

204+
private getResourcesDir(appOutDir: string): string {
205+
return this.platform === Platform.OSX ? this.getOSXResourcesDir(appOutDir) : path.join(appOutDir, "resources")
206+
}
207+
204208
private getOSXResourcesDir(appOutDir: string): string {
205209
return path.join(appOutDir, this.appName + ".app", "Contents", "Resources")
206210
}
207211

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

226-
private async sanityCheckPackage(appOutDir: string, asar: boolean): Promise<any> {
229+
private async sanityCheckAsar(asarFile: string): Promise<any> {
230+
const outStat = await statOrNull(asarFile)
231+
232+
if (outStat == null) {
233+
throw new Error(`Package file ${asarFile} was not created.`)
234+
}
235+
236+
try {
237+
listPackage(asarFile)
238+
}
239+
catch (e) {
240+
throw new Error(`Package file ${asarFile} is corrupted.`)
241+
}
242+
}
243+
244+
private async sanityCheckPackage(appOutDir: string, isAsar: boolean): Promise<any> {
227245
const outStat = await statOrNull(appOutDir)
246+
228247
if (outStat == null) {
229248
throw new Error(`Output directory ${appOutDir} does not exists. Seems like a wrong configuration.`)
230249
}
231250
else if (!outStat.isDirectory()) {
232251
throw new Error(`Output directory ${appOutDir} is not a directory. Seems like a wrong configuration.`)
233252
}
234253

254+
const resourcesDir = this.getResourcesDir(appOutDir)
255+
if (isAsar) {
256+
await this.sanityCheckAsar(path.join(resourcesDir, "app.asar"))
257+
}
258+
235259
const mainFile = this.metadata.main || "index.js"
236-
const mainFileExists = await this.statFileInPackage(appOutDir, mainFile, asar)
260+
const mainFileExists = await this.statFileInPackage(resourcesDir, mainFile, isAsar)
237261
if (!mainFileExists) {
238262
throw new Error(`Application entry file ${mainFile} could not be found in package. Seems like a wrong configuration.`)
239263
}

0 commit comments

Comments
 (0)