Skip to content

Commit

Permalink
commit zip buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
zoumiaojiang committed May 27, 2017
1 parent 4f921df commit 322b49f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bpwa",
"version": "1.1.2",
"version": "1.1.3",
"description": "pwa project solution",
"main": "dist/index.js",
"scripts": {
Expand All @@ -25,8 +25,8 @@
"author": "sfe-sy<sfe-sy@baidu.com>",
"license": "ISC",
"dependencies": {
"adm-zip": "^0.4.7",
"ajv": "^5.1.3",
"archiver": "^1.3.0",
"babel-core": "^6.24.1",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
Expand All @@ -36,8 +36,7 @@
"glob": "^7.1.2",
"request": "^2.81.0",
"request-promise": "^4.2.1",
"simple-git": "^1.73.0",
"tar.gz": "^1.0.5"
"simple-git": "^1.73.0"
},
"devDependencies": {
"babel-cli": "^6.24.0",
Expand Down
26 changes: 15 additions & 11 deletions src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import gData from './data';
import git from 'simple-git';
import glob from 'glob';
import etpl from 'etpl';
import Zip from 'adm-zip';
import fs from 'fs-extra';
import path from 'path';

import archiver from 'archiver';

/**
* 从git上downloa代码下来
*
Expand Down Expand Up @@ -70,11 +71,8 @@ function renderTemplate(fields, ltd, template, isStream) {
},
(err, files) => {
files.forEach(file => {

const filePath = path.resolve(ltd, file);

if (fs.statSync(filePath).isFile()) {

const fileCon = fs.readFileSync(filePath, 'utf8');

// 这里可以直接通过外界配置的规则,重新计算出一份数据,只要和 template 里面的字段对应上就好了。
Expand All @@ -90,16 +88,22 @@ function renderTemplate(fields, ltd, template, isStream) {
const afterCon = etplCompile.compile(fileCon)(renderData);
fs.writeFileSync(filePath, afterCon);
}

});

if (isStream) {

const zip = new Zip();
zip.addLocalFolder(ltd, fields.name);
zip.toBuffer(buffer => resolve(buffer));
// zip.writeZip(path.resolve(ltd, '../files.zip'));
// console.log(fs.createReadStream(ltd));
const archive = archiver('zip', {
zlib: {level: 9} // Sets the compression level.
});
const tmpZipPath = path.resolve(ltd, 'tmp.zip');
const output = fs.createWriteStream(tmpZipPath);
archive.pipe(output);
archive.directory(ltd, fields.name);
archive.finalize().on('finish', () => {
const zipCon = fs.createReadStream(tmpZipPath);
zipCon.on('data', data => {
resolve(data);
});
});
}
else {
fs.copySync(ltd, dirPath);
Expand Down

0 comments on commit 322b49f

Please sign in to comment.