Skip to content
This repository was archived by the owner on Feb 9, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- '6'
- '8'
git:
submodules: false
before_script:
Expand Down
58 changes: 53 additions & 5 deletions lib/wrappers/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
import * as fs from "fs";
import { Extract } from "unzip";
import * as path from "path";
import * as yauzl from "yauzl";
import * as util from "util";

const access = util.promisify(fs.access);
const mkdir = util.promisify(fs.mkdir);

export class FileSystem {
public exists(path: string): boolean {
return fs.existsSync(path);
}

public extractZip(pathToZip: string, outputDir: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
const stream = fs.createReadStream(pathToZip).pipe(Extract({ path: outputDir }));
stream.on("close", resolve);
stream.on("error", reject);
return new Promise((resolve, reject) => {
yauzl.open(pathToZip, { autoClose: true, lazyEntries: true }, (openError, zipFile) => {
if (openError) {
return reject(openError);
}

zipFile.on('entry', entry => {
const fn = <string>entry.fileName;
if (/\/$/.test(fn)) {
return zipFile.readEntry();
}

zipFile.openReadStream(entry, (openStreamError, stream) => {
if(openStreamError) {
return reject(openStreamError);
};

const filePath = `${outputDir}/${fn}`;

return createParentDirsIfNeeded(filePath)
.catch(createParentDirError => reject(createParentDirError))
.then(() => {
const outfile = fs.createWriteStream(filePath);
stream.once('end', () => {
zipFile.readEntry();
outfile.close();
});
stream.pipe(outfile);
});
});
});

zipFile.once('end', () => resolve());

zipFile.readEntry();
});
});
}

Expand All @@ -23,3 +60,14 @@ export class FileSystem {
return JSON.parse(content.toString());
}
}

function createParentDirsIfNeeded(filePath: string) {
const dirs = path.dirname(filePath).split(path.sep);
return dirs.reduce((p, dir) => p.then(parent => {
const current = `${parent}${path.sep}${dir}`;

return access(current)
.catch(e => mkdir(current))
.then(() => current);
}), Promise.resolve(''));
}
Loading