Skip to content

Commit 10d3d39

Browse files
authored
refactor(jszip-cli): Use async/await (#1039)
1 parent cdbb271 commit 10d3d39

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

packages/jszip-cli/src/BuildService.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('BuildService', () => {
1313
},
1414
}));
1515
vi.mock('glob', () => ({
16-
globSync: (params: string | string[]) => (typeof params === 'string' ? [params] : params),
16+
glob: async (params: string | string[]) => (typeof params === 'string' ? [params] : params),
1717
}));
1818
});
1919

@@ -33,7 +33,7 @@ describe('BuildService', () => {
3333

3434
test('adds specified files', async () => {
3535
const files = ['a.js', 'b.js'];
36-
const buildService = jsZipCLI.add(files);
36+
const buildService = await jsZipCLI.add(files);
3737

3838
addDefaultSpies(buildService);
3939

@@ -53,7 +53,7 @@ describe('BuildService', () => {
5353
verbose: false,
5454
});
5555
const files = ['a.js', 'b.js', 'b.js.map'];
56-
const buildService = jsZipCLI.add(files);
56+
const buildService = await jsZipCLI.add(files);
5757

5858
addDefaultSpies(buildService);
5959

@@ -74,7 +74,7 @@ describe('BuildService', () => {
7474
verbose: false,
7575
});
7676
const files = ['a.js', 'b.js', 'b.js.map'];
77-
const buildService = jsZipCLI.add(files);
77+
const buildService = await jsZipCLI.add(files);
7878

7979
addDefaultSpies(buildService);
8080

packages/jszip-cli/src/BuildService.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {promises as fs, Stats as fsStats} from 'node:fs';
33
import JSZip from 'jszip';
44
import logdown from 'logdown';
55
import progress from 'progress';
6-
import {globSync} from 'glob';
6+
import {glob} from 'glob';
77

88
import {FileService} from './FileService.js';
99
import {Entry, TerminalOptions} from './interfaces.js';
@@ -42,17 +42,18 @@ export class BuildService {
4242
this.compressedFilesCount = 0;
4343
}
4444

45-
public add(rawEntries: string[]): BuildService {
45+
public async add(rawEntries: string[]): Promise<BuildService> {
4646
this.logger.info(`Adding ${rawEntries.length} entr${rawEntries.length === 1 ? 'y' : 'ies'} to ZIP file.`);
4747
const normalizedEntries = this.normalizePaths(rawEntries);
48-
this.entries = globSync(normalizedEntries).map(rawEntry => {
48+
this.entries = [];
49+
for (const rawEntry of await glob(normalizedEntries)) {
4950
const resolvedPath = path.resolve(rawEntry);
5051
const baseName = path.basename(rawEntry);
51-
return {
52+
this.entries.push({
5253
resolvedPath,
5354
zipPath: baseName,
54-
};
55-
});
55+
});
56+
}
5657
return this;
5758
}
5859

packages/jszip-cli/src/JSZipCLI.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class JSZipCLI {
5151
* @param rawEntries The entries (files, directories) to add.
5252
* If not specified, entries from configuration file are used.
5353
*/
54-
public add(rawEntries?: string[]): BuildService {
54+
public add(rawEntries?: string[]): Promise<BuildService> {
5555
if (!rawEntries || !rawEntries.length) {
5656
if (this.options.entries) {
5757
rawEntries = this.options.entries;
@@ -87,7 +87,8 @@ export class JSZipCLI {
8787
throw new Error('No configuration file and no mode specified.');
8888
}
8989
if (this.options.mode === 'add') {
90-
const {outputFile, compressedFilesCount} = await this.add().save();
90+
const buildService = await this.add();
91+
const {outputFile, compressedFilesCount} = await buildService.save();
9192

9293
if (this.options.outputEntry && !this.options.quiet) {
9394
console.info(`Done compressing ${compressedFilesCount} files to "${outputFile}".`);

packages/jszip-cli/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ commander
7070
...(options.quiet && {quiet: options.quiet}),
7171
...(options.verbose && {verbose: options.verbose}),
7272
});
73-
jszip.add(entries);
73+
await jszip.add(entries);
7474
const {outputFile, compressedFilesCount} = await jszip.save();
7575

7676
if (options.output && !options.quiet) {

0 commit comments

Comments
 (0)