forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite-test-files.ts
39 lines (34 loc) · 1.37 KB
/
write-test-files.ts
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
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { BuildOutputFileType } from '@angular/build';
import { ResultFile, emitFilesToDisk } from '@angular/build/private';
import fs from 'node:fs/promises';
import path from 'node:path';
export async function writeTestFiles(files: Record<string, ResultFile>, testDir: string) {
const directoryExists = new Set<string>();
// Writes the test related output files to disk and ensures the containing directories are present
await emitFilesToDisk(Object.entries(files), async ([filePath, file]) => {
if (file.type !== BuildOutputFileType.Browser && file.type !== BuildOutputFileType.Media) {
return;
}
const fullFilePath = path.join(testDir, filePath);
// Ensure output subdirectories exist
const fileBasePath = path.dirname(fullFilePath);
if (fileBasePath && !directoryExists.has(fileBasePath)) {
await fs.mkdir(fileBasePath, { recursive: true });
directoryExists.add(fileBasePath);
}
if (file.origin === 'memory') {
// Write file contents
await fs.writeFile(fullFilePath, file.contents);
} else {
// Copy file contents
await fs.copyFile(file.inputPath, fullFilePath, fs.constants.COPYFILE_FICLONE);
}
});
}