-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clearTranspiled.js
54 lines (47 loc) · 1.45 KB
/
clearTranspiled.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
49
50
51
52
53
54
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
function clearTranspiledFiles(directory) {
fs.readdir(directory, (error, files) => {
if (error) {
console.error('Error reading directory:', error);
return;
}
files.forEach((file) => {
const filePath = path.join(directory, file);
fs.stat(filePath, (statErr, stats) => {
if (statErr) {
console.error('Error getting file stats:', statErr);
return;
}
if (stats.isDirectory() && path.basename(filePath) !== 'node_modules') {
// Recursive call for subdirectories, excluding 'node_modules'
clearTranspiledFiles(filePath);
} else if (
stats.isFile() &&
(path.extname(file) === '.js' || file.endsWith('.js.map'))
) {
// Check for corresponding TypeScript file
const tsFile = path.join(
directory,
`${path.basename(file, path.extname(file) === '.js' ? '.js' : '.js.map')}.ts`,
);
fs.access(tsFile, fs.constants.F_OK, (accessErr) => {
if (!accessErr) {
// If TypeScript file exists, delete JavaScript or source map file
fs.unlink(filePath, (unlinkErr) => {
if (unlinkErr) {
console.error('Error deleting file:', unlinkErr);
} else {
console.log('Deleted:', filePath);
}
});
}
});
}
});
});
});
}
// Replace 'path_to_your_directory' with the actual path to your directory
clearTranspiledFiles('.');