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 all 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
6 changes: 6 additions & 0 deletions .changeset/wet-pears-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"app-builder-lib": patch
"builder-util": patch
---

chore(deps): Update app-builder-bin 5.0-alpha
28 changes: 27 additions & 1 deletion packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts
Original file line number Diff line number Diff line change
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, file: 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, 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 "${file}" not found in the source directory "${cwd}"`)
}
return findNodeModulesWithFile(parentDir, file)
}
}

/** @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
73 changes: 53 additions & 20 deletions packages/app-builder-lib/src/util/appFileCopier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,61 @@ 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) {
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 {
// 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)
function removePnpmAndNextTwoFolders(file: string) {
const parts = file.split(path.sep)
const pnpmIndex = parts.findIndex(part => part === ".pnpm")

if (pnpmIndex >= 0 && parts.length > pnpmIndex + 2) {
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?

return parts.join(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) => {
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 (nodeModulesIndicesFilePath.length > 0) {
const firstNodeModulesIndexFilePath: number = nodeModulesIndicesFilePath[0]
return path.join(destination, ...filePathParts.slice(firstNodeModulesIndexFilePath))
}
return destination
}

const targetNodeModulesIndex: number = nodeModulesIndicesDestination[nodeModulesIndicesFilePath.length - 1] || nodeModulesIndicesDestination.slice(-1)[0]

if (nodeModulesIndicesFilePath.length === 0) {
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)
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
}
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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/builder-util/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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