@@ -14,11 +14,16 @@ import walk from './walk';
1414/**
1515 * Copy files (binary included) recursively.
1616 */
17- function copyFiles ( srcPath : string , destPath : string ) {
18- walk ( srcPath ) . forEach ( absoluteSrcFilePath => {
19- const relativeFilePath = path . relative ( srcPath , absoluteSrcFilePath ) ;
20- copyFile ( absoluteSrcFilePath , path . resolve ( destPath , relativeFilePath ) ) ;
21- } ) ;
17+ async function copyFiles ( srcPath : string , destPath : string ) {
18+ return Promise . all (
19+ walk ( srcPath ) . map ( async absoluteSrcFilePath => {
20+ const relativeFilePath = path . relative ( srcPath , absoluteSrcFilePath ) ;
21+ await copyFile (
22+ absoluteSrcFilePath ,
23+ path . resolve ( destPath , relativeFilePath ) ,
24+ ) ;
25+ } ) ,
26+ ) ;
2227}
2328
2429/**
@@ -33,10 +38,13 @@ function copyFile(srcPath: string, destPath: string) {
3338 return ;
3439 }
3540
36- copyBinaryFile ( srcPath , destPath , err => {
37- if ( err ) {
38- throw err ;
39- }
41+ return new Promise ( ( resolve , reject ) => {
42+ copyBinaryFile ( srcPath , destPath , err => {
43+ if ( err ) {
44+ reject ( err ) ;
45+ }
46+ resolve ( destPath ) ;
47+ } ) ;
4048 } ) ;
4149}
4250
@@ -47,17 +55,15 @@ function copyBinaryFile(srcPath, destPath, cb) {
4755 let cbCalled = false ;
4856 const { mode} = fs . statSync ( srcPath ) ;
4957 const readStream = fs . createReadStream ( srcPath ) ;
58+ const writeStream = fs . createWriteStream ( destPath ) ;
5059 readStream . on ( 'error' , err => {
5160 done ( err ) ;
5261 } ) ;
53- const writeStream = fs . createWriteStream ( destPath ) ;
5462 writeStream . on ( 'error' , err => {
5563 done ( err ) ;
5664 } ) ;
57- writeStream . on ( 'close' , ( ) => {
65+ readStream . on ( 'close' , ( ) => {
5866 done ( ) ;
59- } ) ;
60- writeStream . on ( 'ready' , ( ) => {
6167 fs . chmodSync ( destPath , mode ) ;
6268 } ) ;
6369 readStream . pipe ( writeStream ) ;
0 commit comments