Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check application package #355

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not add such option. User should use standard package.json main. And, so, our check should check such field in the package.json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood correctly interface AppMetadata holds all "useful" properties of the application package.json for the sake ot typesafety. I thought I only gave access to main (an already existent option). Please correct me if I am wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you are right.


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