-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); |