|
1
|
import fs from "fs/promises" |
|
2
|
import { |
|
3
|
File, |
|
4
|
isIdentifier, |
|
5
|
isObjectProperty, |
|
6
|
parse, |
|
7
|
ParseResult, |
|
8
|
traverse, |
|
9
|
} from "../babel" |
|
10
|
import { logger } from "../logger" |
|
11
|
import { crawl, generateHash, getParserOptions } from "../utils" |
|
12
|
import { getConfigArgument } from "./helpers" |
|
13
|
|
|
14
|
export async function generateCustomFieldHashes( |
|
15
|
sources: Set<string> |
|
16
|
): Promise<{ linkHash: string; formHash: string; displayHash: string }> { |
|
17
|
const files = await getFilesFromSources(sources) |
|
18
|
const contents = await Promise.all(files.map(getCustomFieldContents)) |
|
19
|
|
|
20
|
const linkContents = contents.map((c) => c.link).filter(Boolean) |
|
21
|
const formContents = contents.map((c) => c.form).filter(Boolean) |
|
22
|
const displayContents = contents.map((c) => c.display).filter(Boolean) |
|
23
|
|
|
24
|
const totalLinkContent = linkContents.join("") |
|
25
|
const totalFormContent = formContents.join("") |
|
26
|
const totalDisplayContent = displayContents.join("") |
|
27
|
|
|
28
|
return { |
|
29
|
linkHash: generateHash(totalLinkContent), |
|
30
|
formHash: generateHash(totalFormContent), |
|
31
|
displayHash: generateHash(totalDisplayContent), |
|
32
|
} |
|
33
|
} |
|
34
|
|
|
35
|
async function getFilesFromSources(sources: Set<string>): Promise<string[]> { |
|
36
|
return ( |
|
37
|
await Promise.all( |
|
38
|
Array.from(sources).map(async (source) => |
|
39
|
crawl(`${source}/custom-fields`) |
|
40
|
) |
|
41
|
) |
|
42
|
).flat() |
|
43
|
} |
|
44
|
|
|
45
|
async function getCustomFieldContents(file: string): Promise<{ |
|
46
|
link: string | null |
|
47
|
form: string | null |
|
48
|
display: string | null |
|
49
|
}> { |
|
50
|
const code = await fs.readFile(file, "utf-8") |
|
51
|
|
|
52
|
let ast: ParseResult<File> | null = null |
|
53
|
|
|
54
|
try { |
|
55
|
ast = parse(code, getParserOptions(file)) |
|
56
|
} catch (e) { |
|
57
|
logger.error(`An error occurred while parsing the file.`, { |
|
58
|
file, |
|
59
|
error: e, |
|
60
|
}) |
|
61
|
return { link: null, form: null, display: null } |
|
62
|
} |
|
63
|
|
|
64
|
let linkContent: string | null = null |
|
65
|
let formContent: string | null = null |
|
66
|
let displayContent: string | null = null |
|
67
|
|
|
68
|
try { |
|
69
|
traverse(ast, { |
|
70
|
ExportDefaultDeclaration(path) { |
|
71
|
const configArgument = getConfigArgument(path) |
|
72
|
if (!configArgument) { |
|
73
|
return |
|
74
|
} |
|
75
|
|
|
76
|
configArgument.properties.forEach((prop) => { |
|
77
|
if (!isObjectProperty(prop) || !prop.key || !isIdentifier(prop.key)) { |
|
78
|
return |
|
79
|
} |
|
80
|
|
|
81
|
switch (prop.key.name) { |
|
82
|
case "link": |
|
83
|
linkContent = code.slice(prop.start!, prop.end!) |
|
84
|
break |
|
85
|
case "forms": |
|
86
|
formContent = code.slice(prop.start!, prop.end!) |
|
87
|
break |
|
88
|
case "display": |
|
89
|
displayContent = code.slice(prop.start!, prop.end!) |
|
90
|
break |
|
91
|
} |
|
92
|
}) |
|
93
|
}, |
|
94
|
}) |
|
95
|
} catch (e) { |
|
96
|
logger.error( |
|
97
|
`An error occurred while processing ${file}. See the below error for more details:\n${e}`, |
|
98
|
{ file, error: e } |
|
99
|
) |
|
100
|
return { link: null, form: null, display: null } |
|
101
|
} |
|
102
|
|
|
103
|
return { link: linkContent, form: formContent, display: displayContent } |
|
104
|
} |