Skip to content

Commit

Permalink
cross platform clean script.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Nov 29, 2023
1 parent eb2e515 commit 7411269
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@behave-graph/monorepo",
"version": "0.9.10",
"private": true,
"type": "module",
"description": "Simple, extensible behavior graph engine",
"keywords": [
"behavior",
Expand All @@ -25,9 +26,9 @@
"scripts": {
"build": "lerna run build",
"changeset:add": "changeset add",
"clean:dist": "rm -rf packages/*/dist apps/*/dist",
"clean:node_modules": "rm -rf node_modules packages/*/node_modules apps/*/node_modules",
"clean": "npm run clean:dist && npm run clean:node_modules",
"clean:node_modules": "node scripts/clean.js --node_modules",
"clean:dist": "node scripts/clean.js --dist",
"clean": "node scripts/clean.js --node_modules --dist",
"watch": "lerna run watch --parallel --stream",
"docs": "typedoc",
"format": "eslint \"{apps,packages}/*/src/**/*.{js,ts,tsx,json}\" --fix && prettier \"{apps,packages}/*/src/**/*.{js,ts,tsx,json}\" --check --write",
Expand Down
58 changes: 58 additions & 0 deletions scripts/clean.js
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();

0 comments on commit 7411269

Please sign in to comment.