Skip to content

Commit

Permalink
feat: prevent error "Unable to find a valid app"
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Aug 4, 2016
1 parent 753cd08 commit efba26a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
14 changes: 9 additions & 5 deletions src/asarUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,33 +384,37 @@ function writeAsarFile(filesystem: any, dest: string, toPack: Array<string>, cha
})
}

export async function checkFileInPackage(asarFile: string, relativeFile: string) {
export async function checkFileInArchive(asarFile: string, relativeFile: string, messagePrefix: string) {
function error(text: string) {
return new Error(`${messagePrefix} "${relativeFile}" in the "${asarFile}" ${text}`)
}

let stat: AsarFileInfo | null
try {
stat = statFile(asarFile, relativeFile)
}
catch (e) {
const fileStat = await statOrNull(asarFile)
if (fileStat == null) {
throw new Error(`File "${asarFile}" does not exist. Seems like a wrong configuration.`)
throw error(`does not exist. Seems like a wrong configuration.`)
}

try {
listPackage(asarFile)
}
catch (e) {
throw new Error(`File "${asarFile}" is corrupted: ${e}`)
throw error(`is corrupted: ${e}`)
}

// asar throws error on access to undefined object (info.link)
stat = null
}

if (stat == null) {
throw new Error(`Application entry file "${relativeFile}" in the "${asarFile}" does not exist. Seems like a wrong configuration.`)
throw error(`does not exist. Seems like a wrong configuration.`)
}
if (stat.size === 0) {
throw new Error(`Application entry file "${relativeFile}" in the "${asarFile}" is corrupted: size 0`)
throw error(`is corrupted: size 0`)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ElectronPackagerOptions } from "./packager/dirPackager"

export interface Metadata {
readonly repository?: string | RepositoryInfo | null

dependencies?: { [key: string]: string }
}

/*
Expand Down
15 changes: 15 additions & 0 deletions src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ export class Packager implements BuildInfo {
checkNotEmpty("description", appMetadata.description)
checkNotEmpty("version", appMetadata.version)

checkDependencies(this.devMetadata.dependencies)
if ((<any>appMetadata) !== this.devMetadata) {
checkDependencies(appMetadata.dependencies)

if ((<any>appMetadata).build != null) {
throw new Error(util.format(errorMessages.buildInAppSpecified, appPackageFile, devAppPackageFile))
}
Expand Down Expand Up @@ -282,4 +285,16 @@ async function checkWineVersion(checkPromise: Promise<string>) {
if (semver.lt(wineVersion, "1.8.0")) {
throw new Error(wineError(`wine 1.8+ is required, but your version is ${wineVersion}`))
}
}

function checkDependencies(dependencies?: { [key: string]: string }) {
if (dependencies == null) {
return
}

for (let name of ["electron", "electron-prebuilt", "electron-builder"]) {
if (name in dependencies) {
throw new Error(`${name} must be in the devDependencies`)
}
}
}
16 changes: 8 additions & 8 deletions src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Packager } from "./packager"
import { AsarOptions } from "asar-electron-builder"
import { archiveApp } from "./targets/archive"
import { Minimatch } from "minimatch"
import { checkFileInPackage, createAsarArchive } from "./asarUtil"
import { checkFileInArchive, createAsarArchive } from "./asarUtil"
import { deepAssign } from "./util/deepAssign"
import { warn, log, task } from "./util/log"
import { AppInfo } from "./appInfo"
Expand Down Expand Up @@ -362,34 +362,34 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
return path.join(appOutDir, `${this.appInfo.productFilename}.app`, "Contents", "Resources")
}

private async checkFileInPackage(resourcesDir: string, file: string, isAsar: boolean) {
private async checkFileInPackage(resourcesDir: string, file: string, messagePrefix: string, isAsar: boolean) {
const relativeFile = path.relative(this.info.appDir, path.resolve(this.info.appDir, file))
if (isAsar) {
await checkFileInPackage(path.join(resourcesDir, "app.asar"), relativeFile)
await checkFileInArchive(path.join(resourcesDir, "app.asar"), relativeFile, messagePrefix)
}
else {
const outStat = await statOrNull(path.join(resourcesDir, "app", relativeFile))
if (outStat == null) {
throw new Error(`Application entry file "${relativeFile}" does not exist. Seems like a wrong configuration.`)
throw new Error(`${messagePrefix} "${relativeFile}" does not exist. Seems like a wrong configuration.`)
}
else if (!outStat.isFile()) {
throw new Error(`Application entry file "${relativeFile}" is not a file. Seems like a wrong configuration.`)
throw new Error(`${messagePrefix} "${relativeFile}" is not a file. Seems like a wrong configuration.`)
}
}
}

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 exist. 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 mainFile = this.appInfo.metadata.main || "index.js"
await this.checkFileInPackage(this.getResourcesDir(appOutDir), mainFile, isAsar)
const resourcesDir = this.getResourcesDir(appOutDir)
await this.checkFileInPackage(resourcesDir, this.appInfo.metadata.main || "index.js", "Application entry file", isAsar)
await this.checkFileInPackage(resourcesDir, "package.json", "Application", isAsar)
}

protected async archiveApp(format: string, appOutDir: string, outFile: string): Promise<any> {
Expand Down
5 changes: 4 additions & 1 deletion src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ export async function computeDefaultAppDirectory(projectDir: string, userAppDir:
else if (!stat.isDirectory()) {
throw new Error(`Application directory ${userAppDir} is not a directory`)
}
else if (projectDir === absolutePath) {
warn(`Specified application directory "${userAppDir}" equals to project dir — superfluous or wrong configuration`)
}
return absolutePath
}

Expand Down Expand Up @@ -238,7 +241,7 @@ let tmpDirCounter = 0
const tempDirPrefix = `${process.pid.toString(36)}-${Date.now().toString(36)}`

export function getTempName(prefix?: string | n): string {
return `${prefix == null ? "" : prefix + "-"}${tempDirPrefix}-${(tmpDirCounter++).toString(36)}`
return `${prefix == null ? "" : `${prefix}-`}${tempDirPrefix}-${(tmpDirCounter++).toString(36)}`
}

export function isEmptyOrSpaces(s: string | n) {
Expand Down

0 comments on commit efba26a

Please sign in to comment.