Skip to content

Commit

Permalink
feat: added renameIcon script (lucide-icons#1630)
Browse files Browse the repository at this point in the history
* feat: added renameIcon script

* feat: added renameIcon script

* chore: converted renameIcon.sh to mjs

* refactor: cleanup

* Delete scripts/renameIcon.sh
  • Loading branch information
jguddas authored Dec 16, 2023
1 parent 2693da3 commit e7abba5
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions scripts/renameIcon.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import simpleGit from 'simple-git';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';

const oldName = path.basename(process.argv[2]).replace(/\.[^/.]+$/, '');
const newName = path.basename(process.argv[3]).replace(/\.[^/.]+$/, '');

if (!newName || !oldName) {
console.error('Usage: node ./scripts/renameIcon.mjs <oldIcon> <newIcon>');
process.exit(1);
}
if (oldName === newName) {
console.error('ERROR: Old name and new name are the same');
process.exit(1);
}

const git = simpleGit();

async function main() {
try {
if (await fileExists(`icons/${newName}.svg`)) {
console.error(`ERROR: Icon icons/${newName}.svg already exists`);
process.exit(1);
}
if (await fileExists(`icons/${newName}.json`)) {
console.error(`ERROR: Metadata file icons/${newName}.json already exists`);
process.exit(1);
}
if (!(await fileExists(`icons/${oldName}.svg`))) {
console.error(`ERROR: Icon icons/${oldName}.svg doesn't exist`);
process.exit(1);
}
if (!(await fileExists(`icons/${oldName}.json`))) {
console.error(`ERROR: Metadata file icons/${oldName}.json doesn't exist`);
process.exit(1);
}

await git.mv(`icons/${oldName}.svg`, `icons/${newName}.svg`);
await git.mv(`icons/${oldName}.json`, `icons/${newName}.json`);
const json = fs.readFileSync(`icons/${newName}.json`, 'utf8');
const jsonData = JSON.parse(json);
if (Array.isArray(jsonData.aliases)) {
jsonData.aliases.push(oldName);
} else {
jsonData.aliases = [oldName];
}
fs.writeFileSync(`icons/${newName}.json`, JSON.stringify(jsonData, null, 2));
await git.add(`icons/${newName}.json`);

console.log('SUCCESS: Next steps:');
console.log(`git checkout -b rename/${oldName}-to-${newName};`);
console.log(`git commit -m 'Renamed ${oldName} to ${newName}';`);
console.log(`gh pr create --title 'Renamed ${oldName} to ${newName}';`);
console.log('git checkout main;');
} catch (err) {
console.error(err.message);
process.exit(1);
}
}

async function fileExists(filePath) {
try {
await promisify(fs.access)(filePath);
return true;
} catch {
return false;
}
}

main();

0 comments on commit e7abba5

Please sign in to comment.