Scripts
Calculate migration Typescript
Within a Javascript
is migrating to Typescript
project, sometime you have a need to know what percentile you already migrate to Typescript
. Here is a simple bash script can tell you that.
Javascript
You can put this in a script to run at the end of a pipeline to show the migration progress in the project. Later you can just check the old pipelines to see the progress without checking out the code and run each.
const fs = require("fs");
const path = require("path");
const folderPath = "./src";
let totalFiles = 0;
let tsxFiles = 0;
function countFiles(dir) {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
const fileStat = fs.lstatSync(filePath);
if (fileStat.isDirectory()) {
countFiles(filePath);
return;
}
if (path.extname(filePath) === ".js") totalFiles++;
if (path.extname(filePath) === ".tsx") {
totalFiles++;
tsxFiles++;
}
});
}
countFiles(folderPath);
const percentage = (tsxFiles / totalFiles) * 100;
console.table({
".tsx": tsxFiles,
Total: totalFiles,
"%": `${percentage.toFixed(2)}%`,
});