Skip to content

Commit

Permalink
Improve read from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 22, 2017
1 parent 4a7f89a commit ae69c61
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const regexgen = require('regexgen')
const getStdin = require('get-stdin')
const path = require('path')

require('update-notifier')({pkg: pkg}).notify()
require('update-notifier')({pkg}).notify()

const cli = require('meow')({
pkg: pkg,
Expand All @@ -17,20 +17,26 @@ function parseJSON (str) {
try {
return JSON.parse(str)
} catch (err) {

}
}

getStdin().then((stdin) => {
const input = parseJSON(stdin) || stdin || cli.input
const flags = Object.keys(cli.flags).join('')

if (!input.length) {
cli.showHelp()
getStdin()
.then(stdin => stdin.substring(0, stdin.length - 1))
.then(stdin => parseJSON(stdin) || stdin || cli.input)
.then(input => [].concat(input))
.then(input => {
if (!input.length) {
cli.showHelp()
process.exit(1)
}

const flags = Object.keys(cli.flags).join('')
const output = regexgen([].concat(input), flags)

process.stdout.write(output.toString())
process.exit(0)
})
.catch(err => {
console.log(err)
process.exit(1)
}

const output = regexgen(input, flags)
process.stdout.write(output.toString())
process.exit(0)
})
})

0 comments on commit ae69c61

Please sign in to comment.