Created
July 23, 2019 11:19
-
-
Save NoTimeForHero/44d1d991020656750b9d1c20dd2fac7e to your computer and use it in GitHub Desktop.
Script to recursive change file line endings (from CLRF to LR)
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 commander = require('commander'); | |
const colors = require('colors'); | |
const replace = require('replace-in-file'); | |
const fs = require('fs'); | |
const util = require('util'); | |
let path = null; | |
commander | |
.version('0.1.0') | |
.arguments('<filename>') | |
.action(f => path = f) | |
// TODO: Implement new conversions | |
//.option('-f, --from [type]', 'Input line ending', 'clrf') | |
//.option('-t, --to [type]', 'Output line ending', 'lf') | |
.parse(process.argv); | |
if (process.argv.length <= 2) { | |
commander.outputHelp(); | |
return; | |
} | |
const is_dir = (path) => fs.lstatSync(path).isDirectory(); | |
const exit = (message, {color='red',code=1}={}) => { | |
console.error(colors[color](message)); | |
process.exit(code); | |
} | |
if (!path) exit('No input file! Exit.'); | |
if (!fs.existsSync(path)) exit(`Path "${path}" is not exists!`); | |
if (is_dir(path) && !path.endsWith('/**')) path += '/**'; | |
const config = { | |
files: path, | |
from: /\r\n/g, | |
to: '\n', | |
countMatches: true | |
}; | |
const results = replace.sync(config); | |
const changed = results.filter(x => x.hasChanged); | |
results.forEach(result => { | |
const line = util.format( | |
'File: %s (%s/%s)', | |
colors.green(result.file), | |
colors.yellow(result.numReplacements), | |
colors.yellow(result.numMatches) | |
); | |
console.log(line); | |
}); | |
console.log(); | |
console.log(util.format('Files changed/total: %s/%s', colors.yellow(changed.length), colors.yellow(results.length))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment