Skip to content

Commit

Permalink
perf(zip): refactored to use another lib instead archiver
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Jul 28, 2022
1 parent 3dbc585 commit d4d06f5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 107 deletions.
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^8.0.4",
"@types/archiver": "^5.3.1",
"@types/chai": "^4",
"@types/mocha": "^9.0.0",
"@types/mock-fs": "^4.13.1",
Expand Down
92 changes: 5 additions & 87 deletions src/commands/run/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
//#region Imports

import * as fs from 'fs';
import {
createReadStream,
createWriteStream,
existsSync,
mkdirSync,
readFileSync,
statSync,
} from 'fs';
import { existsSync, mkdirSync, readFileSync, statSync } from 'fs';
import { join, relative, resolve } from 'path';
import {
DependencyInfo,
Expand All @@ -19,13 +11,12 @@ import {
import { Flags } from '@oclif/core';
import { LoadOptions } from '@oclif/core/lib/interfaces';
import rimraf from 'rimraf';
import { ZipFile } from 'yazl';
import CustomCommand from '../../common/custom-command';
import CustomError from '../../common/custom-error';
import { defaultIgnoredFileExtensions } from '../../common/extensions';
import { HeadlessOptions } from '../../common/headless';
import { OutputInfo } from '../../common/output-info';
import { ZipArtifact } from '../../common/zip';
import { FasterZip, ZipArtifact } from '../../common/zip';

//#endregion

Expand Down Expand Up @@ -485,7 +476,7 @@ export default class Run extends CustomCommand {
protected async zipDirectory(
flags: typeof Run.flags,
dir: string,
sources: ZipArtifact[],
zipArtifacts: ZipArtifact[],
outputPath: string,
): Promise<void> {
this.logMessage(flags, 'log', 'Creating the output file');
Expand All @@ -500,82 +491,9 @@ export default class Run extends CustomCommand {
}

const rootPath = resolve(process.cwd(), dir);
const fasterZip = new FasterZip();

const zipfile = new ZipFile();
const stream = createWriteStream(outputPath);

zipfile.outputStream.pipe(stream);

function readdirAndAddToZip(
source: ZipArtifact,
path: string,
callback: (err: Error | null) => void,
) {
fs.readdir(path, (err, files) => {
if (err) return callback(err);

let pending = files.length;

if (!pending) return callback(null);

files.forEach(file => {
const filePath = join(path, file);

fs.stat(filePath, (_err, stats) => {
if (_err) return callback(_err);

if (stats.isDirectory()) {
readdirAndAddToZip(source, filePath, __err => {
if (__err) return callback(__err);

pending -= 1;

if (!pending) return callback(null);
});
} else {
if (
!source.shouldIgnore ||
(source.shouldIgnore && !source.shouldIgnore(filePath))
) {
const metadataPath = relative(rootPath, filePath);
const readStream = createReadStream(filePath);

zipfile.addReadStream(readStream, metadataPath);
}

pending -= 1;

if (!pending) return callback(null);
}
});
});
});
}

for (const source of sources) {
await new Promise<void>((resolve, reject) => {
if (source.type === 'directory') {
readdirAndAddToZip(source, source.path, err => {
if (err) reject(err);
else resolve();
});
} else {
const metadataPath = relative(rootPath, source.path);
const readStream = createReadStream(source.path);

zipfile.addReadStream(readStream, metadataPath);
resolve();
}
});
}

zipfile.end();

await new Promise<void>((resolve, reject) => {
zipfile.outputStream.once('error', err => reject(err));

stream.once('error', err => reject(err)).once('close', () => resolve());
});
await fasterZip.run(rootPath, outputPath, zipArtifacts);

this.logMessage(flags, 'log', 'Creating the output file... created');
}
Expand Down
102 changes: 102 additions & 0 deletions src/common/zip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,108 @@
import fs, { createReadStream, createWriteStream } from 'fs';
import { join, relative } from 'path';
import { ZipFile } from 'yazl';

export interface ZipArtifact {
path: string;
name: string;
type: 'file' | 'directory';
shouldIgnore?: (fileName: string) => boolean;
}

export class FasterZip {
public async run(
rootPath: string,
outputPath: string,
zipArtifacts: ZipArtifact[],
): Promise<void> {
const zipfile = new ZipFile();
const stream = createWriteStream(outputPath);

zipfile.outputStream.pipe(stream);

for (const artifact of zipArtifacts) {
await new Promise<void>((resolve, reject) => {
if (artifact.type === 'directory') {
this.readdirAndAddToZip(
zipfile,
rootPath,
artifact,
artifact.path,
err => {
if (err) reject(err);
else resolve();
},
);
} else {
const metadataPath = relative(rootPath, artifact.path);
const readStream = createReadStream(artifact.path);

zipfile.addReadStream(readStream, metadataPath);
resolve();
}
});
}

zipfile.end();

await new Promise<void>((resolve, reject) => {
zipfile.outputStream.once('error', err => reject(err));

stream.once('error', err => reject(err)).once('close', () => resolve());
});
}

readdirAndAddToZip(
zipFile: ZipFile,
rootPath: string,
source: ZipArtifact,
path: string,
callback: (err: Error | null) => void,
) {
fs.readdir(path, (err, files) => {
if (err) return callback(err);

let pending = files.length;

if (!pending) return callback(null);

files.forEach(file => {
const filePath = join(path, file);

fs.stat(filePath, (_err, stats) => {
if (_err) return callback(_err);

if (stats.isDirectory()) {
this.readdirAndAddToZip(
zipFile,
rootPath,
source,
filePath,
__err => {
if (__err) return callback(__err);

pending -= 1;

if (!pending) return callback(null);
},
);
} else {
if (
!source.shouldIgnore ||
(source.shouldIgnore && !source.shouldIgnore(filePath))
) {
const metadataPath = relative(rootPath, filePath);
const readStream = createReadStream(filePath);

zipFile.addReadStream(readStream, metadataPath);
}

pending -= 1;

if (!pending) return callback(null);
}
});
});
});
}
}

0 comments on commit d4d06f5

Please sign in to comment.