Skip to content

Commit afd8132

Browse files
authored
fix: delete the file symlink when the target is empty (#8371)
1 parent b20496b commit afd8132

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

.changeset/happy-monkeys-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"app-builder-lib": patch
3+
---
4+
5+
delete the symlink file when the target is empty

packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Packager } from "../packager"
77
import { resolveFunction } from "../platformPackager"
88
import { FileCopyHelper } from "./AppFileWalker"
99
import { NodeModuleInfo } from "./packageDependencies"
10+
import { realpathSync } from "fs"
1011

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

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

47-
const result: Array<string> = []
48+
const result: Array<string | undefined> = []
4849
const queue: Array<string> = []
50+
const emptyDirs: Set<string> = new Set()
51+
const symlinkFiles: Map<string, number> = new Map()
4952
const tmpPath = moduleInfo.dir
5053
const moduleName = moduleInfo.name
5154
queue.length = 1
@@ -130,17 +133,34 @@ export class NodeModuleCopyHelper extends FileCopyHelper {
130133
CONCURRENCY
131134
)
132135

136+
let isEmpty = true
133137
for (const child of sortedFilePaths) {
134138
if (child != null) {
135139
result.push(child)
140+
if (this.metadata.get(child)?.isSymbolicLink()) {
141+
symlinkFiles.set(child, result.length - 1)
142+
}
143+
isEmpty = false
136144
}
137145
}
138146

147+
if (isEmpty) {
148+
emptyDirs.add(dirPath)
149+
}
150+
139151
dirs.sort()
140152
for (const child of dirs) {
141153
queue.push(dirPath + path.sep + child)
142154
}
143155
}
144-
return result
156+
157+
for (const [file, index] of symlinkFiles) {
158+
const resolvedPath = realpathSync(file)
159+
if (emptyDirs.has(resolvedPath)) {
160+
// delete symlink file if target is a empty dir
161+
result[index] = undefined
162+
}
163+
}
164+
return result.filter((it): it is string => it !== undefined)
145165
}
146166
}

0 commit comments

Comments
 (0)