@idlebox/node-error-codes 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/lib/nodejs-error-code.generated.d.ts +2562 -0
- package/lib/nodejs-error-code.generated.d.ts.map +1 -0
- package/lib/nodejs-error-code.generated.js +2569 -0
- package/lib/nodejs-error-code.generated.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +31 -0
- package/src/cache.generated.json +4429 -0
- package/src/nodejs-error-code.generated.ts +2570 -0
- package/src/nodejs-error-code.generator.ts +68 -0
- package/src/tsconfig.json +12 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { GenerateContext } from '@build-script/codegen';
|
|
2
|
+
import * as assert from 'node:assert';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import type API_JSON from './cache.generated.json';
|
|
5
|
+
|
|
6
|
+
const API_SOURCE = 'https://nodejs.org/api/errors.json';
|
|
7
|
+
|
|
8
|
+
export async function generate(context: GenerateContext) {
|
|
9
|
+
const cache_file = context.file('cache.json');
|
|
10
|
+
let content = '';
|
|
11
|
+
|
|
12
|
+
let body: typeof API_JSON;
|
|
13
|
+
try {
|
|
14
|
+
body = JSON.parse(await readFile(cache_file.absolutePath, 'utf-8'));
|
|
15
|
+
console.log('use exists api json at %s', cache_file.absolutePath);
|
|
16
|
+
} catch {}
|
|
17
|
+
|
|
18
|
+
if (!body) {
|
|
19
|
+
console.log('download api json from %s', API_SOURCE);
|
|
20
|
+
const resp = await fetch(API_SOURCE);
|
|
21
|
+
body = (await resp.json()) as any;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
assert.ok(body.miscs?.[0]?.name === 'Errors', 'api json struct change');
|
|
25
|
+
|
|
26
|
+
cache_file.append(JSON.stringify(body, null, 2));
|
|
27
|
+
|
|
28
|
+
function get_id(id: string) {
|
|
29
|
+
for (const section of body.miscs[0].miscs) {
|
|
30
|
+
if (section.name === id) {
|
|
31
|
+
return section.modules!;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
throw new Error(`missing section: ${id}`);
|
|
35
|
+
}
|
|
36
|
+
content += 'export enum NodeError {\n';
|
|
37
|
+
for (const err of get_id('node.js_error_codes')) {
|
|
38
|
+
content += line(err.textRaw, err.desc, false);
|
|
39
|
+
}
|
|
40
|
+
content += '\n';
|
|
41
|
+
for (const err of get_id('legacy_node.js_error_codes')) {
|
|
42
|
+
content += line(err.textRaw, err.desc, true);
|
|
43
|
+
}
|
|
44
|
+
content += '}\n\n';
|
|
45
|
+
|
|
46
|
+
content += 'export enum OpenSSLError {\n';
|
|
47
|
+
for (const section of get_id('openssl_error_codes')) {
|
|
48
|
+
for (const err of (section as any).modules) {
|
|
49
|
+
content += line(err.textRaw, err.desc, false);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
content += '}\n';
|
|
53
|
+
|
|
54
|
+
return content;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function line(text: string, desc: string, legacy: boolean) {
|
|
58
|
+
let ret = `\t/**\n${desc.replace(/^/gm, '\t * ')}\n`;
|
|
59
|
+
if (legacy) {
|
|
60
|
+
ret += '\t * @deprecated\n';
|
|
61
|
+
}
|
|
62
|
+
ret += '\t */\n';
|
|
63
|
+
|
|
64
|
+
const plain = text.replaceAll('`', '').trim();
|
|
65
|
+
ret += `\t${plain} = '${plain}',\n`;
|
|
66
|
+
|
|
67
|
+
return ret;
|
|
68
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@internal/local-rig/profiles/default/tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"typeRoots": ["../node_modules/@types", "../node_modules"],
|
|
5
|
+
"outDir": "../lib",
|
|
6
|
+
"rootDir": "./",
|
|
7
|
+
"types": ["node"],
|
|
8
|
+
},
|
|
9
|
+
// "files": ["preload.ts"],
|
|
10
|
+
// "include": ["**/*.ts"],
|
|
11
|
+
"exclude": ["**/*.generator.ts"],
|
|
12
|
+
}
|