Skip to content

Commit

Permalink
Updated readme, code cleanup and added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jurosh committed Jun 28, 2017
1 parent d82af25 commit 4fdd8d9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ Run conversions (cannot be undone):

```
eolConverter "**/*.js"
eolConverter "**/*.{js,jsx,ts,tsx}"
eolConverter "src/**/*.js"
eolConverter "{src,tests}/**/*.js"
```

**To CRLF (Windows default)**

```
eolConverter crlf "**/*.{js,jsx,ts,tsx}"
eolConverter crlf "**/*.js"
```

## Tips
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Run `npm start "examples/*.text"` to do CRLF to LF conversion.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "eol-converter-cli",
"version": "1.0.5",
"version": "1.0.7",
"description": "Newline converter CLI for simple CRLF -> LF (and reverse) convertions of multiple files.",
"main": "src/index.js",
"bin": {
"eolConverter": "./src/index.js"
},
"scripts": {
"eolConverter": "node src/index.js"
"start": "node src/index.js"
},
"keywords": [
"eol",
Expand Down
32 changes: 18 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,39 @@
'use strict';

const eol = require('eol')
const glob = require("glob")
const glob = require('glob')
const fs = require('fs')
const path = require('path')

const argv = process.argv;
const inputFiles = argv[argv.length - 1]
const argv = process.argv
const inputFilesGlob = argv[argv.length - 1]
const isWarmup = argv[argv.length - 2] === 'warmup'
const isCrlf = argv[argv.length - 2] === 'crlf'

const dir = process.cwd()

console.log('Running in directory ' + dir)
console.log('Files GLOB regex: ' + inputFiles)
console.log(isWarmup ? 'WARMUP: will only list files, no action will be performed - run `convertToLF` to do conversion' : '')
console.log('Files GLOB regex: ' + inputFilesGlob)
console.log(isWarmup
? 'WARMUP: will only list files, no action will be performed'
: 'Converting to ' + (isCrlf ? 'CRLF' : 'LF'))
console.log('---')

glob(inputFiles, null, function (er, files) {
glob(inputFilesGlob, null, function (er, files) {
if (!files || files.length === 0) {
return console.error('ERROR: no files found')
}
files.forEach(fileName => {
console.log(fileName)
if (!isWarmup) {
try {
const file = fs.readFileSync(path.join(dir, fileName))
const converted = isCrlf ? eol.crlf(file.toString()) : eol.lf(file.toString())
fs.writeFileSync(fileName, converted);
} catch (error) {
console.warn(error)
}
if (isWarmup) {
return;
}
try {
const fileContent = fs.readFileSync(path.join(dir, fileName)).toString()
const convertFn = (isCrlf ? eol.crlf : eol.lf).bind(eol)
fs.writeFileSync(fileName, convertFn(fileContent))
} catch (error) {
console.warn(error)
}
})
console.log('---')
Expand Down

0 comments on commit 4fdd8d9

Please sign in to comment.