@@ -214,7 +214,49 @@ export default function cpy(
214214 ...options ,
215215 } ;
216216
217+ /**
218+ @param {GlobPattern[] } patterns
219+ @param {string[] } ignorePatterns
220+ @returns {Promise<Entry[]> }
221+ */
222+ const getEntries = async ( patterns , ignorePatterns ) => {
223+ let entries = [ ] ;
224+
225+ for ( const pattern of patterns ) {
226+ /**
227+ @type {string[] }
228+ */
229+ let matches = [ ] ;
230+
231+ try {
232+ matches = pattern . getMatches ( ) ;
233+ } catch ( error ) {
234+ options . signal ?. throwIfAborted ( ) ;
235+ throw new CpyError ( `Cannot glob \`${ pattern . originalPath } \`: ${ error . message } ` , { cause : error } ) ;
236+ }
237+
238+ if ( matches . length === 0 && ! isDynamicPattern ( pattern . originalPath ) && ! isDynamicPattern ( ignorePatterns ) ) {
239+ throw new CpyError ( `Cannot copy \`${ pattern . originalPath } \`: the file doesn't exist` ) ;
240+ }
241+
242+ for ( const sourcePath of matches ) {
243+ entries . push ( new Entry ( sourcePath , path . relative ( options . cwd , sourcePath ) , pattern ) ) ;
244+ }
245+ }
246+
247+ if ( options . filter !== undefined ) {
248+ entries = await pFilter ( entries , options . filter , {
249+ concurrency : 1024 ,
250+ signal : options . signal ,
251+ } ) ;
252+ }
253+
254+ return entries ;
255+ } ;
256+
217257 const promise = ( async ( ) => {
258+ options . signal ?. throwIfAborted ( ) ;
259+
218260 /**
219261 @type {Entry[] }
220262 */
@@ -236,31 +278,7 @@ export default function cpy(
236278
237279 patterns = patterns . map ( pattern => new GlobPattern ( pattern , destination , { ...options , ignore} ) ) ;
238280
239- for ( const pattern of patterns ) {
240- /**
241- @type {string[] }
242- */
243- let matches = [ ] ;
244-
245- try {
246- matches = pattern . getMatches ( ) ;
247- } catch ( error ) {
248- throw new CpyError ( `Cannot glob \`${ pattern . originalPath } \`: ${ error . message } ` , { cause : error } ) ;
249- }
250-
251- if ( matches . length === 0 && ! isDynamicPattern ( pattern . originalPath ) && ! isDynamicPattern ( ignore ) ) {
252- throw new CpyError ( `Cannot copy \`${ pattern . originalPath } \`: the file doesn't exist` ) ;
253- }
254-
255- entries = [
256- ...entries ,
257- ...matches . map ( sourcePath => new Entry ( sourcePath , path . relative ( options . cwd , sourcePath ) , pattern ) ) ,
258- ] ;
259- }
260-
261- if ( options . filter !== undefined ) {
262- entries = await pFilter ( entries , options . filter , { concurrency : 1024 } ) ;
263- }
281+ entries = await getEntries ( patterns , ignore ) ;
264282
265283 if ( entries . length === 0 ) {
266284 const progressData = {
@@ -306,7 +324,7 @@ export default function cpy(
306324
307325 const progressData = {
308326 totalFiles : entries . length ,
309- percent : entries . length === 0 ? 0 : completedFiles / entries . length ,
327+ percent : completedFiles / entries . length ,
310328 completedFiles,
311329 completedSize,
312330 sourcePath : event . sourcePath ,
@@ -321,33 +339,35 @@ export default function cpy(
321339 }
322340 } ;
323341
324- return pMap (
325- entries ,
326- async entry => {
327- let to = preprocessDestinationPath ( {
328- entry,
329- destination,
330- options,
331- } ) ;
342+ const copyFileMapper = async entry => {
343+ let to = preprocessDestinationPath ( {
344+ entry,
345+ destination,
346+ options,
347+ } ) ;
332348
333- // Apply rename after computing the base destination path
334- to = renameFile ( to , options . rename ) ;
349+ // Apply rename after computing the base destination path
350+ to = renameFile ( to , options . rename ) ;
335351
336- // Check for self-copy after rename has been applied
337- if ( isSelfCopy ( entry . path , to ) ) {
338- throw new CpyError ( `Refusing to copy to itself: \`${ entry . path } \`` ) ;
339- }
352+ // Check for self-copy after rename has been applied
353+ if ( isSelfCopy ( entry . path , to ) ) {
354+ throw new CpyError ( `Refusing to copy to itself: \`${ entry . path } \`` ) ;
355+ }
340356
341- try {
342- await copyFile ( entry . path , to , { ...options , onProgress : fileProgressHandler } ) ;
343- } catch ( error ) {
344- throw new CpyError ( `Cannot copy from \`${ entry . relativePath } \` to \`${ to } \`: ${ error . message } ` , { cause : error } ) ;
345- }
357+ try {
358+ await copyFile ( entry . path , to , { ...options , onProgress : fileProgressHandler } ) ;
359+ } catch ( error ) {
360+ options . signal ?. throwIfAborted ( ) ;
361+ throw new CpyError ( `Cannot copy from \`${ entry . relativePath } \` to \`${ to } \`: ${ error . message } ` , { cause : error } ) ;
362+ }
363+
364+ return to ;
365+ } ;
346366
347- return to ;
348- } ,
349- { concurrency } ,
350- ) ;
367+ return pMap ( entries , copyFileMapper , {
368+ concurrency ,
369+ signal : options . signal ,
370+ } ) ;
351371 } ) ( ) ;
352372
353373 promise . on = ( _eventName , callback ) => {
0 commit comments