import * as Promise from 'bluebird' import { createWriteStream } from 'fs' import * as archiver from 'archiver' import * as extractZip from 'extract-zip' import * as globby from 'globby'; export function zip (zipPath: string, filesPatterns: string[], cwd: string = process.cwd()) { return globby(filesPatterns, { cwd }) .then((filesPaths: string[]) => { return archive(filesPaths, zipPath, cwd); }) } export function archive (filesPaths: string[], zipPath: string, cwd: string) : Promise{ return new Promise((resolve, reject) => { const zip = archiver('zip'); const output = createWriteStream(zipPath) output.on('close', () => resolve(zipPath)) zip.on('error', reject) zip.pipe(output) filesPaths.forEach((file) => { const fileName = file.startsWith('./') ? file.substr(2) : file; zip.glob(fileName, { cwd }) }) zip.finalize() }) } export function extract(zipPath: string, destDir:string): Promise { return new Promise((resolve, reject) => { extractZip(zipPath, { dir: destDir }, (err) => { if (err) { reject(err); } else { resolve(); } }) }); }