Skip to content

Commit d4c2470

Browse files
authored
Cleanup archiver warning (#1682)
Cleans up a benign (but scary) warning that states using archiver will crash when called.
1 parent b949b3c commit d4c2470

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/commands/captureDiagnostics.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { exec } from "child_process";
2222
import { Writable } from "stream";
2323
import { WorkspaceContext } from "../WorkspaceContext";
2424
import { Version } from "../utilities/version";
25-
import { execFileStreamOutput } from "../utilities/utilities";
25+
import { destructuredPromise, execFileStreamOutput } from "../utilities/utilities";
2626
import configuration from "../configuration";
2727
import { FolderContext } from "../FolderContext";
2828

@@ -101,28 +101,21 @@ function configureZipArchiver(zipFilePath: string): {
101101
done: Promise<void>;
102102
} {
103103
const output = fs.createWriteStream(zipFilePath);
104-
const archive = archiver("zip", {
105-
zlib: { level: 9 }, // Maximum compression
104+
// Create an archive with max compression
105+
const archive = archiver.create("zip", {
106+
zlib: { level: 9 },
106107
});
107-
let resolve: () => void;
108-
let reject: (error: unknown) => void;
109-
const done = new Promise<void>((res, rej) => {
110-
resolve = res;
111-
reject = rej;
108+
const { promise, resolve, reject } = destructuredPromise<void>();
109+
output.once("close", () => {
110+
archive.removeListener("error", reject);
111+
resolve();
112112
});
113-
output.on("close", async () => {
114-
try {
115-
resolve();
116-
} catch (error) {
117-
reject(error);
118-
}
119-
});
120-
121-
archive.on("error", (err: Error) => {
113+
archive.once("error", err => {
114+
output.removeListener("close", resolve);
122115
reject(err);
123116
});
124117
archive.pipe(output);
125-
return { archive, done };
118+
return { archive, done: promise };
126119
}
127120

128121
export async function promptForDiagnostics(ctx: WorkspaceContext) {

src/utilities/utilities.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,23 @@ export function regexEscapedString(string: string, omitting?: Set<string>): stri
386386
}
387387
return result;
388388
}
389+
390+
/* eslint-disable @typescript-eslint/no-explicit-any */
391+
/**
392+
* Creates a promise that can be resolved or rejected outside the promise executor.
393+
* @returns An object containing a promise, a resolve function, and a reject function.
394+
*/
395+
export function destructuredPromise<T>(): {
396+
promise: Promise<T>;
397+
resolve: (value: T) => void;
398+
reject: (reason?: any) => void;
399+
} {
400+
let resolve: (value: T) => void;
401+
let reject: (reason?: any) => void;
402+
const p = new Promise<T>((res, rej) => {
403+
resolve = res;
404+
reject = rej;
405+
});
406+
return { promise: p, resolve: resolve!, reject: reject! };
407+
}
408+
/* eslint-enable @typescript-eslint/no-explicit-any */

0 commit comments

Comments
 (0)