Skip to content

Commit 432f784

Browse files
committed
add findLongestCommonDirectory
1 parent 2469179 commit 432f784

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

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

+19-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ const BOWER_COMPONENTS_PATTERN = `${path.sep}bower_components${path.sep}`
1818
/** @internal */
1919
export const ELECTRON_COMPILE_SHIM_FILENAME = "__shim.js"
2020

21+
function findLongestCommonDirectory(path1: string, path2: string) {
22+
const parts1 = path1.split(path.sep)
23+
const parts2 = path2.split(path.sep)
24+
25+
const commonParts = []
26+
27+
for (let i = 0; i < Math.min(parts1.length, parts2.length); i++) {
28+
if (parts1[i] === parts2[i]) {
29+
commonParts.push(parts1[i])
30+
} else {
31+
break
32+
}
33+
}
34+
35+
return commonParts.length > 0 ? path.join(...commonParts, path.sep) : path.sep
36+
}
37+
2138
export function getDestinationPath(file: string, fileSet: ResolvedFileSet) {
2239
if (file === fileSet.src) {
2340
return fileSet.destination
@@ -27,17 +44,8 @@ export function getDestinationPath(file: string, fileSet: ResolvedFileSet) {
2744
if (file.length > src.length && file.startsWith(src) && file[src.length] === path.sep) {
2845
return dest + file.substring(src.length)
2946
} else {
30-
// hoisted node_modules
31-
// not lastIndexOf, to ensure that nested module (top-level module depends on) copied to parent node_modules, not to top-level directory
32-
// project https://github.com/angexis/punchcontrol/commit/cf929aba55c40d0d8901c54df7945e1d001ce022
33-
let index = file.indexOf(NODE_MODULES_PATTERN)
34-
if (index < 0 && file.endsWith(`${path.sep}node_modules`)) {
35-
index = file.length - 13
36-
}
37-
// if (index < 0) {
38-
// throw new Error(`File "${file}" not under the source directory "${fileSet.src}"`)
39-
// }
40-
return dest + file.substring(index)
47+
const commonParts = findLongestCommonDirectory(file, dest)
48+
return dest + file.substring(commonParts.length)
4149
}
4250
}
4351
}

0 commit comments

Comments
 (0)