dsql-admin/dsql-app/utils/appendTsCheck.ts

38 lines
913 B
TypeScript
Raw Normal View History

2024-11-05 11:12:42 +00:00
// @ts-check
2025-01-13 08:00:21 +00:00
import fs from "fs";
import path from "path";
2024-11-05 11:12:42 +00:00
const excludeRegexp = /\/node_modules|\/dump|\/.tmp|\/.next/;
2025-01-13 08:00:21 +00:00
function traverse(dir: string) {
2024-11-05 11:12:42 +00:00
const files = fs.readdirSync(dir);
if (dir.match(excludeRegexp)) return;
for (let i = 0; i < files.length; i++) {
const file = files[i];
const filePath = path.resolve(dir, file);
const fileStat = fs.statSync(filePath);
if (fileStat.isDirectory()) {
traverse(filePath);
continue;
}
const fileMatch = file.match(/\.(jsx?)$/);
if (fileMatch) {
const fileContent = fs.readFileSync(filePath, "utf-8");
if (fileContent.includes("@ts-check")) continue;
fs.writeFileSync(
filePath,
"// @ts-check\n\n" + fileContent,
"utf-8"
);
}
}
}
traverse(process.cwd());