Skip to content

Commit

Permalink
refactor: extract log from core
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Dec 22, 2024
1 parent 6c30645 commit 73aa581
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 72 deletions.
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
4 changes: 2 additions & 2 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
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

0 comments on commit 73aa581

Please sign in to comment.