-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcopyReleaseFile.ts
More file actions
83 lines (77 loc) · 2.06 KB
/
Copy pathcopyReleaseFile.ts
File metadata and controls
83 lines (77 loc) · 2.06 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { releaseFilesDefaultNames } from "../params.js";
import fs from "fs";
import path from "path";
interface FileConfig {
regex: RegExp;
maxSize: number;
required: boolean;
multiple: boolean;
id: string;
}
function getDefaultName(fileId: string): string | undefined {
return releaseFilesDefaultNames[
fileId as keyof typeof releaseFilesDefaultNames
];
}
export function copyReleaseFile({
fileConfig,
fromDir,
toDir
}: {
fileConfig: FileConfig;
fromDir: string;
toDir: string;
}): void {
const files = fs.readdirSync(fromDir);
const matchingFiles = files.filter(file => fileConfig.regex.test(file));
if (matchingFiles.length === 0) {
if (fileConfig.required) {
throw new NoFileFoundError(fileConfig, fromDir);
} else {
// Ignore
}
} else if (matchingFiles.length === 1) {
fs.copyFileSync(
path.join(fromDir, matchingFiles[0]),
path.join(toDir, getDefaultName(fileConfig.id) || matchingFiles[0])
);
} else {
if (fileConfig.multiple) {
for (const matchingFile of matchingFiles) {
fs.copyFileSync(
path.join(fromDir, matchingFile),
path.join(toDir, matchingFile)
);
}
} else {
throw new TooManyFilesError(fileConfig, fromDir, matchingFiles);
}
}
}
class NoFileFoundError extends Error {
constructor(fileConfig: FileConfig, fromDir: string) {
super(
`No ${fileConfig.id} found in ${fromDir}.` +
`${fileConfig.id} naming must match ${fileConfig.regex.toString()}.` +
`Please rename it to ${
getDefaultName(fileConfig.id) || fileConfig.regex.toString()
}`
);
}
}
class TooManyFilesError extends Error {
constructor(
fileConfig: FileConfig,
fromDir: string,
matchingFiles: string[]
) {
super(
`More than one ${fileConfig.id} found in ${fromDir}: ` +
matchingFiles.join(", ") +
`Only one file can match ${fileConfig.regex.toString()}` +
`Please rename it to ${
getDefaultName(fileConfig.id) || fileConfig.regex.toString()
}`
);
}
}