@@ -3,112 +3,87 @@ import fs from 'fs';
33import path from 'path' ;
44import JSZip from 'jszip' ;
55import ZipExporter from '../libs/zipExporter' ;
6- import UnzippedReader from '../libs/UnzippedReader' ;
7- import { Readable } from 'stream' ;
86
97JSZip . make = ( ) => {
10- const augment = ( _opts ) => {
11- const opts = _opts || { } ;
12- opts . createFolders = opts . createFolders || true ;
13- return opts ;
14- }
8+ const augment = ( _opts ) => {
9+ const opts = _opts || { } ;
10+ opts . createFolders = opts . createFolders || true ;
11+ return opts ;
12+ } ;
1513
16- const instance = new JSZip ( ) ;
17- const originals = {
18- loadAsync : instance . loadAsync ,
19- file : instance . file ,
20- } ;
14+ const instance = new JSZip ( ) ;
15+ const originals = {
16+ loadAsync : instance . loadAsync ,
17+ file : instance . file ,
18+ } ;
2119
22- instance . loadAsync = ( data , options ) => {
23- return originals . loadAsync . call ( instance , data , augment ( options ) ) ;
24- } ;
20+ instance . loadAsync = ( data , options ) => {
21+ return originals . loadAsync . call ( instance , data , augment ( options ) ) ;
22+ } ;
2523
26- instance . file = ( name , content , options ) => {
27- if ( ! content ) {
28- return originals . file . call ( instance , name ) ;
29- }
30- return originals . file . call ( instance , name , content , augment ( options ) ) ;
31- } ;
32- return instance ;
24+ instance . file = ( name , content , options ) => {
25+ if ( ! content ) {
26+ return originals . file . call ( instance , name ) ;
27+ }
28+ return originals . file . call ( instance , name , content , augment ( options ) ) ;
29+ } ;
30+ return instance ;
3331} ;
3432
3533const zipDir = async ( dir , zippedDir ) => {
36- return new Promise ( ( resolve , reject ) => {
37- fs . readdir ( dir , ( err , files ) => {
38- if ( err ) {
39- reject ( err ) ;
40- }
41- const filePaths = files . map ( file => path . join ( dir , file ) ) ;
42- const typedFiles = filePaths . reduce ( ( acc , filePath ) => {
43- const stats = fs . statSync ( filePath ) ;
44- acc . push ( {
45- path : filePath ,
46- isDirectory : stats . isDirectory ( ) ? 'dir' : 'file' ,
47- } ) ;
48- return acc ;
49- } , [ ] ) ;
50- for ( const entry of typedFiles ) {
51- const parsedEntryPath = path . parse ( entry . path ) ;
52- if ( entry . type === 'dir' ) {
53- const newZippedDir = zippedDir . folder ( parsedEntryPath . base ) ;
54- zipDir ( entry . path , newZippedDir ) . then ( console . log ) ;
55- }
56- else {
57- fs . readFile ( entry . path , ( err , data ) => {
58- if ( err ) {
59- reject ( err ) ;
60- }
61- zippedDir . file ( parsedEntryPath . base , data ) ;
62- } ) ;
63- }
64- }
65- resolve ( ) ;
66- } ) ;
34+ const files = await fs . promises . readdir ( dir ) ;
35+ const filePaths = files . map ( file => path . join ( dir , file ) ) ;
36+ const typedFiles = [ ] ;
37+ for ( const filePath of filePaths ) {
38+ const stats = await fs . promises . stat ( filePath ) ;
39+ typedFiles . push ( {
40+ path : filePath ,
41+ type : stats . isDirectory ( ) ? 'dir' : 'file' ,
6742 } ) ;
68- }
69-
70- const zipDirSync = ( dir , zippedDir ) => {
71- const entries = fs . readdirSync ( dir ) ;
72-
73- for ( const entry of entries ) {
74- const entryNormalizedPath = path . normalize ( path . join ( dir , entry ) ) ;
75- const stats = fs . statSync ( entryNormalizedPath ) ;
76- if ( stats . isDirectory ( ) ) {
77- const newZippedDir = zippedDir . folder ( entry ) ;
78- zipDirSync ( entryNormalizedPath , newZippedDir ) ;
79- }
80- else {
81- const data = fs . readFileSync ( entryNormalizedPath ) ;
82- zippedDir . file ( entry , data ) ;
83- }
43+ }
44+ for ( const entry of typedFiles ) {
45+ const parsedEntryPath = path . parse ( entry . path ) ;
46+ if ( entry . type === 'dir' ) {
47+ zippedDir . folder ( parsedEntryPath . base ) ;
48+ await zipDir ( entry . path , zippedDir ) ;
8449 }
85- return zippedDir ;
86- }
50+ else {
51+ const data = await fs . promises . readFile ( entry . path ) ;
52+ zippedDir . file ( parsedEntryPath . base , data . toString ( ) ) ;
53+ }
54+ }
55+ } ;
8756
8857const Zipper = { } ;
8958
90- Zipper . zip = ( entity , _callback , _shiftedCallback ) => {
91- const zippedObj = JSZip . make ( ) ;
92- if ( entity instanceof Buffer ) {
93- // entity is a buffer containing a file, _callback is the name of the buffer
94- const callback = _shiftedCallback || ( ( ) => { } ) ;
95- zippedObj . file ( _callback , entity ) ;
96- callback ( null , new ZipExporter ( zippedObj , false , true ) ) ;
97- }
98- else if ( entity instanceof UnzippedReader ) {
99- // entity is an unzipped file
100- const callback = _callback || ( ( ) => { } ) ;
101- callback ( null , new ZipExporter ( entity . unzipped_file , false , true ) ) ;
59+ Zipper . zip = async ( entity , _callback , _shiftedCallback ) => {
60+ const zippedObj = JSZip . make ( ) ;
61+ if ( typeof entity === 'string' ) {
62+ // entity is a path to a file/directory
63+ const callback = _callback || ( ( ) => { } ) ;
64+ const normalizedPath = path . normalize ( entity ) ;
65+ const stats = await fs . promises . stat ( normalizedPath ) ;
66+ if ( stats . isDirectory ( ) ) {
67+ await zipDir ( normalizedPath , zippedObj ) ;
68+ callback ( null , new ZipExporter ( zippedObj , false , true ) ) ;
69+ return ;
10270 }
10371 else {
104- _callback ( new Error ( 'Invalid entity type' ) ) ;
72+ const parsedPath = path . parse ( normalizedPath ) ;
73+ const data = await fs . promises . readFile ( normalizedPath ) ;
74+ zippedObj . file ( parsedPath . base , data ) ;
75+ callback ( null , new ZipExporter ( zippedObj , false , true ) ) ;
10576 }
77+ }
78+ else {
79+ _callback ( new Error ( 'Invalid entity type' ) ) ;
80+ }
10681} ;
10782
10883Zipper . unzip = async ( entity ) => {
109- const zippedObj = JSZip . make ( ) ;
110- await zippedObj . loadAsync ( entity ) ;
111- return new ZipExporter ( zippedObj , true , true ) ;
112- }
84+ const zippedObj = JSZip . make ( ) ;
85+ await zippedObj . loadAsync ( entity ) ;
86+ return new ZipExporter ( zippedObj , true , true ) ;
87+ } ;
11388
114- export default Zipper ;
89+ export default Zipper ;
0 commit comments