read args from stdin or argv

This commit is contained in:
Max Howell 2022-09-30 09:03:03 -04:00
parent 43b8edbc0f
commit 2541306eff
6 changed files with 73 additions and 20 deletions

View file

@ -18,10 +18,11 @@ args:
import { Installation } from "types"
import { useCellar, usePrefix, useFlags, useCache } from "hooks"
import { run, pkg as pkgutils } from "utils"
import { run } from "utils"
import { crypto } from "deno/crypto/mod.ts"
import { encode } from "deno/encoding/hex.ts"
import { set_output } from "./utils/gha.ts"
import * as ARGV from "./utils/args.ts"
import Path from "path"
const cellar = useCellar()
@ -36,7 +37,7 @@ if (import.meta.main) {
const checksums: string[] = []
const bottles: Path[] = []
for (const pkg of Deno.args.map(pkgutils.parse)) {
for await (const pkg of ARGV.pkgs()) {
console.log({ bottling: pkg })
const installation = await cellar.resolve(pkg)

View file

@ -13,16 +13,17 @@ args:
---*/
import { usePantry } from "hooks"
import { Installation, Package } from "types"
import { Installation } from "types"
import { pkg as pkgutils } from "utils"
import { useFlags, usePrefix } from "hooks"
import { set_output } from "./utils/gha.ts"
import build from "./build/build.ts"
import * as ARGV from "./utils/args.ts"
useFlags()
const pantry = usePantry()
const dry = Deno.args.map(pkgutils.parse)
const dry = await ARGV.toArray(ARGV.pkgs())
const gha = !!Deno.env.get("GITHUB_ACTIONS")
const group_it = gha && dry.length > 1
const rv: Installation[] = []

View file

@ -9,8 +9,8 @@ args:
- --import-map={{ srcroot }}/import-map.json
---*/
import { pkg as pkgutils } from "utils"
import { useCellar, useFlags } from "hooks"
import * as ARGV from "./utils/args.ts"
useFlags()
@ -20,7 +20,7 @@ const cellar = useCellar()
const desired_filter = !!Deno.env.get("INVERT")
const rv: string[] = []
for (const pkg of Deno.args.map(pkgutils.parse)) {
for await (const pkg of ARGV.pkgs()) {
const isInstalled = !!await cellar.has(pkg)
if (isInstalled == desired_filter) {
rv.push(pkg.project)

View file

@ -20,14 +20,12 @@ import { pkg } from "utils"
import { usePantry, useFlags } from "hooks"
import { hydrate } from "prefab"
import { PackageRequirement } from "types"
import * as ARGV from "./utils/args.ts"
const flags = useFlags()
const pantry = usePantry()
const dry = Deno.args.map(project => {
const match = project.match(/projects\/(.*)\/package.yml/)
return match ? match[1] : project
}).map(pkg.parse)
const dry = await ARGV.toArray(ARGV.pkgs())
const wet = await hydrate(dry, async (pkg, dry) => {
const deps = await pantry.getDeps(pkg)

View file

@ -13,24 +13,18 @@ args:
---*/
import { Installation, Package, PackageRequirement } from "types"
import { usePantry, useCellar, useFlags } from "hooks"
import { usePantry, useFlags } from "hooks"
import useShellEnv, { expand } from "hooks/useShellEnv.ts"
import { run, undent, pkg as pkgutils } from "utils"
import { resolve, install, hydrate, link } from "prefab"
import Path from "path"
import * as ARGV from "./utils/args.ts"
const { debug } = useFlags()
const cellar = useCellar()
const pantry = usePantry()
for (const project of Deno.args) {
const pkg = await (async () => {
const match = project.match(/projects\/(.+)\/package.yml/)
const parsable = match ? match[1] : project
const pkg = pkgutils.parse(parsable)
return await cellar.resolve(pkg)
})()
for await (const pkg of ARGV.installs()) {
await test(pkg)
}

59
scripts/utils/args.ts Normal file
View file

@ -0,0 +1,59 @@
import { Installation, Package, PackageRequirement } from "types"
import { useCellar } from "hooks"
import { parse } from "utils/pkg.ts"
/// processes Deno.args unless STDIN is not a TTY and has input
export async function *args(): AsyncGenerator<string> {
if (Deno.isatty(Deno.stdin.rid)) {
for (const arg of Deno.args) {
yield arg
}
} else {
let yielded_something = false
const buf = new Uint8Array(10)
const decode = (() => { const d = new TextDecoder(); return d.decode.bind(d) })()
let n: number | null
let txt = ''
const rx = /\s*(.*?)\s+/
while ((n = await Deno.stdin.read(buf)) !== null) {
txt += decode(buf.subarray(0, n))
while (true) {
const match = txt.match(rx)
if (!match) break
yield match[1]
txt = txt.slice(match[0].length)
yielded_something = true
}
}
if (txt) {
yield txt
} else if (!yielded_something) {
for (const arg of Deno.args) {
yield arg
}
}
}
}
export async function *pkgs(): AsyncGenerator<Package | PackageRequirement> {
for await (const arg of args()) {
const match = arg.match(/projects\/(.*)\/package.yml/)
const project = match ? match[1] : arg
yield parse(project)
}
}
export async function *installs(): AsyncGenerator<Installation> {
const cellar = useCellar()
for await (const pkg of pkgs()) {
yield await cellar.resolve(pkg)
}
}
export async function toArray<T>(input: AsyncGenerator<T>) {
const rv: T[] = []
for await (const i of input) {
rv.push(i)
}
return rv
}