1- var fs = require ( 'co-fs' ) ;
2- var co = require ( 'co' ) ;
3- var fse = require ( 'co-fs-extra' ) ;
4- var path = require ( 'path' ) ;
5- var JSZip = require ( 'jszip' ) ;
1+ const fs = require ( 'fs-extra' ) ;
2+ const path = require ( 'path' ) ;
3+ const JSZip = require ( 'jszip' ) ;
64
7- var FileManager = { } ;
5+ const FileManager = { } ;
86
9- FileManager . getStats = function * ( p ) {
10- var stats = yield fs . stat ( p ) ;
7+ FileManager . getStats = async p => {
8+ const stats = fs . statSync ( p ) ;
119 return {
1210 folder : stats . isDirectory ( ) ,
1311 size : stats . size ,
1412 mtime : stats . mtime . getTime ( )
1513 }
1614} ;
1715
18- FileManager . list = function * ( dirPath ) {
19- var files = yield fs . readdir ( dirPath ) ;
20- var stats = [ ] ;
21- for ( var i = 0 ; i < files . length ; ++ i ) {
22- var fPath = path . join ( dirPath , files [ i ] ) ;
23- var stat = yield FileManager . getStats ( fPath ) ;
16+ FileManager . list = async dirPath => {
17+ const files = await fs . readdir ( dirPath ) ;
18+ const stats = [ ] ;
19+ for ( let i = 0 ; i < files . length ; ++ i ) {
20+ const fPath = path . join ( dirPath , files [ i ] ) ;
21+ const stat = await FileManager . getStats ( fPath ) ;
2422 stat . name = files [ i ] ;
2523 stats . push ( stat ) ;
2624 }
2725 return stats ;
2826} ;
2927
30- FileManager . remove = function * ( p ) {
31- yield fse . remove ( p ) ;
28+ FileManager . remove = async p => {
29+ await fs . remove ( p ) ;
3230} ;
3331
34- FileManager . mkdirs = function * ( dirPath ) {
35- yield fse . mkdirs ( dirPath ) ;
32+ FileManager . mkdirs = async dirPath => {
33+ await fs . mkdirs ( dirPath ) ;
3634} ;
3735
38- FileManager . move = function * ( srcs , dest ) {
39- for ( var i = 0 ; i < srcs . length ; ++ i ) {
40- var basename = path . basename ( srcs [ i ] ) ;
41- yield fse . move ( srcs [ i ] , path . join ( dest , basename ) ) ;
36+ FileManager . move = async ( srcs , dest ) => {
37+ for ( let i = 0 ; i < srcs . length ; ++ i ) {
38+ const basename = path . basename ( srcs [ i ] ) ;
39+ await fs . move ( srcs [ i ] , path . join ( dest , basename ) ) ;
4240 }
4341} ;
4442
45- FileManager . rename = function * ( src , dest ) {
46- yield fse . move ( src , dest ) ;
43+ FileManager . rename = async ( src , dest ) => {
44+ await fs . move ( src , dest ) ;
4745} ;
4846
49- FileManager . archive = function * ( src , archive , dirPath , embedDirs ) {
50- var zip = new JSZip ( ) ;
51- var baseName = path . basename ( archive , '.zip' ) ;
47+ FileManager . archive = async ( src , archive , dirPath , embedDirs ) => {
48+ const zip = new JSZip ( ) ;
49+ const baseName = path . basename ( archive , '.zip' ) ;
5250
53- function * addFile ( file ) {
54- var data = yield fs . readFile ( file ) ;
55- var name ;
51+ const addFile = async file => {
52+ const data = await fs . readFile ( file ) ;
53+ let name ;
5654 if ( embedDirs ) {
5755 name = file ;
5856 if ( name . indexOf ( dirPath ) === 0 ) {
@@ -65,30 +63,30 @@ FileManager.archive = function *(src, archive, dirPath, embedDirs) {
6563 C . logger . info ( 'Added ' + name + ' ' + data . length + ' bytes to archive ' + archive ) ;
6664 }
6765
68- function * addDir ( dir ) {
69- var contents = yield fs . readdir ( dir ) ;
70- for ( var file of contents ) {
71- yield * process ( path . join ( dir , file ) ) ;
66+ const addDir = async dir => {
67+ const contents = await fs . readdir ( dir ) ;
68+ for ( const file of contents ) {
69+ await process ( path . join ( dir , file ) ) ;
7270 }
7371 }
7472
75- function * process ( fp ) {
76- var stat = yield fs . stat ( fp ) ;
73+ const process = async fp => {
74+ const stat = await fs . stat ( fp ) ;
7775 if ( stat . isDirectory ( ) ) {
78- yield * addDir ( fp ) ;
76+ await addDir ( fp ) ;
7977 } else {
80- yield addFile ( fp ) ;
78+ await addFile ( fp ) ;
8179 }
8280 }
8381
8482 // Add each src. For directories, do the entire recursive dir.
85- for ( var file of src ) {
86- yield * process ( file ) ;
83+ for ( const file of src ) {
84+ await process ( file ) ;
8785 }
8886
8987 // Generate the zip and store the final.
90- var data = yield zip . generateAsync ( { type :'nodebuffer' , compression :'DEFLATE' } ) ;
91- yield fs . writeFile ( archive , data , 'binary' ) ;
88+ const data = await zip . generateAsync ( { type :'nodebuffer' , compression :'DEFLATE' } ) ;
89+ await fs . writeFile ( archive , data , 'binary' ) ;
9290} ;
9391
9492module . exports = FileManager ;
0 commit comments