Files
lucide/scripts/rename/renamePattern.mts
Eric Fennis 3e644fda2d chore(scripts): Refactor scripts to typescript (#3316)
* Adjust typescript types

* adjust types

* fix types in all helper files

* Fix types

* Migrate js files to ts files

* Refactor to TS files

* Rename extentions

* Adjust imports

* Fix builds

* Update lockfile

* Fix last typescript migration

* Fix entry path @lucide/outline-svg

* Fix types

* add checkout step

* format files

* Format files
2025-06-18 15:47:24 +02:00

67 lines
2.1 KiB
TypeScript

import path from 'path';
import { getCurrentDirPath, readSvgDirectory } from '../../tools/build-helpers/helpers.ts';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import { renameIcon } from './renameIcon.function.mts';
import { type Arguments } from 'yargs';
async function main() {
const currentDir = getCurrentDirPath(import.meta.url);
const ICONS_DIR = path.resolve(currentDir, '../../icons');
const svgFiles = await readSvgDirectory(ICONS_DIR);
const iconNames = svgFiles.map((icon) => icon.split('.')[0]).reverse();
const argv = (yargs(hideBin(process.argv))
// @ts-ignore
.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()) as unknown as Arguments<{
pattern: string;
replacement: string;
dryRun: boolean;
addAlias: boolean;
}>;
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) {
if(err instanceof Error) {
console.error(err.message);
} else {
console.error('An unexpected error occurred:', err);
}
}
}
}
main();