diff --git a/src/core.ts b/src/core.ts index c423bc4295..cfdc00a928 100644 --- a/src/core.ts +++ b/src/core.ts @@ -515,6 +515,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() } diff --git a/test/core.test.js b/test/core.test.js index e28fa6c42a..c9be502b09 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -226,6 +226,21 @@ describe('core', () => { assert.equal((await p).toString(), 'foo\n') }) + test('ProcessPromise: implements json()', async () => { + const p = $`echo '{"key":"value"}'` + assert.deepEqual((await p).json(), { 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')