Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Sep 9, 2017
1 parent 817c5ea commit e9da9b6
Show file tree
Hide file tree
Showing 4 changed files with 559 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
printWidth: 120
semi: false
singleQuote: true
trailingComma: "all"
47 changes: 21 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let read = {
raw: (ask, maskAfter) => {
// masking isn't available without setRawMode
if (!stdin.setRawMode || process.env.TERM === 'dumb') return read.notty(ask)
return new Promise(function (resolve, reject) {
return new Promise(function(resolve, reject) {
const ansi = require('ansi-escapes')

let input = ''
Expand All @@ -19,15 +19,9 @@ let read = {
stdin.resume()
stdin.setRawMode(true)

function stop () {
function stop() {
if (maskAfter) {
stderr.write(
ansi.cursorHide +
ansi.cursorLeft +
ask +
input.replace(/./g, '*') +
'\n' +
ansi.cursorShow)
stderr.write(ansi.cursorHide + ansi.cursorLeft + ask + input.replace(/./g, '*') + '\n' + ansi.cursorShow)
} else {
stderr.write('\n')
}
Expand All @@ -36,30 +30,30 @@ let read = {
stdin.pause()
}

function enter () {
function enter() {
if (input.length === 0) return
stop()
resolve(input)
}

function ctrlc () {
function ctrlc() {
reject(new Error('SIGINT'))
stop()
}

function backspace () {
function backspace() {
if (input.length === 0) return
input = input.substr(0, input.length - 1)
stderr.write(ansi.cursorBackward(1))
stderr.write(ansi.eraseEndLine)
}

function newchar (c) {
function newchar(c) {
input += c
stderr.write(maskAfter ? c : '*'.repeat(c.length))
}

let fn = function (c) {
let fn = function(c) {
switch (c) {
case '\u0004': // Ctrl-d
case '\r':
Expand All @@ -80,16 +74,15 @@ let read = {
return new Promise((resolve, reject) => {
const spawn = require('cross-spawn')
stderr.write(ask)
let output = spawn.sync('sh',
['-c', 'read -s PASS && echo $PASS'], {
stdio: ['inherit', 'pipe', 'inherit'],
encoding: 'utf8'
})
let output = spawn.sync('sh', ['-c', 'read -s PASS && echo $PASS'], {
stdio: ['inherit', 'pipe', 'inherit'],
encoding: 'utf8',
})
stderr.write('\n')
if (output.error) return reject(output.error)
resolve(output.stdout.trim())
})
}
},
}

/**
Expand All @@ -104,13 +97,15 @@ let read = {
* @param {string} [options.method=mask] - mask or hide
* @returns {Promise<string>} input from user
*/
function prompt (ask, options) {
options = Object.assign({
method: 'mask'
}, options)
function prompt(ask, options) {
options = Object.assign(
{
method: 'mask',
},
options,
)
stdin.setEncoding('utf8')
return read[options.method](ask)
.then(input => input || prompt(ask))
return read[options.method](ask).then(input => input || prompt(ask))
}

module.exports = prompt
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"cross-spawn": "^5.1.0"
},
"devDependencies": {
"jsdoc-to-markdown": "^3.0.0"
"husky": "^0.14.3",
"jsdoc-to-markdown": "^3.0.0",
"lint-staged": "^4.1.3",
"prettier": "^1.6.1"
},
"files": [
"index.js"
Expand All @@ -24,8 +27,15 @@
"license": "WTFPL",
"main": "index.js",
"repository": "https://github.com/dickeyxxx/password-prompt",
"lint-staged": {
"**/*.js": [
"prettier --write",
"git add"
]
},
"scripts": {
"docs": "jsdoc2md -t README.hbs index.js > README.md; echo",
"precommit": "lint-staged",
"version": "npm run docs && git add README.md"
}
}
Loading

0 comments on commit e9da9b6

Please sign in to comment.