-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathoutput-files.ts
More file actions
53 lines (46 loc) · 1.59 KB
/
output-files.ts
File metadata and controls
53 lines (46 loc) · 1.59 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { copyFile, mkdir } from 'node:fs/promises';
import path from 'node:path';
import { DEFAULT_PERSIST_FILENAME, type Format } from '@code-pushup/models';
import { objectFromEntries, objectToKeys } from '@code-pushup/utils';
import type { OutputFiles, Settings } from './models.js';
import type { ProjectConfig } from './monorepo/tools.js';
const BASE_DIR = path.join('.code-pushup', '.ci');
type OutputType = 'current' | 'previous' | 'comparison';
export async function saveOutputFiles<T extends Partial<OutputFiles>>({
project,
type,
files,
settings: { logger, directory },
}: {
project: Pick<ProjectConfig, 'name'> | null;
type: OutputType;
files: T;
settings: Pick<Settings, 'logger' | 'directory'>;
}): Promise<T> {
const baseDir = project ? path.join(BASE_DIR, project.name) : BASE_DIR;
const outputDir = path.join(directory, baseDir, `.${type}`);
const name =
type === 'comparison'
? `${DEFAULT_PERSIST_FILENAME}-diff`
: DEFAULT_PERSIST_FILENAME;
const formats = objectToKeys(files) as Format[];
const outputs = objectFromEntries(
formats.map(format => [
format,
path.join(outputDir, `${name}.${format.toString()}`),
]),
);
if (formats.length > 0) {
await mkdir(outputDir, { recursive: true });
}
await Promise.all(
formats.map(async format => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const src = files[format]!;
const dest = outputs[format];
await copyFile(src, dest);
logger.debug(`Copied ${type} report from ${src} to ${dest}`);
}),
);
return outputs as T;
}