Skip to content

Commit

Permalink
feat request: provide formatters shortcuts google#764
Browse files Browse the repository at this point in the history
Signed-off-by: Anil Kumar Behera <[email protected]>
  • Loading branch information
Anonymous961 committed May 16, 2024
1 parent 3d24076 commit ebf8048
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
79 changes: 47 additions & 32 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
)
try {
useBash()
} catch (err) {}
} catch (err) { }

type Resolve = (out: ProcessOutput) => void

Expand Down Expand Up @@ -514,6 +514,18 @@ export class ProcessOutput extends Error {
return this._combined
}

json() {
return JSON.parse(this._combined)
}

buffer() {
return Buffer.from(this._combined, 'utf8')
}

text() {
return this._combined;
}

valueOf() {
return this._combined.trim()
}
Expand Down Expand Up @@ -543,9 +555,8 @@ export class ProcessOutput extends Error {
let message = `exit code: ${code}`
if (code != 0 || signal != null) {
message = `${stderr || '\n'} at ${from}`
message += `\n exit code: ${code}${
exitCodeInfo(code) ? ' (' + exitCodeInfo(code) + ')' : ''
}`
message += `\n exit code: ${code}${exitCodeInfo(code) ? ' (' + exitCodeInfo(code) + ')' : ''
}`
if (signal != null) {
message += `\n signal: ${signal}`
}
Expand All @@ -570,11 +581,10 @@ export class ProcessOutput extends Error {
stdout: ${stringify(this.stdout, chalk.green)},
stderr: ${stringify(this.stderr, chalk.red)},
signal: ${inspect(this.signal)},
exitCode: ${(this.exitCode === 0 ? chalk.green : chalk.red)(this.exitCode)}${
exitCodeInfo(this.exitCode)
? chalk.grey(' (' + exitCodeInfo(this.exitCode) + ')')
: ''
}
exitCode: ${(this.exitCode === 0 ? chalk.green : chalk.red)(this.exitCode)}${exitCodeInfo(this.exitCode)
? chalk.grey(' (' + exitCodeInfo(this.exitCode) + ')')
: ''
}
}`
}
}
Expand Down Expand Up @@ -602,45 +612,45 @@ export async function kill(pid: number, signal?: string) {
for (const p of children) {
try {
process.kill(+p.pid, signal)
} catch (e) {}
} catch (e) { }
}
try {
process.kill(-pid, signal)
} catch (e) {
try {
process.kill(+pid, signal)
} catch (e) {}
} catch (e) { }
}
}

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

export function log(entry: LogEntry) {
switch (entry.kind) {
Expand All @@ -651,6 +661,11 @@ export function log(entry: LogEntry) {
case 'stdout':
case 'stderr':
if (!entry.verbose) return
const eol = Buffer.from('\n', 'utf-8')

if (!entry.data.slice(-1).equals(eol)) {
entry.data = Buffer.concat([entry.data, eol])
}
process.stderr.write(entry.data)
break
case 'cd':
Expand Down
18 changes: 18 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ describe('core', () => {
assert.equal((await p).toString(), 'foo\n')
})

test('ProcessPromise: implements json()', async () => {
const p = $`echo '{"key":"value"}'`
assert.equal(
JSON.stringify((await p).json()),
JSON.stringify({ key: 'value' })
)
})

test('ProcessPromise: implements text()', async () => {
const p = $`echo foo`
assert.equal((await p).toString(), 'foo\n')
})

test('ProcessPromise: implements buffer()', async () => {
const p = $`echo foo`
assert.equal((await p).buffer().compare(Buffer.from('foo\n', 'utf-8')), 0)
})

test('ProcessPromise: implements valueOf()', async () => {
const p = $`echo foo`
assert.equal((await p).valueOf(), 'foo')
Expand Down

0 comments on commit ebf8048

Please sign in to comment.