Skip to content

Commit

Permalink
if there's nothing being piped in, try for a package-lock in cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Feb 21, 2019
1 parent de42224 commit 27dab8e
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions functions/get-input.js
@@ -1,10 +1,37 @@
import {createReadStream} from "fs"
import path from "path"
import fs from "fs"
import {promisify} from "util"

export default async function getInput ({file = "-"}) {
let stream = file == "-"
? process.stdin
: createReadStream(path.resolve(file))
async function checkPathIsRegularFile (path) {
return promisify(fs.stat)(path)
.then(stats => stats.isFile())
.catch(() => false)
}

async function getStream ({file, isTTY}) {
if (file != null && file != "-") {
let filepath = path.resolve(file)
if (!await checkPathIsRegularFile(filepath)) {
throw new Error(`not a file: "${filepath}"`)
}
return createReadStream(filepath)
}

let packageLockPath = "package-lock.json"

let packageLockExists = await checkPathIsRegularFile(packageLockPath)

if (!file && isTTY && packageLockExists) {
return createReadStream(packageLockPath)
}

return process.stdin
}

export default async function getInput ({file}) {
let {isTTY} = process.stdin
let stream = await getStream({file, isTTY})

stream.resume()
stream.setEncoding("utf-8")
Expand Down

0 comments on commit 27dab8e

Please sign in to comment.