From 8b887383c606c2a1601a60685cce5b08d835eeb6 Mon Sep 17 00:00:00 2001 From: tdzl2003 Date: Tue, 23 Feb 2016 16:45:03 +0800 Subject: [PATCH] switch to zip package --- local-cli/src/bundle.js | 48 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/local-cli/src/bundle.js b/local-cli/src/bundle.js index 37b00c6..6d1c1c1 100644 --- a/local-cli/src/bundle.js +++ b/local-cli/src/bundle.js @@ -6,8 +6,8 @@ import * as path from 'path'; import { mkdir as mkdirRecurisve } from 'mkdir-recursive'; import { getRNVersion } from './utils'; import * as fs from 'fs'; -import * as tar from 'tar'; -import fstream from 'fstream'; +import {ZipFile} from 'yazl'; + function mkdir(dir){ return new Promise((resolve, reject) => { @@ -25,25 +25,37 @@ function pack(dir, output){ return mkdir(path.dirname(output)) .then(()=>{ return new Promise((resolve, reject) => { - const dest = fs.createWriteStream(output); + var zipfile = new ZipFile(); - var packer = tar.Pack({ - noProprietary: true, - fromBase: true, - }) - .on('error', err => { - reject(err); - }) - .on('end', () => { + function addDirectory(root, rel){ + if (rel) { + zipfile.addEmptyDirectory(rel); + } + const childs = fs.readdirSync(root); + for (const name of childs) { + if (name === '.' || name === '..'){ + continue; + } + const fullPath = path.join(root, name); + const stat = fs.statSync(fullPath); + if (stat.isFile()) { + console.log('adding: ' + rel+name); + zipfile.addFile(fullPath, rel+name); + } else if (stat.isDirectory()) { + console.log('adding: ' + rel+name+'/'); + addDirectory(fullPath, rel+name+'/'); + } + } + } + + addDirectory(dir, ''); + + zipfile.outputStream.on('error', err => reject(err)); + zipfile.outputStream.pipe(fs.createWriteStream(output)) + .on("close", function() { resolve(); }); - - fstream.Reader({ path: dir, type: "Directory" }) - .on('error', err => { - reject(err); - }) - .pipe(packer) - .pipe(dest); + zipfile.end(); }); }) }