2023-06-19 00:09:21 +02:00
|
|
|
import { crawl } from "../../tools/crawl";
|
|
|
|
import { join as pathJoin } from "path";
|
|
|
|
import * as fs from "fs";
|
2024-05-15 05:14:01 +02:00
|
|
|
import type { ThemeType } from "../../shared/constants";
|
2024-05-16 09:29:26 +02:00
|
|
|
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
2023-06-19 00:09:21 +02:00
|
|
|
|
2023-06-21 18:06:12 +02:00
|
|
|
/** Assumes the theme type exists */
|
2024-05-20 15:48:51 +02:00
|
|
|
export function readFieldNameUsage(params: {
|
|
|
|
themeSrcDirPath: string;
|
|
|
|
themeType: ThemeType;
|
|
|
|
}): string[] {
|
2024-05-16 09:29:26 +02:00
|
|
|
const { themeSrcDirPath, themeType } = params;
|
2023-06-19 00:09:21 +02:00
|
|
|
|
2024-05-07 18:10:22 +02:00
|
|
|
const fieldNames = new Set<string>();
|
2023-06-19 00:09:21 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
for (const srcDirPath of [
|
|
|
|
pathJoin(getThisCodebaseRootDirPath(), "src", themeType),
|
|
|
|
pathJoin(themeSrcDirPath, themeType)
|
|
|
|
]) {
|
|
|
|
const filePaths = crawl({
|
|
|
|
dirPath: srcDirPath,
|
|
|
|
returnedPathsType: "absolute"
|
|
|
|
}).filter(filePath => /\.(ts|tsx|js|jsx)$/.test(filePath));
|
2023-06-19 00:09:21 +02:00
|
|
|
|
|
|
|
for (const filePath of filePaths) {
|
|
|
|
const rawSourceFile = fs.readFileSync(filePath).toString("utf8");
|
|
|
|
|
2023-06-19 00:49:40 +02:00
|
|
|
if (!rawSourceFile.includes("messagesPerField")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
for (const functionName of [
|
|
|
|
"printIfExists",
|
|
|
|
"existsError",
|
|
|
|
"get",
|
|
|
|
"exists",
|
|
|
|
"getFirstError"
|
|
|
|
] as const) {
|
2024-05-07 18:10:22 +02:00
|
|
|
if (!rawSourceFile.includes(functionName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
rawSourceFile
|
|
|
|
.split(functionName)
|
|
|
|
.filter(part => part.startsWith("("))
|
|
|
|
.map(part => {
|
|
|
|
let [p1] = part.split(")");
|
|
|
|
|
|
|
|
p1 = p1.slice(1);
|
|
|
|
|
|
|
|
return p1;
|
|
|
|
})
|
|
|
|
.map(part => {
|
|
|
|
return part
|
|
|
|
.split(",")
|
|
|
|
.map(a => a.trim())
|
2024-05-20 15:48:51 +02:00
|
|
|
.filter((...[, i]) =>
|
|
|
|
functionName !== "printIfExists" ? true : i === 0
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
a =>
|
|
|
|
a.startsWith('"') ||
|
|
|
|
a.startsWith("'") ||
|
|
|
|
a.startsWith("`")
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
a =>
|
|
|
|
a.endsWith('"') ||
|
|
|
|
a.endsWith("'") ||
|
|
|
|
a.endsWith("`")
|
|
|
|
)
|
2024-05-07 18:10:22 +02:00
|
|
|
.map(a => a.slice(1).slice(0, -1));
|
|
|
|
})
|
|
|
|
.flat()
|
|
|
|
.forEach(fieldName => fieldNames.add(fieldName));
|
|
|
|
} catch {}
|
|
|
|
}
|
2023-06-19 00:09:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:10:22 +02:00
|
|
|
return Array.from(fieldNames);
|
2023-06-19 00:09:21 +02:00
|
|
|
}
|