-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestSetup.ts
More file actions
42 lines (32 loc) · 1.21 KB
/
TestSetup.ts
File metadata and controls
42 lines (32 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import axios from 'axios';
import fs from 'fs';
import AdmZip from 'adm-zip';
async function downloadAndExtractZip(url: string, extractPath: string): Promise<void> {
if (!fs.existsSync(extractPath)) {
fs.mkdirSync(extractPath, { recursive: true });
}
const response = await axios({
url,
method: 'GET',
responseType: 'stream',
});
const zipPath = './temp.zip';
const writer = fs.createWriteStream(zipPath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => {
const zip = new AdmZip(zipPath);
// Extract the contents to the specified path
zip.extractAllTo(extractPath, /*overwrite*/ true);
// Get the name of the extracted folder (e.g., "LockDealNFT-master")
const extractedFolderName = zip.getEntries()[0].entryName.split('/')[0];
// Remove all words that start with a hyphen from the extracted folder name
const modifiedFolderName = extractedFolderName.replace(/-[^/]+/g, '');
fs.renameSync(`${extractPath}${extractedFolderName}`, `${extractPath}${modifiedFolderName}`);
fs.unlinkSync(zipPath);
resolve();
});
writer.on('error', reject);
});
}
export { downloadAndExtractZip };