Skip to content

Commit

Permalink
feat(zip): utilize bestzip instead of archiver
Browse files Browse the repository at this point in the history
  • Loading branch information
vamche committed Jun 17, 2021
1 parent 1f0ebc7 commit 7aa262d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"typescript": "^4.0.3"
},
"dependencies": {
"archiver": "^5.2.0",
"bestzip": "^2.2.0",
"chokidar": "^3.4.3",
"esbuild": ">=0.8",
"fs-extra": "^9.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export async function pack(this: EsbuildPlugin) {
}));

const startZip = Date.now();
await zip(artifactPath, filesPathList);
await zip(artifactPath, filesPathList, this.buildDirPath);

const { size } = fs.statSync(artifactPath);

Expand Down
36 changes: 7 additions & 29 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as archiver from 'archiver';
import { bestzip } from 'bestzip';
import * as childProcess from 'child_process';
import * as fs from 'fs-extra';
import * as path from 'path';
import { join } from 'ramda';
import { IFiles } from './types';
import { IFiles, IFile } from './types';

export class SpawnError extends Error {
constructor(message: string, public stdout: string, public stderr: string) {
Expand Down Expand Up @@ -92,35 +92,13 @@ export const humanSize = (size: number) => {
return `${sanitized} ${['B', 'KB', 'MB', 'GB', 'TB'][i]}`;
};

export const zip = (zipPath: string, filesPathList: IFiles) => {
export const zip = (zipPath: string, filesPathList: IFiles, workingDir?: string) => {
fs.mkdirpSync(path.dirname(zipPath));

const zip = archiver.create('zip');
const output = fs.createWriteStream(zipPath);

// write zip
output.on('open', () => {
zip.pipe(output);

filesPathList.forEach(file => {
const stats = fs.statSync(file.rootPath);
if (stats.isDirectory()) return;

zip.append(fs.readFileSync(file.rootPath), {
name: file.localPath,
mode: stats.mode,
date: new Date(0), // necessary to get the same hash when zipping the same content
});
});

zip.finalize();
});

return new Promise((resolve, reject) => {
output.on('close', () => {
resolve(zipPath);
});
zip.on('error', err => reject(err));
return bestzip({
source: filesPathList.map((file: IFile) => file.localPath),
destination: zipPath,
cwd: workingDir || process.cwd()
});
};

Expand Down

0 comments on commit 7aa262d

Please sign in to comment.