All files / scripts clean.js

0% Statements 0/30
0% Branches 0/21
0% Functions 0/4
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                                                                                                                                                                                                           
import { deleteAsync } from 'del';
import path from 'path';
import { fileURLToPath } from 'url';
 
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(__dirname, '../');
 
/**
 * Group deleted files by package/directory for cleaner reporting
 */
function groupByDirectory(files, rootPath) {
  const grouped = new Map();
 
  for (const file of files) {
    const relative = path.relative(rootPath, file);
    const parts = relative.split(path.sep);
 
    // Group by package or top-level directory
    const topLevel =
      parts[0] === 'packages' && parts.length > 1
        ? `${parts[0]}/${parts[1]}` // e.g., packages/sds-subsystems
        : parts[0]; // e.g., storybook-static
 
    if (!grouped.has(topLevel)) {
      grouped.set(topLevel, []);
    }
    grouped.get(topLevel).push(file);
  }
 
  return grouped;
}
 
/**
 * Removes all build artifacts from the monorepo
 */
async function clean() {
  const dryRun = process.argv.includes('--dry-run');
 
  console.log(`๐Ÿงน Cleaning build artifacts...${dryRun ? ' (dry run)' : ''}\n`);
 
  // Patterns to clean (relative to root)
  const patterns = [
    // Root-level build artifacts
    'storybook-static',
    'coverage',
    'reports',
    'tmp',
    'dist',
    '__lwr_cache__',
    '.www',
    '__generated_site*',
 
    // Package-level build artifacts (all packages)
    'packages/*/dist',
    'packages/*/build',
    'packages/*/.build',
    'packages/*/__lwr_cache__',
    'packages/*/coverage',
    'packages/*/reports',
    'packages/*/tmp',
 
    // UIF packages build artifacts
    'packages/fds-uif/*/dist',
    'packages/fds-uif/*/build',
    'packages/fds-uif/*/.build',
    'packages/fds-uif/*/coverage',
 
    // Package-specific generated public folders (NOT source folders)
    'packages/sds-subsystems/public',
    'packages/sds-components/public',
    'packages/design-system/public',
  ];
 
  // Explicitly ignore patterns (safety)
  const ignorePatterns = ['**/node_modules/**', '**/.git/**', '**/src/**', '**/.yarn/**'];
 
  const deleted = await deleteAsync(patterns, {
    cwd: root,
    dryRun,
    ignore: ignorePatterns,
    onProgress: ({ deletedCount, totalCount }) => {
      if (!dryRun && deletedCount > 0 && deletedCount % 100 === 0) {
        process.stdout.write(`  Deleted ${deletedCount}/${totalCount} items...\r`);
      }
    },
  });
 
  if (deleted.length === 0) {
    console.log('  Nothing to clean - already clean! ๐ŸŽ‰');
    return;
  }
 
  // Group by directory for cleaner output
  const grouped = groupByDirectory(deleted, root);
 
  // Print summary
  console.log('');
  for (const [dir, files] of grouped) {
    console.log(`  โœ“ ${dryRun ? 'Would remove' : 'Removed'}: ${dir} (${files.length} items)`);
  }
 
  console.log(`\nโœจ ${dryRun ? 'Would clean' : 'Cleanup complete!'}`);
  console.log(`   ${deleted.length} items ${dryRun ? 'to remove' : 'removed'}`);
}
 
clean().catch((error) => {
  console.error('โŒ Error during cleanup:', error);
  process.exit(1);
});