Skip to content

Commit 6064106

Browse files
committed
chore: add filter option to unzip
1 parent 3911dd4 commit 6064106

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

@iconify/tools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "module",
44
"description": "Collection of functions for cleaning up and parsing SVG for Iconify project",
55
"author": "Vjacheslav Trushkin",
6-
"version": "5.0.0-beta.2",
6+
"version": "5.0.0-beta.3",
77
"publishConfig": {
88
"tag": "next"
99
},

@iconify/tools/src/download/github/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import type { DocumentNotModified } from '../types/modified';
77
import { getGitHubRepoHash } from './hash';
88
import type { GitHubAPIOptions } from './types';
99
import { downloadFile } from '../api/download';
10-
import { unzip } from '../helpers/unzip';
10+
import { unzip, type UnzipFilterCallback } from '../helpers/unzip';
1111
import type { DownloadSourceMixin } from '../types/sources';
1212

1313
interface IfModifiedSinceOption {
1414
// Download only if it was modified since hash
1515
ifModifiedSince: string | DownloadGitHubRepoResult;
16+
17+
// Filter files
18+
filter?: UnzipFilterCallback;
1619
}
1720

1821
/**
@@ -72,7 +75,7 @@ async function findMatchingDirs(
7275
* Download GitHub repo using API
7376
*/
7477
export async function downloadGitHubRepo<
75-
T extends IfModifiedSinceOption & DownloadGitHubRepoOptions
78+
T extends IfModifiedSinceOption & DownloadGitHubRepoOptions,
7679
>(options: T): Promise<DownloadGitHubRepoResult | DocumentNotModified>;
7780
export async function downloadGitHubRepo(
7881
options: DownloadGitHubRepoOptions
@@ -89,8 +92,8 @@ export async function downloadGitHubRepo(
8992
typeof ifModifiedSince === 'string'
9093
? ifModifiedSince
9194
: ifModifiedSince.downloadType === 'github'
92-
? ifModifiedSince.hash
93-
: null;
95+
? ifModifiedSince.hash
96+
: null;
9497
if (hash === expectedHash) {
9598
return 'not_modified';
9699
}
@@ -110,7 +113,7 @@ export async function downloadGitHubRepo(
110113
try {
111114
const stat = await fs.stat(archiveTarget);
112115
exists = stat.isFile();
113-
} catch (err) {
116+
} catch {
114117
//
115118
}
116119

@@ -156,14 +159,14 @@ export async function downloadGitHubRepo(
156159
force: true,
157160
recursive: true,
158161
});
159-
} catch (err) {
162+
} catch {
160163
//
161164
}
162165
}
163166
}
164167

165168
// Unpack it
166-
await unzip(archiveTarget, rootDir);
169+
await unzip(archiveTarget, rootDir, options.filter);
167170

168171
// Get actual dir
169172
const matchingDirs = await findMatchingDirs(rootDir, hash);

@iconify/tools/src/download/gitlab/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import type { DocumentNotModified } from '../types/modified';
77
import { getGitLabRepoHash } from './hash';
88
import { defaultGitLabBaseURI, GitLabAPIOptions } from './types';
99
import { downloadFile } from '../api/download';
10-
import { unzip } from '../helpers/unzip';
10+
import { unzip, type UnzipFilterCallback } from '../helpers/unzip';
1111
import type { DownloadSourceMixin } from '../types/sources';
1212

1313
interface IfModifiedSinceOption {
1414
// Download only if it was modified since hash
1515
ifModifiedSince: string | DownloadGitLabRepoResult;
16+
17+
// Filter files
18+
filter?: UnzipFilterCallback;
1619
}
1720

1821
/**
@@ -72,7 +75,7 @@ async function findMatchingDirs(
7275
* Download GitLab repo using API
7376
*/
7477
export async function downloadGitLabRepo<
75-
T extends IfModifiedSinceOption & DownloadGitLabRepoOptions
78+
T extends IfModifiedSinceOption & DownloadGitLabRepoOptions,
7679
>(options: T): Promise<DownloadGitLabRepoResult | DocumentNotModified>;
7780
export async function downloadGitLabRepo(
7881
options: DownloadGitLabRepoOptions
@@ -89,8 +92,8 @@ export async function downloadGitLabRepo(
8992
typeof ifModifiedSince === 'string'
9093
? ifModifiedSince
9194
: ifModifiedSince.downloadType === 'gitlab'
92-
? ifModifiedSince.hash
93-
: null;
95+
? ifModifiedSince.hash
96+
: null;
9497
if (hash === expectedHash) {
9598
return 'not_modified';
9699
}
@@ -110,7 +113,7 @@ export async function downloadGitLabRepo(
110113
try {
111114
const stat = await fs.stat(archiveTarget);
112115
exists = stat.isFile();
113-
} catch (err) {
116+
} catch {
114117
//
115118
}
116119

@@ -156,14 +159,14 @@ export async function downloadGitLabRepo(
156159
force: true,
157160
recursive: true,
158161
});
159-
} catch (err) {
162+
} catch {
160163
//
161164
}
162165
}
163166
}
164167

165168
// Unpack it
166-
await unzip(archiveTarget, rootDir);
169+
await unzip(archiveTarget, rootDir, options.filter);
167170

168171
// Get actual dir
169172
const matchingDirs = await findMatchingDirs(rootDir, hash);

@iconify/tools/src/download/helpers/unzip.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ import { unzip as unzipAsync } from 'fflate';
22
import { readFile, mkdir, writeFile } from 'node:fs/promises';
33
import { dirname, join, normalize } from 'pathe';
44

5+
export type UnzipFilterCallback = (file: string) => boolean;
6+
57
/**
68
* Unzip archive
79
*/
8-
export async function unzip(source: string, path: string): Promise<void> {
10+
export async function unzip(
11+
source: string,
12+
path: string,
13+
filter?: UnzipFilterCallback
14+
): Promise<void> {
915
const dir = normalize(path);
1016
const data = await readFile(source);
1117

@@ -15,8 +21,12 @@ export async function unzip(source: string, path: string): Promise<void> {
1521
): Promise<void> {
1622
const createdDirs = new Set<string>();
1723
for (const name in data) {
18-
// const content = data[name];
1924
const filePath = normalize(join(dir, name));
25+
if (filter && !filter(filePath)) {
26+
continue;
27+
}
28+
29+
// Check path
2030
if (
2131
filePath.startsWith('/') ||
2232
filePath.includes('..') ||

0 commit comments

Comments
 (0)