@@ -6,6 +6,7 @@ const { mkdirs } = require('../mkdirs')
66const { pathExists } = require ( '../path-exists' )
77const { utimesMillis } = require ( '../util/utimes' )
88const stat = require ( '../util/stat' )
9+ const { asyncIteratorConcurrentProcess } = require ( '../util/async' )
910
1011async function copy ( src , dest , opts = { } ) {
1112 if ( typeof opts === 'function' ) {
@@ -113,28 +114,20 @@ async function onDir (srcStat, destStat, src, dest, opts) {
113114 await fs . mkdir ( dest )
114115 }
115116
116- const promises = [ ]
117-
118- // loop through the files in the current directory to copy everything
119- for await ( const item of await fs . opendir ( src ) ) {
117+ // iterate through the files in the current directory to copy everything
118+ await asyncIteratorConcurrentProcess ( await fs . opendir ( src ) , async ( item ) => {
120119 const srcItem = path . join ( src , item . name )
121120 const destItem = path . join ( dest , item . name )
122121
123- promises . push (
124- runFilter ( srcItem , destItem , opts ) . then ( include => {
125- if ( include ) {
126- // only copy the item if it matches the filter function
127- return stat . checkPaths ( srcItem , destItem , 'copy' , opts ) . then ( ( { destStat } ) => {
128- // If the item is a copyable file, `getStatsAndPerformCopy` will copy it
129- // If the item is a directory, `getStatsAndPerformCopy` will call `onDir` recursively
130- return getStatsAndPerformCopy ( destStat , srcItem , destItem , opts )
131- } )
132- }
133- } )
134- )
135- }
136-
137- await Promise . all ( promises )
122+ const include = await runFilter ( srcItem , destItem , opts )
123+ // only copy the item if it matches the filter function
124+ if ( include ) {
125+ const { destStat } = await stat . checkPaths ( srcItem , destItem , 'copy' , opts )
126+ // If the item is a copyable file, `getStatsAndPerformCopy` will copy it
127+ // If the item is a directory, `getStatsAndPerformCopy` will call `onDir` recursively
128+ await getStatsAndPerformCopy ( destStat , srcItem , destItem , opts )
129+ }
130+ } )
138131
139132 if ( ! destStat ) {
140133 await fs . chmod ( dest , srcStat . mode )
0 commit comments