diff --git a/functions/get-input.js b/functions/get-input.js index 37f8f8e..024228c 100644 --- a/functions/get-input.js +++ b/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")