-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathpatchFs.ts
59 lines (53 loc) Β· 1.56 KB
/
patchFs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
PatchedPackageDetails,
getPackageDetailsFromPatchFilename,
} from "./PackageDetails"
import { relative } from "./path"
import klawSync from "klaw-sync"
export const getPatchFiles = (patchesDir: string) => {
try {
return klawSync(patchesDir, { nodir: true })
.map(({ path }) => relative(patchesDir, path))
.filter((path) => path.endsWith(".patch"))
} catch (e) {
return []
}
}
interface GroupedPatches {
numPatchFiles: number
pathSpecifierToPatchFiles: Record<string, PatchedPackageDetails[]>
warnings: string[]
}
export const getGroupedPatches = (patchesDirectory: string): GroupedPatches => {
const files = getPatchFiles(patchesDirectory)
if (files.length === 0) {
return {
numPatchFiles: 0,
pathSpecifierToPatchFiles: {},
warnings: [],
}
}
const warnings: string[] = []
const pathSpecifierToPatchFiles: Record<string, PatchedPackageDetails[]> = {}
for (const file of files) {
const details = getPackageDetailsFromPatchFilename(file)
if (!details) {
warnings.push(`Unrecognized patch file in patches directory ${file}`)
continue
}
if (!pathSpecifierToPatchFiles[details.pathSpecifier]) {
pathSpecifierToPatchFiles[details.pathSpecifier] = []
}
pathSpecifierToPatchFiles[details.pathSpecifier].push(details)
}
for (const arr of Object.values(pathSpecifierToPatchFiles)) {
arr.sort((a, b) => {
return (a.sequenceNumber ?? 0) - (b.sequenceNumber ?? 0)
})
}
return {
numPatchFiles: files.length,
pathSpecifierToPatchFiles,
warnings,
}
}