Files
lucide/scripts/rename/renamePattern.mjs
Eric Fennis 077242cfa0 refactor(scripts): Cleaning up scripts (#2092)
* cleanup scripts

* Move helpers to package

* Fixes scripts

* Fix scripts

* Formatting

* Fix helpers import paths

* Remove lucide-figma

* Rename helpers package

* Fix build

* formatting

* Adjust main build-icons file

* Add export casing

* Adds `exportModuleNameCasing` fro lab project

* format files

* Bump package version @lucide/build-icons

* Revert changes in icons

* Revert changes in PR yml

* Fix lint issues

* Fix site build

* fix lint errors

* Attempt fix linting

* Fix lint errors
2024-06-28 11:24:37 +02:00

56 lines
1.8 KiB
JavaScript

import path from 'path';
import { getCurrentDirPath, readSvgDirectory } from '@lucide/helpers';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import { renameIcon } from './renameIcon.function.mjs';
async function main() {
const currentDir = getCurrentDirPath(import.meta.url);
const ICONS_DIR = path.resolve(currentDir, '../../icons');
const svgFiles = readSvgDirectory(ICONS_DIR);
const iconNames = svgFiles.map((icon) => icon.split('.')[0]).reverse();
const argv = yargs(hideBin(process.argv))
.usage('$0 <pattern> <replacement>', 'Renames all icons matching a pattern', (yargs) => {
yargs
.positional('pattern', {
type: 'string',
demandOption: true,
describe: 'A regular expression, e.g. "^rhombus-(.+)$"',
})
.positional('replacement', {
type: 'string',
demandOption: true,
describe: 'A replacement string, e.g. "diamond-\\1"',
});
})
.strictCommands()
.options({
'dry-run': { type: 'boolean', default: false, alias: 'd' },
'add-alias': { type: 'boolean', default: true, alias: 'a' },
})
.parse();
const pattern = new RegExp(argv.pattern, 'g');
const replacement = argv.replacement.replaceAll(/\\([0-9]+)/g, (s, i) => `$${i}`);
if (!(pattern instanceof RegExp)) {
console.error(`${pattern} is not a valid regular expression.`);
process.exit(1);
}
for (const oldName of iconNames.filter((name) => pattern.test(name))) {
const newName = oldName.replaceAll(pattern, replacement);
console.log(`Renaming ${oldName} => ${newName}`);
try {
if (!argv.dryRun) {
await renameIcon(ICONS_DIR, oldName, newName, false, argv.addAlias);
}
} catch (err) {
console.error(err.message);
}
}
}
main();