Skip to main content

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.

Result

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)}%`,
});

Bash

total_files=$(find ./src -name "*.tsx" -o -name "*.jsx" -o -name "*.js" -type f | wc -l)
tsx_files=$(find ./src -name "*.tsx" -type f | wc -l)
percentage=$(echo "scale=2; $tsx_files * 100 / $total_files" | bc)
echo "$tsx_files .tsx files out of $total_files files, which is $percentage%."

Regex for validating patterns in Git

To validate commit in remote you want to use some convention in the team like https://www.conventionalcommits.org/en/v1.0.0/.

A simple regex to help you build your ideal pattern faster:

^(fix|feat|build|chore|ci|docs|style|refactor|perf|test):\s.+

Testing script

var re = /^(fix|feat|build|chore|ci|docs|style|refactor|perf|test):\s.+/;

console.log(re.test("fix something")); // false
console.log(re.test("fix: something")); // true