keycloak_theme/src/bin/sync-extensions/getExtensionModuleFileSourceCodeReadyToBeCopied.ts

142 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-11-02 22:39:03 +01:00
import { getIsPrettierAvailable, runPrettier } from "../tools/runPrettier";
import * as fsPr from "fs/promises";
import { join as pathJoin, sep as pathSep } from "path";
import { assert } from "tsafe/assert";
import type { BuildContext } from "../shared/buildContext";
2024-11-09 14:02:19 +01:00
import { KEYCLOAK_THEME } from "../shared/constants";
2024-11-02 22:39:03 +01:00
export type BuildContextLike = {
themeSrcDirPath: string;
};
assert<BuildContext extends BuildContextLike ? true : false>();
export async function getExtensionModuleFileSourceCodeReadyToBeCopied(params: {
2024-11-02 22:39:03 +01:00
buildContext: BuildContextLike;
fileRelativePath: string;
isOwnershipAction: boolean;
extensionModuleDirPath: string;
extensionModuleName: string;
extensionModuleVersion: string;
2024-11-09 14:02:19 +01:00
}): Promise<Buffer> {
const {
buildContext,
extensionModuleDirPath,
2024-11-09 14:02:19 +01:00
fileRelativePath,
isOwnershipAction,
extensionModuleName,
extensionModuleVersion
2024-11-09 14:02:19 +01:00
} = params;
2024-11-02 22:39:03 +01:00
let sourceCode = (
await fsPr.readFile(
pathJoin(extensionModuleDirPath, KEYCLOAK_THEME, fileRelativePath)
)
2024-11-02 22:39:03 +01:00
).toString("utf8");
sourceCode = addCommentToSourceCode({
sourceCode,
fileRelativePath,
2024-12-24 01:10:32 +01:00
commentLines: (() => {
const path = fileRelativePath.split(pathSep).join("/");
return isOwnershipAction
? [
`This file has been claimed for ownership from ${extensionModuleName} version ${extensionModuleVersion}.`,
2024-12-24 01:10:32 +01:00
`To relinquish ownership and restore this file to its original content, run the following command:`,
``,
`$ npx keycloakify own --path '${path}' --revert`
2024-12-24 01:10:32 +01:00
]
: [
`WARNING: Before modifying this file, run the following command:`,
``,
`$ npx keycloakify own --path '${path}'`,
``,
`This file is provided by ${extensionModuleName} version ${extensionModuleVersion}.`,
`It was copied into your repository by the postinstall script: \`keycloakify sync-extensions\`.`
2024-12-24 01:10:32 +01:00
];
})()
});
2024-11-02 22:39:03 +01:00
2024-11-09 14:02:19 +01:00
const destFilePath = pathJoin(buildContext.themeSrcDirPath, fileRelativePath);
2024-11-02 22:39:03 +01:00
format: {
if (!(await getIsPrettierAvailable())) {
break format;
}
sourceCode = await runPrettier({
2024-11-09 14:02:19 +01:00
filePath: destFilePath,
2024-11-02 22:39:03 +01:00
sourceCode
});
}
2024-11-09 14:02:19 +01:00
return Buffer.from(sourceCode, "utf8");
2024-11-02 22:39:03 +01:00
}
function addCommentToSourceCode(params: {
sourceCode: string;
fileRelativePath: string;
commentLines: string[];
}): string {
const { sourceCode, fileRelativePath, commentLines } = params;
const toResult = (comment: string) => {
return [comment, ``, sourceCode].join("\n");
};
for (const ext of [".ts", ".tsx", ".css", ".less", ".sass", ".js", ".jsx"]) {
if (!fileRelativePath.endsWith(ext)) {
continue;
}
return toResult(
[`/**`, ...commentLines.map(line => ` * ${line}`), ` */`].join("\n")
);
}
if (fileRelativePath.endsWith(".properties")) {
return toResult(commentLines.map(line => `# ${line}`).join("\n"));
}
if (fileRelativePath.endsWith(".ftl")) {
return toResult(
[`<#--`, ...commentLines.map(line => ` ${line}`), `-->`].join("\n")
);
}
if (fileRelativePath.endsWith(".html") || fileRelativePath.endsWith(".svg")) {
const comment = [
`<!--`,
...commentLines.map(
line =>
` ${line
.replace("--path", "-t")
.replace("--revert", "-r")
.replace("Before modifying", "Before modifying or replacing")}`
),
`-->`
].join("\n");
if (fileRelativePath.endsWith(".html") && sourceCode.trim().startsWith("<!")) {
const [first, ...rest] = sourceCode.split(">");
const last = rest.join(">");
return [`${first}>`, comment, last].join("\n");
}
if (fileRelativePath.endsWith(".svg") && sourceCode.trim().startsWith("<?")) {
const [first, ...rest] = sourceCode.split("?>");
const last = rest.join("?>");
return [`${first}?>`, comment, last].join("\n");
}
return toResult(comment);
}
return sourceCode;
}