Skip to content

Commit

Permalink
fix json output
Browse files Browse the repository at this point in the history
  • Loading branch information
ABevier authored and mxcl committed Apr 19, 2023
1 parent e55578c commit b62710e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/hooks/useDownload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { crypto, toHashString } from "deno/crypto/mod.ts"
import useLogger, { Logger, teal, gray } from "./useLogger.ts"
import useLogger, { Logger, teal, gray, logJSON } from "./useLogger.ts"
import { chuzzle, error, TeaError } from "utils"
import { usePrefix, useFetch } from "hooks"
import { isString } from "is_what"
Expand Down Expand Up @@ -69,7 +69,7 @@ async function internal<T>({ src, headers, logger, dst }: DownloadOptions): Prom
if (!json) {
logger.replace(txt)
} else {
console.error({status: "downloading"})
logJSON({status: "downloading"})
}

const reader = rsp.body ?? error.panic()
Expand All @@ -87,7 +87,7 @@ async function internal<T>({ src, headers, logger, dst }: DownloadOptions): Prom
transform: (buf, controller) => {
n += buf.length
if (json) {
console.error({status: "downloading", "received": n, "content-size": sz })
logJSON({status: "downloading", "received": n, "content-size": sz })
} else if (!sz) {
(logger as Logger).replace(`${txt} ${pretty_size(n)}`)
} else {
Expand All @@ -107,7 +107,7 @@ async function internal<T>({ src, headers, logger, dst }: DownloadOptions): Prom
}
case 304:
if (json) {
console.error({status: "downloaded"})
logJSON({status: "downloaded"})
} else {
logger.replace(`cache: ${teal('hit')}`)
}
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export default async function(err: Error) {
const { silent, debug, json } = useConfig()

if (err instanceof ExitError) {
if (json) console.error({ error: true })
if (json) logger.logJSON({ error: true })
return err.code
} else if (err instanceof TeaError) {
if (json) {
console.error({ error: true, message: msg(err) })
logger.logJSON({ error: true, message: msg(err) })
} else if (!silent) {
const suggestion = await suggestions(err).swallow()
console.error(`${logger.red('error')}: ${err.title()} (${logger.gray(err.code())})`)
Expand All @@ -68,7 +68,7 @@ export default async function(err: Error) {
const code = chuzzle(parseInt(err.code().match(/\d+$/)?.[0] ?? '1')) ?? 1
return code
} else if (json) {
console.error({ error: true, message: err.message })
logger.logJSON({ error: true, message: err.message })
} else if (!silent) {
const { stack, message } = err ?? {}

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useExec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PackageSpecification, Installation, PackageRequirement } from "types"
import { hydrate, resolve, install as base_install, link } from "prefab"
import { VirtualEnv } from "./useVirtualEnv.ts"
import { flatten } from "./useShellEnv.ts"
import useLogger from "./useLogger.ts"
import useLogger, { logJSON } from "./useLogger.ts"
import { pkg as pkgutils, TeaError } from "utils"
import * as semver from "semver"
import Path from "path"
Expand Down Expand Up @@ -118,7 +118,7 @@ async function install(pkgs: PackageSpecification[], update: boolean) {
if (!json) {
logger.replace("resolving package graph")
} else {
console.error({ status: "resolving" })
logJSON({ status: "resolving" })
}

console.debug({hydrating: pkgs})
Expand All @@ -128,7 +128,7 @@ async function install(pkgs: PackageSpecification[], update: boolean) {
logger.clear()

if (json) {
console.error({ status: "resolved", pkgs: pending.map(pkgutils.str) })
logJSON({ status: "resolved", pkgs: pending.map(pkgutils.str) })
}

for (const pkg of pending) {
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,7 @@ export class Logger {
}
}
}

export function logJSON(data: Record<string, unknown>) {
console.error(JSON.stringify(data));
}
10 changes: 5 additions & 5 deletions src/prefab/install.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { usePrefix, useCache, useCellar, useDownload, useOffLicense, useFetch, useConfig } from "hooks"
import { host, panic, pkg as pkgutils } from "utils"
import useLogger, { Logger, red, teal, gray } from "hooks/useLogger.ts"
import useLogger, { Logger, red, teal, gray, logJSON } from "hooks/useLogger.ts"
import { Installation, StowageNativeBottle } from "types"
import { crypto, toHashString } from "deno/crypto/mod.ts"
import { Package } from "types"
Expand All @@ -21,7 +21,7 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In

const log_install_msg = (install: Installation, title = 'installed') => {
if (json) {
console.error({status: title, pkg: pkgutils.str(install.pkg)})
logJSON({status: title, pkg: pkgutils.str(install.pkg)})
} else {
const str = [
gray(usePrefix().prettyString()),
Expand All @@ -41,7 +41,7 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
if (!json) {
logger.replace(teal("locking"))
} else {
console.error({status: "locking", pkg: pkgutils.str(pkg) })
logJSON({status: "locking", pkg: pkgutils.str(pkg) })
}
const { rid } = await Deno.open(shelf.mkpath().string)
await Deno.flock(rid, true)
Expand All @@ -54,15 +54,15 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
if (!json) {
logger.replace(teal("installed"))
} else {
console.error({status: "installed", pkg: pkgutils.str(pkg) })
logJSON({status: "installed", pkg: pkgutils.str(pkg) })
}
return already_installed
}

if (!json) {
logger.replace(teal("querying"))
} else {
console.error({status: "querying", pkg: pkgutils.str(pkg) })
logJSON({status: "querying", pkg: pkgutils.str(pkg) })
}

let stream = await useDownload().stream({ src: url, logger, dst: tarball })
Expand Down

0 comments on commit b62710e

Please sign in to comment.