Open History to see the last commit
1import * as NodeFS from "node:fs";
2import * as NodePath from "node:path";
3import { fileURLToPath } from "node:url";
4
5const DEFAULT_REMOVE_OPTIONS = {
6 recursive: true,
7 force: true,
8 maxRetries: 8,
9 retryDelay: 125,
10};
11
12function childDirectories(rootDir, parentName) {
13 const parentPath = NodePath.join(rootDir, parentName);
14 let entries;
15 try {
16 entries = NodeFS.readdirSync(parentPath, { withFileTypes: true });
17 } catch (error) {
18 if (error && typeof error === "object" && error.code === "ENOENT") {
19 return [];
20 }
21 throw error;
22 }
23
24 return entries
25 .filter((entry) => entry.isDirectory())
26 .map((entry) => NodePath.join(parentPath, entry.name));
27}
28
29export function collectCleanTargets(rootDir) {
30 const workspaceDirs = [
31 ...childDirectories(rootDir, "apps"),
32 ...childDirectories(rootDir, "packages"),
33 ];
34 return [
35 NodePath.join(rootDir, "node_modules"),
36 NodePath.join(rootDir, ".vite-plus"),
37 ...workspaceDirs.map((workspaceDir) => NodePath.join(workspaceDir, "node_modules")),
38 ...workspaceDirs.map((workspaceDir) => NodePath.join(workspaceDir, ".vite-plus")),
39 ...childDirectories(rootDir, "apps").flatMap((appDir) => [
40 NodePath.join(appDir, "dist"),
41 NodePath.join(appDir, "dist-electron"),
42 ]),
43 ...childDirectories(rootDir, "packages").map((packageDir) => NodePath.join(packageDir, "dist")),
44 ];
45}
46
47export function cleanRepository(rootDir) {
48 const removed = [];
49 for (const target of collectCleanTargets(rootDir)) {
50 if (!NodeFS.existsSync(target)) continue;
51 NodeFS.rmSync(target, DEFAULT_REMOVE_OPTIONS);
52 removed.push(target);
53 }
54 return removed;
55}
56
57const scriptPath = fileURLToPath(import.meta.url);
58const repoRoot = NodePath.resolve(NodePath.dirname(scriptPath), "..");
59
60if (process.argv[1] && NodePath.resolve(process.argv[1]) === scriptPath) {
61 const removed = cleanRepository(repoRoot);
62 process.stdout.write(
63 `Cleaned ${removed.length} generated/dependency director${removed.length === 1 ? "y" : "ies"}.\n`,
64 );
65}