Rename Script

The rename.js script enforces clean, readable filenames across every icon in the set. It strips legacy prefixes, removes branding terms, and ensures consistent kebab-case outputs for improved diffs and semantic organization.

What It Does

  • Recursively walks through every subdirectory of the aws-icons folder.
  • Cleans directory and file names using regex-based normalization.
  • Strips redundant size suffixes (like _48) and branding tokens like AWS or Amazon.
  • Applies dry-run preview mode for CI/CD testing or audit use.

Cleaning Logic

The core logic behind this script is centralized in the cleanName() function, which applies a set of chained regex transformations to isolate clean, readable names.

function cleanName(name, isFile) {
  let ext  = "";
  let base = name;

  if (isFile) {
    ext  = path.extname(name);
    base = path.basename(name, ext);
  }

  base = base
    .replace(/^(?:Arch[-_]Category[-_]|Arch[-_]|Res[-_])/i, "")
    .replace(/Amazon|AWS/gi, "")
    .replace(/_(?:48|32)$/i, "")
    .replace(/[-_]{2,}/g, "_")
    .replace(/^[-_]+|[-_]+$/g, "");

  return isFile ? `${base}${ext}` : base;
}

This function is reused across both file and directory names, ensuring consistency throughout the tree.

Safe Rename Logic

The script includes robust safeguards against overwrites. If a target name already exists, the rename is skipped and a warning is logged.

async function renameSafe(parent, oldName, newName) {
  const src = path.join(parent, oldName);
  const dst = path.join(parent, newName);
  if (src === dst) return;

  if (cfg.dryRun) {
    console.log(colour("cyan", `DRY  ${oldName} -> ${newName}`));
    return;
  }

  if (fsc.existsSync(dst)) {
    console.warn(colour("yellow", `Warning: exists, skipping ${newName}`));
    return;
  }

  await fs.rename(src, dst);
  console.log(colour("green", `Renamed ${oldName} -> ${newName}`));
}

Recursive Execution

The script performs a depth-first traversal of the icon tree. Directories are renamed after their children to avoid path mutation during traversal.

async function processDir(dir) {
  const entries = await fs.readdir(dir, { withFileTypes: true });

  for (const entry of entries) {
    const full = path.join(dir, entry.name);
    if (entry.isDirectory()) {
      await processDir(full);
      await renameSafe(dir, entry.name, cleanName(entry.name, false));
    } else {
      await renameSafe(dir, entry.name, cleanName(entry.name, true));
    }
  }
}

Flags and Execution

  • --root specifies the input directory (default: aws-icons).
  • --dry-run logs changes without modifying the file system.

This tool is particularly useful before git commits or in CI environments to enforce naming standards.