Last active
December 25, 2019 18:17
-
-
Save jonsamp/127e1ad21b80243bd61ee48f698e4ada to your computer and use it in GitHub Desktop.
Rename files recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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