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: App builder bin 5.0 #8147

Closed
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -46,7 +69,10 @@ export class NodeModuleCopyHelper extends FileCopyHelper {
const result: Array<string> = []
const queue: Array<string> = []
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
}
queue.length = 1
// The path should be corrected in Windows that when the moduleName is Scoped packages named.
const depPath = path.normalize(tmpPath)
Expand Down
69 changes: 58 additions & 11 deletions packages/app-builder-lib/src/util/appFileCopier.ts
Expand Up @@ -18,26 +18,73 @@ 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)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we able to make this guarantee that the two nested folders within .pnpm folder are okay to be removed? Are they unnecessary symlinks or something?


// Rejoin the remaining parts back into a path string
return parts.join(path.sep)
}

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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor: Please move this to new line

if (part === "node_modules") { 
   acc.push(index)
}

Same comment applies to line 41

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 = fileSet.src
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
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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/builder-util/package.json
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/snapshots/HoistedNodeModuleTest.js.snap
Expand Up @@ -22,7 +22,7 @@ Object {
"foo": Object {
"files": Object {
"package.json": Object {
"offset": "0",
"offset": "4310",
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is an odd change. What's at offset 0 now?

"size": 127,
},
},
Expand Down
99 changes: 99 additions & 0 deletions test/snapshots/mac/macPackagerTest.js.snap
Expand Up @@ -501,6 +501,19 @@ Object {
},
"node_modules": Object {
"files": Object {
"bindings": Object {
"files": Object {
"LICENSE.md": Object {
"size": "<size>",
},
"bindings.js": Object {
"size": "<size>",
},
"package.json": Object {
"size": "<size>",
},
},
},
"debug": Object {
"files": Object {
"LICENSE": Object {
Expand Down Expand Up @@ -534,6 +547,92 @@ Object {
},
},
},
"file-uri-to-path": Object {
"files": Object {
"History.md": Object {
"size": "<size>",
},
"LICENSE": Object {
"size": "<size>",
},
"index.js": Object {
"size": "<size>",
},
"package.json": Object {
"size": "<size>",
},
},
},
"ms": Object {
"files": Object {
"index.js": Object {
"size": "<size>",
},
"license.md": Object {
"size": "<size>",
},
"package.json": Object {
"size": "<size>",
},
},
},
"node-addon-api": Object {
"files": Object {
"LICENSE.md": Object {
"size": "<size>",
},
"common.gypi": Object {
"size": "<size>",
},
"except.gypi": Object {
"size": "<size>",
},
"index.js": Object {
"size": "<size>",
},
"napi-inl.deprecated.h": Object {
"size": "<size>",
},
"napi-inl.h": Object {
"size": "<size>",
},
"napi.h": Object {
"size": "<size>",
},
"node_api.gyp": Object {
"size": "<size>",
},
"noexcept.gypi": Object {
"size": "<size>",
},
"nothing.c": Object {
"size": "<size>",
},
"package-support.json": Object {
"size": "<size>",
},
"package.json": Object {
"size": "<size>",
},
"tools": Object {
"files": Object {
"README.md": Object {
"size": "<size>",
},
"check-napi.js": Object {
"size": "<size>",
},
"clang-format.js": Object {
"size": "<size>",
},
"conversion.js": Object {
"executable": true,
"size": "<size>",
},
},
},
},
},
"node-mac-permissions": Object {
"files": Object {
".prettierrc": Object {
Expand Down