-
Notifications
You must be signed in to change notification settings - Fork 26
/
clean.js
58 lines (50 loc) · 1.65 KB
/
clean.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
55
56
57
58
import fs from 'fs/promises';
import path from 'path';
import process from 'process';
const getSubDirectories = async (directory) => {
try {
const stats = await fs.stat(directory);
if (stats.isDirectory()) {
// get the subdirectories of the packages directory
const subDirectoryPath = path.join(process.cwd(), directory);
const dirNames = await fs.readdir(subDirectoryPath);
return dirNames.map((dirName) => path.join(directory, dirName));
}
} catch (error) {
return [];
}
};
// Function to asynchronously delete a directory
async function deleteDirectory(directory) {
try {
const stats = await fs.stat(directory);
if (stats.isDirectory()) {
try {
await fs.rm(directory, { recursive: true });
} catch (error) {
console.error(`Error deleting ${directory}: ${error.message}`);
}
}
} catch (error) {
//console.error(`Error deleting ${directory}: ${error.message}`);
}
}
const isNodeModulesFlagSet = process.argv.includes('--node_modules');
const isDistFlagSet = process.argv.includes('--dist');
const main = async () => {
const projectDirectories = [
'.',
...(await getSubDirectories('packages')),
...(await getSubDirectories('apps')),
...(await getSubDirectories('examples'))
];
const directoriesToDelete = [];
projectDirectories.forEach((directory) => {
if (isNodeModulesFlagSet)
directoriesToDelete.push(path.join(directory, 'node_modules'));
if (isDistFlagSet) directoriesToDelete.push(path.join(directory, 'dist'));
});
// Run deletion operations in parallel
await Promise.all(directoriesToDelete.map(deleteDirectory));
};
main();