Skip to content

Commit

Permalink
fix: delete the file symlink when the target is empty (#8371)
Browse files Browse the repository at this point in the history
  • Loading branch information
beyondkmp committed Jul 24, 2024
1 parent b20496b commit afd8132
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-monkeys-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-lib": patch
---

delete the symlink file when the target is empty
24 changes: 22 additions & 2 deletions packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Packager } from "../packager"
import { resolveFunction } from "../platformPackager"
import { FileCopyHelper } from "./AppFileWalker"
import { NodeModuleInfo } from "./packageDependencies"
import { realpathSync } from "fs"

const excludedFiles = new Set(
[".DS_Store", "node_modules" /* already in the queue */, "CHANGELOG.md", "ChangeLog", "changelog.md", "Changelog.md", "Changelog", "binding.gyp", ".npmignore"].concat(
Expand Down Expand Up @@ -44,8 +45,10 @@ export class NodeModuleCopyHelper extends FileCopyHelper {

const onNodeModuleFile = await resolveFunction(this.packager.appInfo.type, this.packager.config.onNodeModuleFile, "onNodeModuleFile")

const result: Array<string> = []
const result: Array<string | undefined> = []
const queue: Array<string> = []
const emptyDirs: Set<string> = new Set()
const symlinkFiles: Map<string, number> = new Map()
const tmpPath = moduleInfo.dir
const moduleName = moduleInfo.name
queue.length = 1
Expand Down Expand Up @@ -130,17 +133,34 @@ export class NodeModuleCopyHelper extends FileCopyHelper {
CONCURRENCY
)

let isEmpty = true
for (const child of sortedFilePaths) {
if (child != null) {
result.push(child)
if (this.metadata.get(child)?.isSymbolicLink()) {
symlinkFiles.set(child, result.length - 1)
}
isEmpty = false
}
}

if (isEmpty) {
emptyDirs.add(dirPath)
}

dirs.sort()
for (const child of dirs) {
queue.push(dirPath + path.sep + child)
}
}
return result

for (const [file, index] of symlinkFiles) {
const resolvedPath = realpathSync(file)
if (emptyDirs.has(resolvedPath)) {
// delete symlink file if target is a empty dir
result[index] = undefined
}
}
return result.filter((it): it is string => it !== undefined)
}
}

0 comments on commit afd8132

Please sign in to comment.