-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
48 lines (43 loc) · 1.77 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const fs = require('fs');
const { exec } = require('child_process');
function runTscAndDeleteTsFiles(directory) {
fs.readdir(directory, (err, files) => {
if (err) {
console.error('Error reading directory:', err);
return;
}
files.forEach((file) => {
const filePath = `${directory}/${file}`;
fs.stat(filePath, (err, stats) => {
if (err) {
console.error('Error getting file stats:', err);
return;
}
if (stats.isDirectory()) {
// Recursively call the function for subdirectories
runTscAndDeleteTsFiles(filePath);
} else if (file.endsWith('.ts')) {
// Run tsc for TypeScript files
const tscCommand = `tsc ${filePath}`;
exec(tscCommand, (err, stdout, stderr) => {
if (err) {
console.error(`Error running ${tscCommand}:`, err);
} else {
console.log(stdout);
// Delete the TypeScript file after running tsc
fs.unlink(filePath, (err) => {
if (err) {
console.error(`Error deleting ${filePath}:`, err);
} else {
console.log(`${filePath} deleted.`);
}
});
}
});
}
});
});
});
}
// Example: Run tsc and delete .ts files in the current directory and its subdirectories
runTscAndDeleteTsFiles(__dirname);