Created
July 7, 2016 16:31
-
-
Save tancredi/b7d2716b6328300f67a81e023bfdb936 to your computer and use it in GitHub Desktop.
This file contains 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
'strict mode' | |
const fs = require('fs') | |
const path = require('path') | |
/** | |
* Re-prefix utility | |
* | |
* Usage: re-prefix [ dir ] [ prefix ] [ new_prefix ] | |
* E.g. re-prefix ./icons soc- social-icon- | |
*/ | |
var dir | |
var prefix | |
var newPrefix | |
init() | |
function init() { | |
if (process.argv.length < 5) { return showUsage() } | |
dir = process.argv[2] | |
prefix = process.argv[3] | |
newPrefix = process.argv[4] | |
renameFiles() | |
} | |
function renameFiles() { | |
var files = fs.readdirSync(dir) | |
var count = 0 | |
files | |
.filter(filename => { | |
return filename.substr(0, prefix.length) === prefix | |
}) | |
.map(filename => { | |
var oldPath = path.resolve(dir, filename) | |
var newFilename = newPrefix + filename.substr(prefix.length) | |
var newPath = path.resolve(dir, newFilename) | |
fs.renameSync(oldPath, newPath) | |
count++ | |
}) | |
console.log(`Done (${ count }) processed`) | |
} | |
function showUsage() { | |
console.log('Usage: re-prefix [ dir ] [ prefix ] [ new_prefix ]') | |
console.log('E.g. re-prefix ./icons soc- social-icon-') | |
process.exit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment