Skip to content

Instantly share code, notes, and snippets.

@jonsamp
Last active December 25, 2019 18:17
Show Gist options
  • Save jonsamp/127e1ad21b80243bd61ee48f698e4ada to your computer and use it in GitHub Desktop.
Save jonsamp/127e1ad21b80243bd61ee48f698e4ada to your computer and use it in GitHub Desktop.
Rename files recursively
const path = require('path');
const fs = require('fs');
const listDir = (dir, fileList = []) => {
let files = fs.readdirSync(dir);
files.forEach(file => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = listDir(path.join(dir, file), fileList);
} else {
if (/\.test.tsx$/.test(file)) {
let src = path.join(dir, file);
const newSrc = file.replace('.test.tsx', '-test.tsx');
fileList.push({
oldSrc: src,
newSrc: path.join(dir, newSrc),
});
}
}
});
return fileList;
};
const foundFiles = listDir('./');
foundFiles.forEach(f => {
console.log(`Renamed: ${f.oldSrc} -> ${f.newSrc}`);
fs.renameSync(f.oldSrc, f.newSrc);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment