From f2f045854bcea144628fe2c6967537c6813c75d0 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:36:44 +0100 Subject: [PATCH] fix: rewrite `then` to be an async method (#3) Rewrites the `then` method to be an async method rather than nesting a `new Promise`. --- eslint.config.js | 7 +++++- package.json | 2 +- src/main.ts | 62 ++++++++++++++++++++---------------------------- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 0d995f1..f5722da 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -10,5 +10,10 @@ export default [ ] }, eslintConfigs.recommended, - ...tseslintConfigs.strict + ...tseslintConfigs.strict, + { + rules: { + '@typescript-eslint/no-unused-vars': 'off' + } + } ]; diff --git a/package.json b/package.json index 91c10a4..c340bf3 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ ], "scripts": { "build": "tsc --noEmit", - "test": "npm run build && node --test dist/esm/test", + "test": "npm run prepare && echo \"no tests yet\"", "lint": "eslint src", "format": "prettier --write src", "format:check": "prettier --check src", diff --git a/src/main.ts b/src/main.ts index 2d809b5..a263c98 100644 --- a/src/main.ts +++ b/src/main.ts @@ -154,39 +154,36 @@ export class ExecProcess implements Result { await this._processClosed; } - public then( + public async then( onfulfilled?: ((value: Output) => TResult1 | PromiseLike) | null, _onrejected?: ((reason: unknown) => TResult2 | PromiseLike) | null - ): PromiseLike { - return new Promise(async (resolve, reject) => { - if (this._options?.stdin) { - await this._options.stdin; - } + ): Promise { + if (this._options?.stdin) { + await this._options.stdin; + } - const proc = this._process; + const proc = this._process; - if (!proc) { - reject(new Error('No process was started')); - return; - } + if (!proc) { + throw new Error('No process was started'); + } - const [stderr, stdout] = await Promise.all([ - proc.stderr && readStreamAsString(proc.stderr), - proc.stdout && readStreamAsString(proc.stdout), - this._processClosed - ]); - - const result: Output = { - stderr: stderr ?? '', - stdout: stdout ?? '' - }; - - if (onfulfilled) { - resolve(onfulfilled(result)); - } else { - resolve(result as TResult1); - } - }); + const [stderr, stdout] = await Promise.all([ + proc.stderr && readStreamAsString(proc.stderr), + proc.stdout && readStreamAsString(proc.stdout), + this._processClosed + ]); + + const result: Output = { + stderr: stderr ?? '', + stdout: stdout ?? '' + }; + + if (onfulfilled) { + return onfulfilled(result); + } else { + return result as TResult1; + } } public spawn(): void { @@ -214,14 +211,7 @@ export class ExecProcess implements Result { const {command: normalisedCommand, args: normalisedArgs} = normaliseCommandAndArgs(this._command, this._args); - let handle; - - try { - handle = spawn(normalisedCommand, normalisedArgs, nodeOptions); - } catch (err) { - // TODO (jg): handle errors - throw err; - } + const handle = spawn(normalisedCommand, normalisedArgs, nodeOptions); this._process = handle; handle.once('error', this._onError);