From 3c857ba6e4bf4be7c1c3ce6ed20e9cfd62228d7a Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Fri, 22 Mar 2024 19:11:21 +0800 Subject: [PATCH 01/12] update app-builder-bin-5.0 --- packages/builder-util/package.json | 2 +- pnpm-lock.yaml | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/builder-util/package.json b/packages/builder-util/package.json index 8d05726481..705aaf3c53 100644 --- a/packages/builder-util/package.json +++ b/packages/builder-util/package.json @@ -17,7 +17,7 @@ "dependencies": { "7zip-bin": "~5.2.0", "@types/debug": "^4.1.6", - "app-builder-bin": "4.0.0", + "app-builder-bin": "v5.0.0-alpha.2", "bluebird-lst": "^1.0.9", "builder-util-runtime": "workspace:*", "chalk": "^4.1.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2a9cf71c4b..c38e395dc1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -280,8 +280,8 @@ importers: specifier: ^4.1.6 version: 4.1.7 app-builder-bin: - specifier: 4.0.0 - version: 4.0.0 + specifier: v5.0.0-alpha.2 + version: 5.0.0-alpha.2 bluebird-lst: specifier: ^1.0.9 version: 1.0.9 @@ -3570,8 +3570,8 @@ packages: normalize-path: 3.0.0 picomatch: 2.3.1 - /app-builder-bin@4.0.0: - resolution: {integrity: sha512-xwdG0FJPQMe0M0UA4Tz0zEB8rBJTRA5a476ZawAqiBkMv16GRK5xpXThOjMaEOFnZ6zabejjG4J3da0SXG63KA==} + /app-builder-bin@5.0.0-alpha.2: + resolution: {integrity: sha512-0k5iUDX1LipeTtt9OLhxyB6In22fRPA09lhLfx2eBqTffalEkbDwaSEGbm5h6xkyi5A86jOQbZ9uOtL5BWxMIA==} dev: false /aproba@2.0.0: From ce3e7c3fd8be87b5194a361111bb057f14618bdb Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Fri, 22 Mar 2024 20:04:12 +0800 Subject: [PATCH 02/12] fix pnpm modules --- .../app-builder-lib/src/util/appFileCopier.ts | 24 ++++- test/snapshots/mac/macPackagerTest.js.snap | 99 +++++++++++++++++++ test/src/helpers/verifySmartUnpack.ts | 1 + test/src/mac/macPackagerTest.ts | 4 +- 4 files changed, 123 insertions(+), 5 deletions(-) diff --git a/packages/app-builder-lib/src/util/appFileCopier.ts b/packages/app-builder-lib/src/util/appFileCopier.ts index 16767aad98..e3f3b70bb0 100644 --- a/packages/app-builder-lib/src/util/appFileCopier.ts +++ b/packages/app-builder-lib/src/util/appFileCopier.ts @@ -18,12 +18,30 @@ const BOWER_COMPONENTS_PATTERN = `${path.sep}bower_components${path.sep}` /** @internal */ export const ELECTRON_COMPILE_SHIM_FILENAME = "__shim.js" -export function getDestinationPath(file: string, fileSet: ResolvedFileSet) { - if (file === fileSet.src) { +function removePnpmAndNextTwoFolders(file: string) { + // Split the path into parts + const parts = file.split(path.sep) + + // Find the index of the '.pnpm' folder + const pnpmIndex = parts.findIndex(part => part === ".pnpm") + + // If '.pnpm' is found, and there are at least two more folders after it + if (pnpmIndex >= 0 && parts.length > pnpmIndex + 2) { + // Remove '.pnpm' and the next two folders from the parts array + parts.splice(pnpmIndex, 3) + } + + // Rejoin the remaining parts back into a path string + return parts.join(path.sep) +} + +export function getDestinationPath(ttt: string, fileSet: ResolvedFileSet) { + if (ttt === fileSet.src) { return fileSet.destination } else { - const src = fileSet.src + const src = removePnpmAndNextTwoFolders(fileSet.src) const dest = fileSet.destination + const file = removePnpmAndNextTwoFolders(ttt) if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { return dest + file.substring(src.length) } else { diff --git a/test/snapshots/mac/macPackagerTest.js.snap b/test/snapshots/mac/macPackagerTest.js.snap index 58a26252cc..dcbe6ea5eb 100644 --- a/test/snapshots/mac/macPackagerTest.js.snap +++ b/test/snapshots/mac/macPackagerTest.js.snap @@ -501,6 +501,19 @@ Object { }, "node_modules": Object { "files": Object { + "bindings": Object { + "files": Object { + "LICENSE.md": Object { + "size": "", + }, + "bindings.js": Object { + "size": "", + }, + "package.json": Object { + "size": "", + }, + }, + }, "debug": Object { "files": Object { "LICENSE": Object { @@ -534,6 +547,92 @@ Object { }, }, }, + "file-uri-to-path": Object { + "files": Object { + "History.md": Object { + "size": "", + }, + "LICENSE": Object { + "size": "", + }, + "index.js": Object { + "size": "", + }, + "package.json": Object { + "size": "", + }, + }, + }, + "ms": Object { + "files": Object { + "index.js": Object { + "size": "", + }, + "license.md": Object { + "size": "", + }, + "package.json": Object { + "size": "", + }, + }, + }, + "node-addon-api": Object { + "files": Object { + "LICENSE.md": Object { + "size": "", + }, + "common.gypi": Object { + "size": "", + }, + "except.gypi": Object { + "size": "", + }, + "index.js": Object { + "size": "", + }, + "napi-inl.deprecated.h": Object { + "size": "", + }, + "napi-inl.h": Object { + "size": "", + }, + "napi.h": Object { + "size": "", + }, + "node_api.gyp": Object { + "size": "", + }, + "noexcept.gypi": Object { + "size": "", + }, + "nothing.c": Object { + "size": "", + }, + "package-support.json": Object { + "size": "", + }, + "package.json": Object { + "size": "", + }, + "tools": Object { + "files": Object { + "README.md": Object { + "size": "", + }, + "check-napi.js": Object { + "size": "", + }, + "clang-format.js": Object { + "size": "", + }, + "conversion.js": Object { + "executable": true, + "size": "", + }, + }, + }, + }, + }, "node-mac-permissions": Object { "files": Object { ".prettierrc": Object { diff --git a/test/src/helpers/verifySmartUnpack.ts b/test/src/helpers/verifySmartUnpack.ts index 7069e78e1c..d5b746c0c2 100644 --- a/test/src/helpers/verifySmartUnpack.ts +++ b/test/src/helpers/verifySmartUnpack.ts @@ -31,6 +31,7 @@ export async function verifySmartUnpack(resourceDir: string, additionalVerificat if (additionalVerifications) { await additionalVerifications(asarFs) } + console.log(JSON.stringify(asarFs.header)) expect(removeUnstableProperties(asarFs.header)).toMatchSnapshot() const files = (await walk(resourceDir, file => !path.basename(file).startsWith(".") && !file.endsWith(`resources${path.sep}inspector`))).map(it => { diff --git a/test/src/mac/macPackagerTest.ts b/test/src/mac/macPackagerTest.ts index 503aeed881..ae601b25a9 100644 --- a/test/src/mac/macPackagerTest.ts +++ b/test/src/mac/macPackagerTest.ts @@ -107,7 +107,7 @@ test.ifMac("yarn two package.json w/ native module", () => assertPack( "test-app-two-native-modules", { - targets: Platform.MAC.createTarget("zip", Arch.universal), + targets: Platform.MAC.createTarget("zip", Arch.x64), config: { npmRebuild: true, nativeRebuilder: "sequential", @@ -115,7 +115,7 @@ test.ifMac("yarn two package.json w/ native module", () => }, { signed: false, - packed: async context => await verifySmartUnpack(context.getResources(Platform.MAC, Arch.universal)), + packed: async context => await verifySmartUnpack(context.getResources(Platform.MAC, Arch.x64)), } ) ) From 341cb13a3e6e7fa6a4e6575e0dfa98636768fbbe Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Fri, 22 Mar 2024 20:09:59 +0800 Subject: [PATCH 03/12] update ut --- test/src/mac/macPackagerTest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/mac/macPackagerTest.ts b/test/src/mac/macPackagerTest.ts index ae601b25a9..503aeed881 100644 --- a/test/src/mac/macPackagerTest.ts +++ b/test/src/mac/macPackagerTest.ts @@ -107,7 +107,7 @@ test.ifMac("yarn two package.json w/ native module", () => assertPack( "test-app-two-native-modules", { - targets: Platform.MAC.createTarget("zip", Arch.x64), + targets: Platform.MAC.createTarget("zip", Arch.universal), config: { npmRebuild: true, nativeRebuilder: "sequential", @@ -115,7 +115,7 @@ test.ifMac("yarn two package.json w/ native module", () => }, { signed: false, - packed: async context => await verifySmartUnpack(context.getResources(Platform.MAC, Arch.x64)), + packed: async context => await verifySmartUnpack(context.getResources(Platform.MAC, Arch.universal)), } ) ) From b63495cc6ce6bbc3706d373be46d1112b8323e47 Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Fri, 22 Mar 2024 22:27:00 +0800 Subject: [PATCH 04/12] find the not found --- packages/app-builder-lib/src/asar/asarUtil.ts | 61 ++++++++++++++++++- .../src/util/NodeModuleCopyHelper.ts | 29 ++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/packages/app-builder-lib/src/asar/asarUtil.ts b/packages/app-builder-lib/src/asar/asarUtil.ts index bd4aea1dbb..4b8ff8f7f5 100644 --- a/packages/app-builder-lib/src/asar/asarUtil.ts +++ b/packages/app-builder-lib/src/asar/asarUtil.ts @@ -6,14 +6,72 @@ import * as path from "path" import { AsarOptions } from "../options/PlatformSpecificBuildOptions" import { Packager } from "../packager" import { PlatformPackager } from "../platformPackager" -import { getDestinationPath, ResolvedFileSet } from "../util/appFileCopier" +import { ResolvedFileSet } from "../util/appFileCopier" import { AsarFilesystem, Node } from "./asar" import { hashFile, hashFileContents } from "./integrity" import { detectUnpackedDirs } from "./unpackDetector" +import { NODE_MODULES_PATTERN } from "../fileTransformer" // eslint-disable-next-line @typescript-eslint/no-var-requires const pickle = require("chromium-pickle-js") +function generateNewPath(filePath: string, destination: string): string { + // Split the paths into parts + const filePathParts: string[] = filePath.split(path.sep) + const destinationParts: string[] = destination.split(path.sep) + + // Find all occurrences of 'node_modules' in both paths + const nodeModulesIndicesFilePath: number[] = filePathParts.reduce((acc: number[], part: string, index: number) => { + if (part === "node_modules") acc.push(index) + return acc + }, []) + + const nodeModulesIndicesDestination: number[] = destinationParts.reduce((acc: number[], part: string, index: number) => { + if (part === "node_modules") acc.push(index) + return acc + }, []) + + // Calculate the target index for 'node_modules' in the destination path + const targetNodeModulesIndex: number = nodeModulesIndicesDestination[nodeModulesIndicesFilePath.length - 1] || nodeModulesIndicesDestination.slice(-1)[0] + + // If 'node_modules' is not found in the file path or destination, return an error message + if (nodeModulesIndicesFilePath.length === 0 || nodeModulesIndicesDestination.length === 0) { + return 'Error: The specified paths do not contain "node_modules"' + } + + // Reconstruct the path from the destination to the targeted 'node_modules' + const basePath: string = destinationParts.slice(0, targetNodeModulesIndex + 1).join(path.sep) + + // Append the part of the filePath after the last 'node_modules' to the basePath + const newPath: string = path.join(basePath, ...filePathParts.slice(nodeModulesIndicesFilePath.slice(-1)[0] + 1)) + + return newPath +} + +function getDestinationPath(file: string, fileSet: ResolvedFileSet) { + if (file === fileSet.src) { + return fileSet.destination + } else { + const src = fileSet.src + const dest = fileSet.destination + if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { + return (dest + file.substring(src.length)).replace(`${path.sep}.pnpm`, "") + } else { + // hoisted node_modules + // not lastIndexOf, to ensure that nested module (top-level module depends on) copied to parent node_modules, not to top-level directory + // project https://github.com/angexis/punchcontrol/commit/cf929aba55c40d0d8901c54df7945e1d001ce022 + let index = file.indexOf(NODE_MODULES_PATTERN) + if (index < 0 && file.endsWith(`${path.sep}node_modules`)) { + index = file.length - 13 + } + if (index < 0) { + throw new Error(`File "${file}" not under the source directory "${fileSet.src}"`) + } + return generateNewPath(file, dest).replace(`${path.sep}.pnpm`, "") + } + } +} + /** @internal */ export class AsarPackager { private readonly fs = new AsarFilesystem(this.src) @@ -94,6 +152,7 @@ export class AsarPackager { } const pathInArchive = path.relative(rootForAppFilesWithoutAsar, getDestinationPath(file, fileSet)) + log.info({ dest: "dest" }, file) if (stat.isSymbolicLink()) { const s = stat as any diff --git a/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts b/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts index 5988fe61f1..78d7f33ab7 100644 --- a/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts +++ b/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts @@ -2,6 +2,7 @@ import BluebirdPromise from "bluebird-lst" import { CONCURRENCY } from "builder-util/out/fs" import { lstat, readdir } from "fs-extra" import * as path from "path" +import * as fs from "fs" import { excludedNames, FileMatcher } from "../fileMatcher" import { Packager } from "../packager" import { resolveFunction } from "../platformPackager" @@ -31,6 +32,28 @@ const topLevelExcludedFiles = new Set([ ".bin", ]) +async function findNodeModulesWithFile(cwd: string, fileName: string) { + let nodeModulesPath = cwd + if (!cwd.endsWith(`${path.sep}node_modules`)) { + nodeModulesPath = path.join(cwd, "node_modules") + } + + try { + await fs.promises.access(nodeModulesPath, fs.constants.F_OK) + + const targetFilePath = path.join(nodeModulesPath, fileName) + await fs.promises.access(targetFilePath, fs.constants.F_OK) + + return nodeModulesPath + } catch (error) { + const parentDir = path.dirname(cwd) + if (parentDir === cwd) { + throw new Error("File not found") + } + return findNodeModulesWithFile(parentDir, fileName) + } +} + /** @internal */ export class NodeModuleCopyHelper extends FileCopyHelper { constructor(matcher: FileMatcher, packager: Packager) { @@ -46,7 +69,11 @@ export class NodeModuleCopyHelper extends FileCopyHelper { const result: Array = [] const queue: Array = [] for (const moduleName of moduleNames) { - const tmpPath = baseDir + path.sep + moduleName + let tmpPath = baseDir + path.sep + moduleName + if (!fs.existsSync(tmpPath)) { + tmpPath = (await findNodeModulesWithFile(baseDir, moduleName)) + path.sep + moduleName + console.log("xxxxx,", tmpPath) + } queue.length = 1 // The path should be corrected in Windows that when the moduleName is Scoped packages named. const depPath = path.normalize(tmpPath) From 5cc49022163b121439c752b5ea0e589fa9258e7e Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Fri, 22 Mar 2024 22:29:41 +0800 Subject: [PATCH 05/12] fix ut --- test/snapshots/HoistedNodeModuleTest.js.snap | 2 +- test/src/helpers/verifySmartUnpack.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/test/snapshots/HoistedNodeModuleTest.js.snap b/test/snapshots/HoistedNodeModuleTest.js.snap index dc7e333feb..7db9b06c2b 100644 --- a/test/snapshots/HoistedNodeModuleTest.js.snap +++ b/test/snapshots/HoistedNodeModuleTest.js.snap @@ -22,7 +22,7 @@ Object { "foo": Object { "files": Object { "package.json": Object { - "offset": "0", + "offset": "4310", "size": 127, }, }, diff --git a/test/src/helpers/verifySmartUnpack.ts b/test/src/helpers/verifySmartUnpack.ts index d5b746c0c2..7069e78e1c 100644 --- a/test/src/helpers/verifySmartUnpack.ts +++ b/test/src/helpers/verifySmartUnpack.ts @@ -31,7 +31,6 @@ export async function verifySmartUnpack(resourceDir: string, additionalVerificat if (additionalVerifications) { await additionalVerifications(asarFs) } - console.log(JSON.stringify(asarFs.header)) expect(removeUnstableProperties(asarFs.header)).toMatchSnapshot() const files = (await walk(resourceDir, file => !path.basename(file).startsWith(".") && !file.endsWith(`resources${path.sep}inspector`))).map(it => { From 8d7aa73fc4c84b6f749e4b442939844b4ea6a698 Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Sat, 23 Mar 2024 14:06:07 +0800 Subject: [PATCH 06/12] delete log --- packages/app-builder-lib/src/asar/asarUtil.ts | 15 ++------------- .../src/util/NodeModuleCopyHelper.ts | 1 - 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/packages/app-builder-lib/src/asar/asarUtil.ts b/packages/app-builder-lib/src/asar/asarUtil.ts index 4b8ff8f7f5..c51a1dd5e8 100644 --- a/packages/app-builder-lib/src/asar/asarUtil.ts +++ b/packages/app-builder-lib/src/asar/asarUtil.ts @@ -10,7 +10,6 @@ import { ResolvedFileSet } from "../util/appFileCopier" import { AsarFilesystem, Node } from "./asar" import { hashFile, hashFileContents } from "./integrity" import { detectUnpackedDirs } from "./unpackDetector" -import { NODE_MODULES_PATTERN } from "../fileTransformer" // eslint-disable-next-line @typescript-eslint/no-var-requires const pickle = require("chromium-pickle-js") @@ -55,19 +54,9 @@ function getDestinationPath(file: string, fileSet: ResolvedFileSet) { const src = fileSet.src const dest = fileSet.destination if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { - return (dest + file.substring(src.length)).replace(`${path.sep}.pnpm`, "") + return dest + file.substring(src.length) } else { - // hoisted node_modules - // not lastIndexOf, to ensure that nested module (top-level module depends on) copied to parent node_modules, not to top-level directory - // project https://github.com/angexis/punchcontrol/commit/cf929aba55c40d0d8901c54df7945e1d001ce022 - let index = file.indexOf(NODE_MODULES_PATTERN) - if (index < 0 && file.endsWith(`${path.sep}node_modules`)) { - index = file.length - 13 - } - if (index < 0) { - throw new Error(`File "${file}" not under the source directory "${fileSet.src}"`) - } - return generateNewPath(file, dest).replace(`${path.sep}.pnpm`, "") + return generateNewPath(file, dest) } } } diff --git a/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts b/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts index 78d7f33ab7..103557bd48 100644 --- a/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts +++ b/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts @@ -72,7 +72,6 @@ export class NodeModuleCopyHelper extends FileCopyHelper { let tmpPath = baseDir + path.sep + moduleName if (!fs.existsSync(tmpPath)) { tmpPath = (await findNodeModulesWithFile(baseDir, moduleName)) + path.sep + moduleName - console.log("xxxxx,", tmpPath) } queue.length = 1 // The path should be corrected in Windows that when the moduleName is Scoped packages named. From 2c0636177f20f530182734437018bf27da58954d Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Sat, 23 Mar 2024 15:58:07 +0800 Subject: [PATCH 07/12] delete useless code --- packages/app-builder-lib/src/asar/asarUtil.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/app-builder-lib/src/asar/asarUtil.ts b/packages/app-builder-lib/src/asar/asarUtil.ts index c51a1dd5e8..9b56d43752 100644 --- a/packages/app-builder-lib/src/asar/asarUtil.ts +++ b/packages/app-builder-lib/src/asar/asarUtil.ts @@ -48,17 +48,12 @@ function generateNewPath(filePath: string, destination: string): string { } function getDestinationPath(file: string, fileSet: ResolvedFileSet) { - if (file === fileSet.src) { - return fileSet.destination - } else { - const src = fileSet.src - const dest = fileSet.destination - if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { - return dest + file.substring(src.length) - } else { - return generateNewPath(file, dest) - } + const src = fileSet.src + const dest = fileSet.destination + if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { + return dest + file.substring(src.length) } + return generateNewPath(file, dest) } /** @internal */ From 62a5d556410d01d1ec22fecb25beb0930aca2c1d Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Sat, 23 Mar 2024 16:08:37 +0800 Subject: [PATCH 08/12] delete log --- packages/app-builder-lib/src/asar/asarUtil.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/app-builder-lib/src/asar/asarUtil.ts b/packages/app-builder-lib/src/asar/asarUtil.ts index 9b56d43752..0aac1ae480 100644 --- a/packages/app-builder-lib/src/asar/asarUtil.ts +++ b/packages/app-builder-lib/src/asar/asarUtil.ts @@ -136,7 +136,6 @@ export class AsarPackager { } const pathInArchive = path.relative(rootForAppFilesWithoutAsar, getDestinationPath(file, fileSet)) - log.info({ dest: "dest" }, file) if (stat.isSymbolicLink()) { const s = stat as any From e2af8887fe46ea2dccd80705d7d6f9d3c782ed83 Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Sun, 24 Mar 2024 00:43:05 +0800 Subject: [PATCH 09/12] merge getDestination --- packages/app-builder-lib/src/asar/asarUtil.ts | 44 +--------------- .../app-builder-lib/src/util/appFileCopier.ts | 51 +++++++++++++++---- 2 files changed, 41 insertions(+), 54 deletions(-) diff --git a/packages/app-builder-lib/src/asar/asarUtil.ts b/packages/app-builder-lib/src/asar/asarUtil.ts index 0aac1ae480..bd4aea1dbb 100644 --- a/packages/app-builder-lib/src/asar/asarUtil.ts +++ b/packages/app-builder-lib/src/asar/asarUtil.ts @@ -6,7 +6,7 @@ import * as path from "path" import { AsarOptions } from "../options/PlatformSpecificBuildOptions" import { Packager } from "../packager" import { PlatformPackager } from "../platformPackager" -import { ResolvedFileSet } from "../util/appFileCopier" +import { getDestinationPath, ResolvedFileSet } from "../util/appFileCopier" import { AsarFilesystem, Node } from "./asar" import { hashFile, hashFileContents } from "./integrity" import { detectUnpackedDirs } from "./unpackDetector" @@ -14,48 +14,6 @@ import { detectUnpackedDirs } from "./unpackDetector" // eslint-disable-next-line @typescript-eslint/no-var-requires const pickle = require("chromium-pickle-js") -function generateNewPath(filePath: string, destination: string): string { - // Split the paths into parts - const filePathParts: string[] = filePath.split(path.sep) - const destinationParts: string[] = destination.split(path.sep) - - // Find all occurrences of 'node_modules' in both paths - const nodeModulesIndicesFilePath: number[] = filePathParts.reduce((acc: number[], part: string, index: number) => { - if (part === "node_modules") acc.push(index) - return acc - }, []) - - const nodeModulesIndicesDestination: number[] = destinationParts.reduce((acc: number[], part: string, index: number) => { - if (part === "node_modules") acc.push(index) - return acc - }, []) - - // Calculate the target index for 'node_modules' in the destination path - const targetNodeModulesIndex: number = nodeModulesIndicesDestination[nodeModulesIndicesFilePath.length - 1] || nodeModulesIndicesDestination.slice(-1)[0] - - // If 'node_modules' is not found in the file path or destination, return an error message - if (nodeModulesIndicesFilePath.length === 0 || nodeModulesIndicesDestination.length === 0) { - return 'Error: The specified paths do not contain "node_modules"' - } - - // Reconstruct the path from the destination to the targeted 'node_modules' - const basePath: string = destinationParts.slice(0, targetNodeModulesIndex + 1).join(path.sep) - - // Append the part of the filePath after the last 'node_modules' to the basePath - const newPath: string = path.join(basePath, ...filePathParts.slice(nodeModulesIndicesFilePath.slice(-1)[0] + 1)) - - return newPath -} - -function getDestinationPath(file: string, fileSet: ResolvedFileSet) { - const src = fileSet.src - const dest = fileSet.destination - if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { - return dest + file.substring(src.length) - } - return generateNewPath(file, dest) -} - /** @internal */ export class AsarPackager { private readonly fs = new AsarFilesystem(this.src) diff --git a/packages/app-builder-lib/src/util/appFileCopier.ts b/packages/app-builder-lib/src/util/appFileCopier.ts index e3f3b70bb0..a966177075 100644 --- a/packages/app-builder-lib/src/util/appFileCopier.ts +++ b/packages/app-builder-lib/src/util/appFileCopier.ts @@ -35,27 +35,56 @@ function removePnpmAndNextTwoFolders(file: string) { return parts.join(path.sep) } -export function getDestinationPath(ttt: string, fileSet: ResolvedFileSet) { - if (ttt === fileSet.src) { +function getHoistedModulePath(filePath: string, destination: string): string { + const filePathParts: string[] = filePath.split(path.sep) + const destinationParts: string[] = destination.split(path.sep) + + const nodeModulesIndicesFilePath: number[] = filePathParts.reduce((acc: number[], part: string, index: number) => { + if (part === "node_modules") acc.push(index) + return acc + }, []) + + const nodeModulesIndicesDestination: number[] = destinationParts.reduce((acc: number[], part: string, index: number) => { + if (part === "node_modules") acc.push(index) + return acc + }, []) + + if (nodeModulesIndicesDestination.length === 0) { + // If no 'node_modules' in destination, append from the first 'node_modules' in filePath + if (nodeModulesIndicesFilePath.length > 0) { + const firstNodeModulesIndexFilePath: number = nodeModulesIndicesFilePath[0] + return path.join(destination, ...filePathParts.slice(firstNodeModulesIndexFilePath)) + } + // If also no 'node_modules' in filePath, return destination as is + return destination + } + + const targetNodeModulesIndex: number = nodeModulesIndicesDestination[nodeModulesIndicesFilePath.length - 1] || nodeModulesIndicesDestination.slice(-1)[0] + + if (nodeModulesIndicesFilePath.length === 0) { + return 'Error: The specified file path does not contain "node_modules"' + } + + const basePath: string = destinationParts.slice(0, targetNodeModulesIndex + 1).join(path.sep) + const newPath: string = path.join(basePath, ...filePathParts.slice(nodeModulesIndicesFilePath.slice(-1)[0] + 1)) + + return newPath +} + +export function getDestinationPath(filePath: string, fileSet: ResolvedFileSet) { + if (filePath === fileSet.src) { return fileSet.destination } else { const src = removePnpmAndNextTwoFolders(fileSet.src) const dest = fileSet.destination - const file = removePnpmAndNextTwoFolders(ttt) + const file = removePnpmAndNextTwoFolders(filePath) if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { return dest + file.substring(src.length) } else { // hoisted node_modules // not lastIndexOf, to ensure that nested module (top-level module depends on) copied to parent node_modules, not to top-level directory // project https://github.com/angexis/punchcontrol/commit/cf929aba55c40d0d8901c54df7945e1d001ce022 - let index = file.indexOf(NODE_MODULES_PATTERN) - if (index < 0 && file.endsWith(`${path.sep}node_modules`)) { - index = file.length - 13 - } - if (index < 0) { - throw new Error(`File "${file}" not under the source directory "${fileSet.src}"`) - } - return dest + file.substring(index) + return getHoistedModulePath(file, dest) } } } From 5bbf0f6b1a0ced3df976f8a5b0f658b819b5439c Mon Sep 17 00:00:00 2001 From: Mike Maietta Date: Sun, 24 Mar 2024 13:43:13 -0700 Subject: [PATCH 10/12] Create wet-pears-doubt.md --- .changeset/wet-pears-doubt.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/wet-pears-doubt.md diff --git a/.changeset/wet-pears-doubt.md b/.changeset/wet-pears-doubt.md new file mode 100644 index 0000000000..8b17ec7b29 --- /dev/null +++ b/.changeset/wet-pears-doubt.md @@ -0,0 +1,6 @@ +--- +"app-builder-lib": patch +"builder-util": patch +--- + +chore(deps): Update app-builder-bin 5.0-alpha From 269e83743a669bc1fe0be1743f77f51a25d561cb Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Mon, 25 Mar 2024 07:16:32 +0800 Subject: [PATCH 11/12] format error message --- .../src/util/NodeModuleCopyHelper.ts | 8 +++--- .../app-builder-lib/src/util/appFileCopier.ts | 25 ++++++++----------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts b/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts index 103557bd48..91398ba7bd 100644 --- a/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts +++ b/packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts @@ -32,7 +32,7 @@ const topLevelExcludedFiles = new Set([ ".bin", ]) -async function findNodeModulesWithFile(cwd: string, fileName: string) { +async function findNodeModulesWithFile(cwd: string, file: string) { let nodeModulesPath = cwd if (!cwd.endsWith(`${path.sep}node_modules`)) { nodeModulesPath = path.join(cwd, "node_modules") @@ -41,16 +41,16 @@ async function findNodeModulesWithFile(cwd: string, fileName: string) { try { await fs.promises.access(nodeModulesPath, fs.constants.F_OK) - const targetFilePath = path.join(nodeModulesPath, fileName) + const targetFilePath = path.join(nodeModulesPath, file) await fs.promises.access(targetFilePath, fs.constants.F_OK) return nodeModulesPath } catch (error) { const parentDir = path.dirname(cwd) if (parentDir === cwd) { - throw new Error("File not found") + throw new Error(`File "${file}" not found in the source directory "${cwd}"`) } - return findNodeModulesWithFile(parentDir, fileName) + return findNodeModulesWithFile(parentDir, file) } } diff --git a/packages/app-builder-lib/src/util/appFileCopier.ts b/packages/app-builder-lib/src/util/appFileCopier.ts index a966177075..f73300e039 100644 --- a/packages/app-builder-lib/src/util/appFileCopier.ts +++ b/packages/app-builder-lib/src/util/appFileCopier.ts @@ -35,8 +35,8 @@ function removePnpmAndNextTwoFolders(file: string) { return parts.join(path.sep) } -function getHoistedModulePath(filePath: string, destination: string): string { - const filePathParts: string[] = filePath.split(path.sep) +function getHoistedModulePath(file: string, destination: string): string { + const filePathParts: string[] = file.split(path.sep) const destinationParts: string[] = destination.split(path.sep) const nodeModulesIndicesFilePath: number[] = filePathParts.reduce((acc: number[], part: string, index: number) => { @@ -62,7 +62,7 @@ function getHoistedModulePath(filePath: string, destination: string): string { const targetNodeModulesIndex: number = nodeModulesIndicesDestination[nodeModulesIndicesFilePath.length - 1] || nodeModulesIndicesDestination.slice(-1)[0] if (nodeModulesIndicesFilePath.length === 0) { - return 'Error: The specified file path does not contain "node_modules"' + throw new Error(` The specified file path: ${file} does not contain "node_modules" to destination: ${destination}`) } const basePath: string = destinationParts.slice(0, targetNodeModulesIndex + 1).join(path.sep) @@ -74,19 +74,14 @@ function getHoistedModulePath(filePath: string, destination: string): string { export function getDestinationPath(filePath: string, fileSet: ResolvedFileSet) { if (filePath === fileSet.src) { return fileSet.destination - } else { - const src = removePnpmAndNextTwoFolders(fileSet.src) - const dest = fileSet.destination - const file = removePnpmAndNextTwoFolders(filePath) - if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { - return dest + file.substring(src.length) - } else { - // hoisted node_modules - // not lastIndexOf, to ensure that nested module (top-level module depends on) copied to parent node_modules, not to top-level directory - // project https://github.com/angexis/punchcontrol/commit/cf929aba55c40d0d8901c54df7945e1d001ce022 - return getHoistedModulePath(file, dest) - } } + const src = removePnpmAndNextTwoFolders(fileSet.src) + const dest = fileSet.destination + const file = removePnpmAndNextTwoFolders(filePath) + if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) { + return dest + file.substring(src.length) + } + return getHoistedModulePath(file, dest) } export async function copyAppFiles(fileSet: ResolvedFileSet, packager: Packager, transformer: FileTransformer) { From 7f73cd29201d24c4af7790101a119acf2448129d Mon Sep 17 00:00:00 2001 From: "payne.fu" Date: Tue, 26 Mar 2024 11:29:23 +0800 Subject: [PATCH 12/12] delete comments --- packages/app-builder-lib/src/util/appFileCopier.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/packages/app-builder-lib/src/util/appFileCopier.ts b/packages/app-builder-lib/src/util/appFileCopier.ts index f73300e039..5f02c450e4 100644 --- a/packages/app-builder-lib/src/util/appFileCopier.ts +++ b/packages/app-builder-lib/src/util/appFileCopier.ts @@ -19,19 +19,12 @@ const BOWER_COMPONENTS_PATTERN = `${path.sep}bower_components${path.sep}` export const ELECTRON_COMPILE_SHIM_FILENAME = "__shim.js" function removePnpmAndNextTwoFolders(file: string) { - // Split the path into parts const parts = file.split(path.sep) - - // Find the index of the '.pnpm' folder const pnpmIndex = parts.findIndex(part => part === ".pnpm") - // If '.pnpm' is found, and there are at least two more folders after it if (pnpmIndex >= 0 && parts.length > pnpmIndex + 2) { - // Remove '.pnpm' and the next two folders from the parts array parts.splice(pnpmIndex, 3) } - - // Rejoin the remaining parts back into a path string return parts.join(path.sep) } @@ -50,12 +43,10 @@ function getHoistedModulePath(file: string, destination: string): string { }, []) if (nodeModulesIndicesDestination.length === 0) { - // If no 'node_modules' in destination, append from the first 'node_modules' in filePath if (nodeModulesIndicesFilePath.length > 0) { const firstNodeModulesIndexFilePath: number = nodeModulesIndicesFilePath[0] return path.join(destination, ...filePathParts.slice(firstNodeModulesIndexFilePath)) } - // If also no 'node_modules' in filePath, return destination as is return destination }