Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move log to util #1019

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "zx/index",
"path": "build/*.{js,cjs}",
"limit": "803 kB",
"limit": "804 kB",
"brotli": false,
"gzip": false
},
Expand Down
234 changes: 126 additions & 108 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
"fmt": "prettier --write .",
"fmt:check": "prettier --check .",
"build": "npm run build:js && npm run build:dts && npm run build:tests",
"build:js": "node scripts/build-js.mjs --format=cjs --hybrid --entry=src/*.ts:!src/error.ts && npm run build:vendor",
"build:js": "node scripts/build-js.mjs --format=cjs --hybrid --entry=src/*.ts:!src/error.ts:!src/repl.ts && npm run build:vendor",
"build:vendor": "node scripts/build-js.mjs --format=cjs --entry=src/vendor-*.ts --bundle=all",
"build:tests": "node scripts/build-tests.mjs",
"build:dts": "tsc --project tsconfig.prod.json && rm build/error.d.ts && node scripts/build-dts.mjs",
"build:dts": "tsc --project tsconfig.prod.json && rm build/error.d.ts build/repl.d.ts && node scripts/build-dts.mjs",
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
Expand Down Expand Up @@ -104,12 +104,12 @@
"@webpod/ingrid": "^0.0.0-beta.3",
"@webpod/ps": "^0.0.0-beta.12",
"c8": "^10.1.3",
"chalk": "^5.3.0",
"chalk": "^5.4.1",
"create-require": "^1.1.1",
"depseek": "^0.4.1",
"dts-bundle-generator": "^9.5.1",
"esbuild": "^0.24.0",
"esbuild-node-externals": "^1.15.0",
"esbuild": "^0.24.2",
"esbuild-node-externals": "^1.16.0",
"esbuild-plugin-entry-chunks": "^0.1.15",
"esbuild-plugin-extract-helpers": "^0.0.6",
"esbuild-plugin-hybrid-export": "^0.3.1",
Expand Down
63 changes: 4 additions & 59 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ import {
ps,
VoidStream,
type ChalkInstance,
type RequestInfo,
type RequestInit,
type TSpawnStore,
} from './vendor-core.js'
import {
type Duration,
formatCmd,
log,
isString,
isStringLiteral,
noop,
Expand All @@ -59,6 +57,8 @@ import {
toCamelCase,
} from './util.js'

export { log, type LogEntry } from './util.js'

const CWD = Symbol('processCwd')
const SYNC = Symbol('syncExec')
const EOL = Buffer.from(_EOL)
Expand Down Expand Up @@ -805,7 +805,7 @@ export function cd(dir: string | ProcessOutput) {
dir = dir.toString().trim()
}

$.log({ kind: 'cd', dir })
$.log({ kind: 'cd', dir, verbose: !$.quiet && $.verbose })
process.chdir(dir)
$[CWD] = process.cwd()
}
Expand All @@ -826,61 +826,6 @@ export async function kill(pid: number, signal = $.killSignal) {
}
}

export type LogEntry = {
verbose?: boolean
} & (
| {
kind: 'cmd'
cmd: string
}
| {
kind: 'stdout' | 'stderr'
data: Buffer
}
| {
kind: 'cd'
dir: string
}
| {
kind: 'fetch'
url: RequestInfo
init?: RequestInit
}
| {
kind: 'retry'
error: string
}
| {
kind: 'custom'
data: any
}
)

export function log(entry: LogEntry) {
if (!(entry.verbose ?? $.verbose)) return
switch (entry.kind) {
case 'cmd':
process.stderr.write(formatCmd(entry.cmd))
break
case 'stdout':
case 'stderr':
case 'custom':
process.stderr.write(entry.data)
break
case 'cd':
process.stderr.write('$ ' + chalk.greenBright('cd') + ` ${entry.dir}\n`)
break
case 'fetch':
const init = entry.init ? ' ' + inspect(entry.init) : ''
process.stderr.write(
'$ ' + chalk.greenBright('fetch') + ` ${entry.url}${init}\n`
)
break
case 'retry':
process.stderr.write(entry.error + '\n')
}
}

