Skip to content

Commit bcf70c6

Browse files
authored
Move zipFile util to its own module for easy re-use (#1522)
1 parent 64f33a5 commit bcf70c6

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

extensions/ql-vscode/src/pure/zip.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as unzipper from 'unzipper';
2+
3+
/**
4+
* Unzips a zip file to a directory.
5+
* @param sourcePath The path to the zip file.
6+
* @param destinationPath The path to the directory to unzip to.
7+
*/
8+
export async function unzipFile(sourcePath: string, destinationPath: string) {
9+
const file = await unzipper.Open.file(sourcePath);
10+
await file.extract({ path: destinationPath });
11+
}

extensions/ql-vscode/src/remote-queries/gh-actions-api-client.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as unzipper from 'unzipper';
21
import * as path from 'path';
32
import * as fs from 'fs-extra';
43
import { showAndLogErrorMessage, showAndLogWarningMessage, tmpDir } from '../helpers';
@@ -9,6 +8,7 @@ import { DownloadLink, createDownloadPath } from './download-link';
98
import { RemoteQuery } from './remote-query';
109
import { RemoteQueryFailureIndexItem, RemoteQueryResultIndex, RemoteQuerySuccessIndexItem } from './remote-query-result-index';
1110
import { getErrorMessage } from '../pure/helpers-pure';
11+
import { unzipFile } from '../pure/zip';
1212

1313
export const RESULT_INDEX_ARTIFACT_NAME = 'result-index';
1414

@@ -110,10 +110,8 @@ export async function downloadArtifactFromLink(
110110
const response = await octokit.request(`GET ${downloadLink.urlPath}/zip`, {});
111111

112112
const zipFilePath = createDownloadPath(storagePath, downloadLink, 'zip');
113-
await saveFile(`${zipFilePath}`, response.data as ArrayBuffer);
114113

115-
// Extract the zipped artifact.
116-
await unzipFile(zipFilePath, extractedPath);
114+
await unzipBuffer(response.data as ArrayBuffer, zipFilePath, extractedPath);
117115
}
118116
return path.join(extractedPath, downloadLink.innerFilePath || '');
119117
}
@@ -300,20 +298,16 @@ async function downloadArtifact(
300298
archive_format: 'zip',
301299
});
302300
const artifactPath = path.join(tmpDir.name, `${artifactId}`);
303-
await saveFile(`${artifactPath}.zip`, response.data as ArrayBuffer);
304-
await unzipFile(`${artifactPath}.zip`, artifactPath);
301+
await unzipBuffer(response.data as ArrayBuffer, `${artifactPath}.zip`, artifactPath);
305302
return artifactPath;
306303
}
307304

308-
async function saveFile(filePath: string, data: ArrayBuffer): Promise<void> {
305+
async function unzipBuffer(data: ArrayBuffer, filePath: string, destinationPath: string): Promise<void> {
309306
void logger.log(`Saving file to ${filePath}`);
310307
await fs.writeFile(filePath, Buffer.from(data));
311-
}
312308

313-
async function unzipFile(sourcePath: string, destinationPath: string) {
314309
void logger.log(`Unzipping file to ${destinationPath}`);
315-
const file = await unzipper.Open.file(sourcePath);
316-
await file.extract({ path: destinationPath });
310+
await unzipFile(filePath, destinationPath);
317311
}
318312

319313
function getWorkflowError(conclusion: string | null): string {

0 commit comments

Comments
 (0)