keycloak_theme/src/bin/tools/transformCodebase.ts
Joseph Garrone 2a5a4c16ea fix build
2021-03-03 02:31:02 +01:00

70 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import * as fs from "fs";
import * as path from "path";
import { crawl } from "./crawl";
import { id } from "evt/tools/typeSafety/id";
type TransformSourceCode =
(params: {
sourceCode: Buffer;
filePath: string;
}) => {
modifiedSourceCode: Buffer;
newFileName?: string;
} | undefined;
/** Apply a transformation function to every file of directory */
export function transformCodebase(
params: {
srcDirPath: string;
destDirPath: string;
transformSourceCode?: TransformSourceCode;
}
) {
const {
srcDirPath,
destDirPath,
transformSourceCode = id<TransformSourceCode>(({ sourceCode }) => ({ "modifiedSourceCode": sourceCode }))
} = params;
for (const file_relative_path of crawl(srcDirPath)) {
const filePath = path.join(srcDirPath, file_relative_path);
const transformSourceCodeResult = transformSourceCode({
"sourceCode": fs.readFileSync(filePath),
"filePath": path.join(srcDirPath, file_relative_path)
});
if (transformSourceCodeResult === undefined) {
continue;
}
fs.mkdirSync(
path.dirname(
path.join(
destDirPath,
file_relative_path
)
),
{ "recursive": true }
);
const { newFileName, modifiedSourceCode } = transformSourceCodeResult;
fs.writeFileSync(
path.join(
path.dirname(path.join(destDirPath, file_relative_path)),
newFileName ?? path.basename(file_relative_path)
),
modifiedSourceCode
);
}
}