@@ -62,6 +62,7 @@ import {
6262} from "../../project/project-shared.ts" ;
6363import { asArray } from "../../core/array.ts" ;
6464import { normalizePath } from "../../core/path.ts" ;
65+ import { isSubdir } from "fs/_util.ts" ;
6566
6667export async function renderProject (
6768 context : ProjectContext ,
@@ -291,6 +292,12 @@ export async function renderProject(
291292 // track whether we need to keep the lib dir around
292293 let keepLibsDir = false ;
293294
295+ interface FileOperation {
296+ src : string ;
297+ performOperation : ( ) => void ;
298+ }
299+ const fileOperations : FileOperation [ ] = [ ] ;
300+
294301 // move/copy projResults to output_dir
295302 for ( let i = 0 ; i < fileResults . files . length ; i ++ ) {
296303 const renderedFile = fileResults . files [ i ] ;
@@ -327,9 +334,23 @@ export async function renderProject(
327334 ) ;
328335 } ) ;
329336 if ( keepFiles ) {
330- renderedFile . supporting . map ( ( file ) => copyFormatDir ( file ) ) ;
337+ renderedFile . supporting . forEach ( ( file ) => {
338+ fileOperations . push ( {
339+ src : file ,
340+ performOperation : ( ) => {
341+ copyFormatDir ( file ) ;
342+ } ,
343+ } ) ;
344+ } ) ;
331345 } else {
332- renderedFile . supporting . map ( ( file ) => moveFormatDir ( file ) ) ;
346+ renderedFile . supporting . forEach ( ( file ) => {
347+ fileOperations . push ( {
348+ src : file ,
349+ performOperation : ( ) => {
350+ moveFormatDir ( file ) ;
351+ } ,
352+ } ) ;
353+ } ) ;
333354 }
334355 }
335356
@@ -355,6 +376,20 @@ export async function renderProject(
355376 } ) ;
356377 }
357378
379+ // Perform the file operations (deepest folder first)
380+ const sortedOperations = fileOperations . sort ( ( a , b ) => {
381+ if ( a . src === b . src ) {
382+ return 0 ;
383+ } else if ( isSubdir ( a . src , b . src ) ) {
384+ return 1 ;
385+ } else {
386+ return - 1 ;
387+ }
388+ } ) ;
389+ sortedOperations . forEach ( ( op ) => {
390+ op . performOperation ( ) ;
391+ } ) ;
392+
358393 // move or copy the lib dir if we have one (move one subdirectory at a time
359394 // so that we can merge with what's already there)
360395 if ( libDir ) {
0 commit comments