keycloak_theme/src/bin/tools/transformCodebase.ts

89 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-02-21 17:38:59 +01:00
import * as fs from "fs";
import * as path from "path";
2021-02-21 23:40:10 +01:00
import { crawl } from "./crawl";
2024-02-05 08:52:58 +01:00
import { rmSync } from "../tools/fs.rmSync";
2024-05-20 15:48:51 +02:00
type TransformSourceCode = (params: {
sourceCode: Buffer;
filePath: string;
fileRelativePath: string;
}) =>
| {
modifiedSourceCode: Buffer;
newFileName?: string;
}
| undefined;
2021-03-03 02:31:02 +01:00
2024-02-05 08:52:58 +01:00
/**
* Apply a transformation function to every file of directory
* If source and destination are the same this function can be used to apply the transformation in place
* like filtering out some files or modifying them.
* */
2024-05-20 15:48:51 +02:00
export function transformCodebase(params: {
srcDirPath: string;
destDirPath: string;
transformSourceCode?: TransformSourceCode;
}) {
const { srcDirPath, transformSourceCode } = params;
2024-02-05 08:52:58 +01:00
const isTargetSameAsSource = path.relative(srcDirPath, params.destDirPath) === "";
2024-02-05 08:52:58 +01:00
2024-05-20 15:48:51 +02:00
const destDirPath = isTargetSameAsSource
? path.join(srcDirPath, "..", "tmp_xOsPdkPsTdzPs34sOkHs")
: params.destDirPath;
fs.mkdirSync(destDirPath, {
2024-05-20 15:48:51 +02:00
recursive: true
});
2021-02-21 17:38:59 +01:00
2024-05-20 15:48:51 +02:00
for (const fileRelativePath of crawl({
dirPath: srcDirPath,
returnedPathsType: "relative to dirPath"
})) {
2023-08-24 08:58:00 +02:00
const filePath = path.join(srcDirPath, fileRelativePath);
const destFilePath = path.join(destDirPath, fileRelativePath);
// NOTE: Optimization, if we don't need to transform the file, just copy
// it using the lower level implementation.
if (transformSourceCode === undefined) {
fs.mkdirSync(path.dirname(destFilePath), {
2024-05-20 15:48:51 +02:00
recursive: true
});
fs.copyFileSync(filePath, destFilePath);
continue;
}
2021-02-21 17:38:59 +01:00
2021-03-03 02:31:02 +01:00
const transformSourceCodeResult = transformSourceCode({
2024-05-20 15:48:51 +02:00
sourceCode: fs.readFileSync(filePath),
2023-08-24 08:58:00 +02:00
filePath,
fileRelativePath
2021-02-21 17:38:59 +01:00
});
2021-03-03 02:31:02 +01:00
if (transformSourceCodeResult === undefined) {
2021-02-21 17:38:59 +01:00
continue;
}
fs.mkdirSync(path.dirname(destFilePath), {
2024-05-20 15:48:51 +02:00
recursive: true
});
2021-02-21 17:38:59 +01:00
2021-03-03 02:31:02 +01:00
const { newFileName, modifiedSourceCode } = transformSourceCodeResult;
2021-02-21 17:38:59 +01:00
2024-05-20 15:48:51 +02:00
fs.writeFileSync(
path.join(
path.dirname(destFilePath),
newFileName ?? path.basename(destFilePath)
),
modifiedSourceCode
);
2021-02-21 17:38:59 +01:00
}
2024-02-05 08:52:58 +01:00
if (isTargetSameAsSource) {
2024-05-20 15:48:51 +02:00
rmSync(srcDirPath, { recursive: true });
2024-02-05 08:52:58 +01:00
fs.renameSync(destDirPath, srcDirPath);
}
2021-02-21 17:38:59 +01:00
}