const promisifyStream = <S extends Writable>(
stream: S,
from: ProcessPromise
Expand Down
12 changes: 7 additions & 5 deletions src/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function fetch(
url: RequestInfo,
init?: RequestInit
): Promise<Response> {
$.log({ kind: 'fetch', url, init })
$.log({ kind: 'fetch', url, init, verbose: !$.quiet && $.verbose })
return nodeFetch(url, init)
}

Expand Down Expand Up @@ -163,10 +163,12 @@ export async function retry<T>(
if (delayGen) delay = delayGen.next().value
$.log({
kind: 'retry',
error:
chalk.bgRed.white(' FAIL ') +
` Attempt: ${attempt}${total == Infinity ? '' : `/${total}`}` +
(delay > 0 ? `; next in ${delay}ms` : ''),
total,
attempt,
delay,
exception: err,
verbose: !$.quiet && $.verbose,
error: `FAIL Attempt: ${attempt}/${total}, next: ${delay}`, // legacy
})
lastErr = err
if (count == 0) break
Expand Down
2 changes: 1 addition & 1 deletion src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import os from 'node:os'
import path from 'node:path'
import { inspect } from 'node:util'
import { ProcessOutput, defaults } from './core.js'
import { chalk } from './vendor.js'
import { chalk } from './vendor-core.js'

export async function startRepl() {
defaults.verbose = false
Expand Down
66 changes: 65 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import os from 'node:os'
import path from 'node:path'
import fs from 'node:fs'
import { chalk } from './vendor-core.js'
import { chalk, type RequestInfo, type RequestInit } from './vendor-core.js'
import { inspect } from 'node:util'

export { isStringLiteral } from './vendor-core.js'

Expand Down Expand Up @@ -139,6 +140,69 @@ export function parseDuration(d: Duration) {
throw new Error(`Unknown duration: "${d}".`)
}

export type LogEntry = {
verbose?: boolean
} & (
| {
kind: 'cmd'
cmd: string
}
| {
kind: 'stdout' | 'stderr'
data: Buffer
}
| {
kind: 'cd'
dir: string
}
| {
kind: 'fetch'
url: RequestInfo
init?: RequestInit
}
| {
kind: 'retry'
attempt: number
total: number
delay: number
exception: unknown
error?: string
}
| {
kind: 'custom'
data: any
}
)

export function log(entry: LogEntry) {
if (!entry.verbose) return
const stream = process.stderr
switch (entry.kind) {
case 'cmd':
stream.write(formatCmd(entry.cmd))
break
case 'stdout':
case 'stderr':
case 'custom':
stream.write(entry.data)
break
case 'cd':
stream.write('$ ' + chalk.greenBright('cd') + ` ${entry.dir}\n`)
break
case 'fetch':
const init = entry.init ? ' ' + inspect(entry.init) : ''
stream.write('$ ' + chalk.greenBright('fetch') + ` ${entry.url}${init}\n`)
break
case 'retry':
stream.write(
chalk.bgRed.white(' FAIL ') +
` Attempt: ${entry.attempt}${entry.total == Infinity ? '' : `/${entry.total}`}` +
(entry.delay > 0 ? `; next in ${entry.delay}ms` : '') +
'\n'
)
}
}

export function formatCmd(cmd?: string): string {
if (cmd == undefined) return chalk.grey('undefined')
const chars = [...cmd]
Expand Down
3 changes: 0 additions & 3 deletions test/package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ describe('package', () => {
'build/index.cjs',
'build/index.d.ts',
'build/index.js',
'build/repl.cjs',
'build/repl.d.ts',
'build/repl.js',
'build/util.cjs',
'build/util.d.ts',
'build/util.js',
Expand Down
Loading