Skip to content

Instantly share code, notes, and snippets.

@mcasimir
Created January 9, 2024 19:04
Show Gist options
  • Save mcasimir/fed9123c9240496040fcaaa59c53280c to your computer and use it in GitHub Desktop.
Save mcasimir/fed9123c9240496040fcaaa59c53280c to your computer and use it in GitHub Desktop.
Rename hadron to compass
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const _ = require('lodash');
const fsExtra = require('fs-extra');
const fileList = execSync('git ls-files').toString().trim().split('\n')
function removeDuplicateDebugEntries(text) {
if (!text.includes("compass*")) {
return text;
}
const pattern = /(\b[a-z-]*\*,?)+/gi;
return text.replace(pattern, (match) => {
let items = match.split(',').filter(item => item);
let uniqueItems = Array.from(new Set(items));
return uniqueItems.join(',');
});
}
// 1. Replace contents:
for (const file of fileList) {
const replacements = [
['hadron', 'compass'],
['Hadron', 'Compass'],
['HADRON', 'COMPASS']
];
const oldContent = fs.readFileSync(file, 'utf-8');
let newContent = oldContent;
for (const [from, to] of replacements) {
newContent = newContent.split(from).join(to);
}
newContent = removeDuplicateDebugEntries(newContent);
if (oldContent !== newContent) {
fs.writeFileSync(file, newContent);
}
}
// 2. Rename files:
const allSubPaths = fileList.flatMap(f => {
const paths = [];
const parts = f.split(path.sep);
while (parts.length) {
paths.push(path.join(...parts));
parts.pop()
}
return paths;
});
const uniqueSubPaths = Array.from(new Set(allSubPaths));
const sortedList = _.sortBy(uniqueSubPaths, (f) => {
return f.split(path.sep).length
}, (p) => p).reverse();
const replacements = sortedList.map(p => {
const basename = path.basename(p);
const dirname = path.dirname(p);
const newFile = basename.replace('hadron', 'compass');
return [p, path.join(dirname, newFile)];
}).filter(([f, t]) => f !== t);
fsExtra.removeSync('packages/hadron-build/test/fixtures/compass-app');
for (const [from, to] of replacements) {
console.log({from, to});
fsExtra.moveSync(from, to, {
overwrite: true
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment