-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtree.ts
More file actions
26 lines (24 loc) · 940 Bytes
/
tree.ts
File metadata and controls
26 lines (24 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import type { Tree } from '@nx/devkit';
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { ensureDirectoryExists } from '@code-pushup/test-utils';
export async function materializeTree(tree: Tree, targetFolder: string) {
const changes = tree.listChanges();
await Promise.all(
changes.map(async change => {
// Handle absolute paths that start with '/' by making them relative
const relativePath = change.path.startsWith('/')
? change.path.slice(1)
: change.path;
const filePath = path.join(targetFolder, relativePath);
if (change.type === 'CREATE' || change.type === 'UPDATE') {
try {
await ensureDirectoryExists(path.dirname(filePath));
await writeFile(filePath, change.content?.toString() ?? '');
} catch (error) {
console.error(`Failed to process file ${filePath}:`, error);
}
}
}),
);
}