-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-errors.js
36 lines (31 loc) · 1.03 KB
/
create-errors.js
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
/* eslint-disable @typescript-eslint/no-var-requires */
const { StatusCodes, getReasonPhrase } = require("http-status-codes");
const { writeFile } = require("fs/promises");
const { format } = require("prettier");
function functionName(text) {
const name = text.replace(/[\s\-']/g, "").replace(/Error$/, "") + "Error";
return name.charAt(0).toLowerCase() + name.substring(1);
}
async function main() {
const code = [
"// This file is generated, do not edit!\n// node create-errors.js",
'import { statusErrorFactory } from "./error";',
...Object.keys(StatusCodes)
.filter((e) => e.length === 3)
.map(
(code) =>
`export const ${functionName(
getReasonPhrase(code)
)} = statusErrorFactory(${code}, "${getReasonPhrase(code)}");`
),
].join("\n");
const formatted = format(code, {
parser: "typescript",
filepath: "src/errors.ts",
});
await writeFile("src/errors.ts", formatted, "utf-8");
}
main().catch((error) => {
process.exitCode = 1;
console.error(error);
});