Skip to content

Instantly share code, notes, and snippets.

@AmrAbdulrahman
Last active February 1, 2017 09:51
Show Gist options
  • Save AmrAbdulrahman/70d0a602f1d952d41e3a47866f6c6d65 to your computer and use it in GitHub Desktop.
Save AmrAbdulrahman/70d0a602f1d952d41e3a47866f6c6d65 to your computer and use it in GitHub Desktop.
/*
In case you have tens of files with '2 spaces' indentation that you need to convert to '4 spaces'
1. Install 'recursive-readdir-sync', do not add the --save label.
2. cd to the root of the directory you want to process
3. Place this script.
3. Run the script, commit, push, enjoy!
*/
let fs = require('fs');
let recursiveReadSync = require('recursive-readdir-sync');
let files = recursiveReadSync(__dirname);
let extentions = [
'js',
'less',
'html'
].map((extention) => '.' + extention);
let processedFilesCount = 0;
for (let i = 0; i < files.length; i++) {
let filePath = files[i];
let processFile = false;
for (let j = 0; j < extentions.length; j++) {
let extention = extentions[j];
if (filePath.indexOf(extention) === filePath.length - extention.length) { // ends with
processFile = true;
break;
}
}
if (processFile) {
processedFilesCount ++;
convert2SpacesTo4Spaces(filePath);
}
}
console.log(`Successfully processed ${processedFilesCount} file.`);
function convert2SpacesTo4Spaces(filePath) {
let fileContent = fs.readFileSync(filePath);
fileContent = fileContent.toString().replace(/ /g, ' ');
fs.writeFileSync(filePath, fileContent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment