Skip to content

Commit

Permalink
fix: Automatic unpack detection in scoped packages unpacks the entire…
Browse files Browse the repository at this point in the history
… scope

Close #1540
  • Loading branch information
develar committed May 13, 2017
1 parent 90d0da1 commit 3558b22
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
22 changes: 13 additions & 9 deletions packages/electron-builder/src/asarUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,25 @@ export class AsarPackager {
continue
}

const nextSlashIndex = file.indexOf(path.sep, index + NODE_MODULES_PATTERN.length + 1)
let nextSlashIndex = file.indexOf(path.sep, index + NODE_MODULES_PATTERN.length + 1)
if (nextSlashIndex < 0) {
continue
}

if (file[index + NODE_MODULES_PATTERN.length] === "@") {
nextSlashIndex = file.indexOf(path.sep, nextSlashIndex + 1)
}

if (!metadata.get(file)!.isFile()) {
continue
}

const nodeModuleDir = file.substring(0, nextSlashIndex)
if (autoUnpackDirs.has(nodeModuleDir)) {
const packageDir = file.substring(0, nextSlashIndex)
if (autoUnpackDirs.has(packageDir)) {
const fileParent = path.dirname(file)
if (fileParent !== nodeModuleDir && !autoUnpackDirs.has(fileParent)) {
if (fileParent !== packageDir && !autoUnpackDirs.has(fileParent)) {
autoUnpackDirs.add(fileParent)
addValue(dirToCreate, path.relative(this.src, nodeModuleDir), path.relative(nodeModuleDir, fileParent))
addValue(dirToCreate, path.relative(this.src, packageDir), path.relative(packageDir, fileParent))
}
continue
}
Expand All @@ -175,18 +179,18 @@ export class AsarPackager {
continue
}

debug(`${path.relative(this.src, nodeModuleDir)} is not packed into asar archive - contains executable code`)
debug(`${path.relative(this.src, packageDir)} is not packed into asar archive - contains executable code`)

let fileParent = path.dirname(file)

// create parent dir to be able to copy file later without directory existence check
addValue(dirToCreate, path.relative(this.src, nodeModuleDir), path.relative(nodeModuleDir, fileParent))
addValue(dirToCreate, path.relative(this.src, packageDir), path.relative(packageDir, fileParent))

while (fileParent !== nodeModuleDir) {
while (fileParent !== packageDir) {
autoUnpackDirs.add(fileParent)
fileParent = path.dirname(fileParent)
}
autoUnpackDirs.add(nodeModuleDir)
autoUnpackDirs.add(packageDir)
}

if (dirToCreate.size > 0) {
Expand Down
38 changes: 38 additions & 0 deletions test/out/__snapshots__/BuildTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,44 @@ Object {
}
`;
exports[`smart unpack 2`] = `
Array [
"app.asar",
"electron.asar",
"app.asar.unpacked",
"app.asar.unpacked/node_modules",
"app.asar.unpacked/node_modules/edge-cs",
"app.asar.unpacked/node_modules/edge-cs/LICENSE.txt",
"app.asar.unpacked/node_modules/edge-cs/package.json",
"app.asar.unpacked/node_modules/edge-cs/tools",
"app.asar.unpacked/node_modules/edge-cs/tools/install.js",
"app.asar.unpacked/node_modules/edge-cs/src",
"app.asar.unpacked/node_modules/edge-cs/src/edge-cs",
"app.asar.unpacked/node_modules/edge-cs/src/edge-cs/EdgeCompiler.cs",
"app.asar.unpacked/node_modules/edge-cs/src/edge-cs/edge-cs.csproj",
"app.asar.unpacked/node_modules/edge-cs/src/edge-cs/edge-cs.sln",
"app.asar.unpacked/node_modules/edge-cs/src/edge-cs/Properties",
"app.asar.unpacked/node_modules/edge-cs/src/edge-cs/Properties/AssemblyInfo.cs",
"app.asar.unpacked/node_modules/edge-cs/src/Edge.js.CSharp",
"app.asar.unpacked/node_modules/edge-cs/src/Edge.js.CSharp/EdgeCompiler.cs",
"app.asar.unpacked/node_modules/edge-cs/src/Edge.js.CSharp/edge-cs-coreclr.sln",
"app.asar.unpacked/node_modules/edge-cs/src/Edge.js.CSharp/edge-cs-coreclr.xproj",
"app.asar.unpacked/node_modules/edge-cs/src/Edge.js.CSharp/gulpfile.js",
"app.asar.unpacked/node_modules/edge-cs/src/Edge.js.CSharp/package.json",
"app.asar.unpacked/node_modules/edge-cs/src/Edge.js.CSharp/project.json",
"app.asar.unpacked/node_modules/edge-cs/lib",
"app.asar.unpacked/node_modules/edge-cs/lib/edge-cs.dll",
"app.asar.unpacked/node_modules/edge-cs/lib/edge-cs.js",
"app.asar.unpacked/node_modules/edge-cs/lib/bootstrap",
"app.asar.unpacked/node_modules/edge-cs/lib/bootstrap/Dummy.cs",
"app.asar.unpacked/node_modules/edge-cs/lib/bootstrap/project.json",
"app.asar.unpacked/node_modules/@electron-builder",
"app.asar.unpacked/node_modules/@electron-builder/test-smart-unpack",
"app.asar.unpacked/node_modules/@electron-builder/test-smart-unpack/foo.dll",
"app.asar.unpacked/node_modules/@electron-builder/test-smart-unpack/package.json",
]
`;
exports[`www as default dir 1`] = `
Object {
"linux": Array [],
Expand Down
10 changes: 8 additions & 2 deletions test/src/BuildTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BluebirdPromise from "bluebird-lst"
import { Arch, BuildOptions, createTargets, DIR_TARGET, Platform } from "electron-builder"
import { walk } from "electron-builder-util/out/fs"
import { readAsarJson } from "electron-builder/out/asar"
import { normalizeOptions } from "electron-builder/out/builder"
import { createYargs } from "electron-builder/out/cli/cliOptions"
Expand Down Expand Up @@ -156,13 +157,18 @@ test.ifDevOrLinuxCi("smart unpack", app({
projectDirCreated: packageJson(it => {
it.dependencies = {
"debug": "^2.2.0",
"edge-cs": "^1.0.0"
"edge-cs": "1.2.1",
"@electron-builder/test-smart-unpack": "1.0.0",
"@electron-builder/test-smart-unpack-empty": "1.0.0",
}
}),
packed: async context => {
expect(await readAsarJson(path.join(context.getResources(Platform.LINUX), "app.asar"), "node_modules/debug/package.json")).toMatchObject({
const resourceDir = context.getResources(Platform.LINUX)
expect(await readAsarJson(path.join(resourceDir, "app.asar"), "node_modules/debug/package.json")).toMatchObject({
name: "debug"
})

expect((await walk(resourceDir, file => !path.basename(file).startsWith("."))).map(it => it.substring(resourceDir.length + 1))).toMatchSnapshot()
}
}))

Expand Down

0 comments on commit 3558b22

Please sign in to comment.