Skip to content

Commit 82949c7

Browse files
committed
Defer File Operations
By performing the file operations, we ensure that we process subdirectories first.
1 parent 33736f6 commit 82949c7

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/command/render/project.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
} from "../../project/project-shared.ts";
6363
import { asArray } from "../../core/array.ts";
6464
import { normalizePath } from "../../core/path.ts";
65+
import { isSubdir } from "fs/_util.ts";
6566

6667
export 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

Comments
 (0)