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";
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
type TransformSourceCode = (params: {
|
|
|
|
sourceCode: Buffer;
|
|
|
|
filePath: string;
|
|
|
|
fileRelativePath: string;
|
|
|
|
}) =>
|
2021-10-11 21:35:40 +02:00
|
|
|
| {
|
|
|
|
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;
|
|
|
|
}) {
|
2024-02-07 20:01:26 +01:00
|
|
|
const { srcDirPath, transformSourceCode } = params;
|
2024-02-05 08:52:58 +01:00
|
|
|
|
2024-02-08 14:09:55 +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;
|
2024-02-08 14:09:55 +01:00
|
|
|
|
|
|
|
fs.mkdirSync(destDirPath, {
|
2024-05-20 15:48:51 +02:00
|
|
|
recursive: true
|
2024-02-08 14:09:55 +01:00
|
|
|
});
|
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);
|
2024-02-07 20:01:26 +01:00
|
|
|
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
|
2024-02-07 20:01:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-07 20:01:26 +01:00
|
|
|
fs.mkdirSync(path.dirname(destFilePath), {
|
2024-05-20 15:48:51 +02:00
|
|
|
recursive: true
|
2021-10-11 21:35:40 +02:00
|
|
|
});
|
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
|
|
|
}
|