From 2d67ace41ca128d57bb83654c2249d30b16f266b Mon Sep 17 00:00:00 2001 From: Bartosz Wojtkowiak Date: Mon, 19 Sep 2016 13:59:38 +0200 Subject: [PATCH] fix: support asar in package.json's `main` property even when not packaging to asar during the build fix the case when the app is packaged to asar before the build process in this case there might be an asar archive specified in the `main` file path even if the asar options was set to false --- src/platformPackager.ts | 27 ++++++++++++++++++----- test/fixtures/test-app/app/path/app.asar | Bin 0 -> 714 bytes test/src/BuildTest.ts | 26 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/test-app/app/path/app.asar diff --git a/src/platformPackager.ts b/src/platformPackager.ts index c1ff7516fd0..03d9eecba2c 100644 --- a/src/platformPackager.ts +++ b/src/platformPackager.ts @@ -365,12 +365,27 @@ export abstract class PlatformPackager 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 = 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.`) + } } } } diff --git a/test/fixtures/test-app/app/path/app.asar b/test/fixtures/test-app/app/path/app.asar new file mode 100644 index 0000000000000000000000000000000000000000..2967fe1b48409aac2a1670c780622d8b89fe8a1b GIT binary patch literal 714 zcmZWmu};G<5beNJssC_zaVkoh0jeq}3otSv5MyPM3pH}=V7m=a<=;8C4J33p$v!{7 zckkWVXf#?LJyUY z`Tz}`a-(xENjtYFJRUisDeNX==wXRgV*>-mXxGTXuTxki$qd#g-_|~bMUu2MbpLa# zj3}NTA0i&&O6PM97qD6t(rQs5o%#F_-`ZCyIR5v&!Ph&ZJ! 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([ @@ -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"