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

fix: support asar in package.json's main property even when not kaging to asar during the build #759

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 21 additions & 6 deletions src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,27 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
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(`${messagePrefix} "${relativeFile}" does not exist. Seems like a wrong configuration.`)
}
else if (!outStat.isFile()) {
throw new Error(`${messagePrefix} "${relativeFile}" is not a file. Seems like a wrong configuration.`)
const pathParsed = path.parse(file)
// Even when packaging to asar is disabled, it does not imply that the main file can not be inside an .asar archive.
// This may occur when the packaging is done manually before processing with electron-builder.
if (pathParsed.dir.includes(".asar")) {
// The path needs to be split to the part with an asar archive which acts like a directory and the part with
// the path to main file itself. (e.g. path/arch.asar/dir/index.js -> path/arch.asar, dir/index.js)
const pathSplit: Array<string> = pathParsed.dir.split(path.sep)
let partWithAsarIndex = 0
pathSplit.some((pathPart: string, index: number) => (partWithAsarIndex = index, pathPart.endsWith(".asar")))
const asarPath = path.join.apply(path, pathSplit.slice(0, partWithAsarIndex + 1))
let mainPath = (pathSplit.length > partWithAsarIndex + 1) ? path.join.apply(pathSplit.slice(partWithAsarIndex + 1)) : ""
mainPath += path.join(mainPath, pathParsed.base)
await checkFileInArchive(path.join(resourcesDir, "app", asarPath), mainPath, messagePrefix)
} else {
const outStat = await statOrNull(path.join(resourcesDir, "app", relativeFile))
if (outStat == null) {
throw new Error(`${messagePrefix} "${relativeFile}" does not exist. Seems like a wrong configuration.`)
}
else if (!outStat.isFile()) {
throw new Error(`${messagePrefix} "${relativeFile}" is not a file. Seems like a wrong configuration.`)
}
}
}
}
Expand Down
Binary file added test/fixtures/test-app/app/path/app.asar
Binary file not shown.
26 changes: 26 additions & 0 deletions test/src/BuildTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ test("invalid main in the app package.json (no asar)", t => t.throws(assertPack(
}
}), `Application entry file "main.js" does not exist. Seems like a wrong configuration.`))

test("invalid main in the app package.json (custom asar)", t => t.throws(assertPack("test-app", allPlatforms(false), {
projectDirCreated: projectDir => {
return BluebirdPromise.all([
modifyPackageJson(projectDir, data => {
data.main = "path/app.asar/main.js"
}, true),
modifyPackageJson(projectDir, data => {
data.build.asar = false
})
])
}
}), /Application entry file "main.js" in the ("[^"]*") does not exist\. Seems like a wrong configuration\./))

test("main in the app package.json (no asar)", () => assertPack("test-app", allPlatforms(false), {
projectDirCreated: projectDir => {
return BluebirdPromise.all([
Expand All @@ -140,6 +153,19 @@ test("main in the app package.json (no asar)", () => assertPack("test-app", allP
}
}))

test("main in the app package.json (custom asar)", () => assertPack("test-app", allPlatforms(false), {
projectDirCreated: projectDir => {
return BluebirdPromise.all([
modifyPackageJson(projectDir, data => {
data.main = "path/app.asar/index.js"
}, true),
modifyPackageJson(projectDir, data => {
data.build.asar = false
})
])
}
}))

test("relative index", () => assertPack("test-app", allPlatforms(false), {
projectDirCreated: projectDir => modifyPackageJson(projectDir, data => {
data.main = "./index.js"
Expand Down