-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathgenerate.mjs
More file actions
67 lines (53 loc) · 1.77 KB
/
generate.mjs
File metadata and controls
67 lines (53 loc) · 1.77 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
'use strict';
import { basename, join } from 'node:path';
import { checkIndirectReferences } from './utils/checkIndirectReferences.mjs';
import { extractExports } from './utils/extractExports.mjs';
import { findDefinitions } from './utils/findDefinitions.mjs';
import getConfig from '../../utils/configuration/index.mjs';
import {
GITHUB_BLOB_URL,
populate,
} from '../../utils/configuration/templates.mjs';
import { writeFile } from '../../utils/file.mjs';
/**
* Generates the `apilinks.json` file.
*
* @type {import('./types').Generator['generate']}
*/
export async function generate(input) {
const config = getConfig('api-links');
/**
* @type Record<string, string>
*/
const definitions = {};
input.forEach(program => {
/**
* Mapping of definitions to their line number
*
* @type {Record<string, number>}
* @example { 'someclass.foo': 10 }
*/
const nameToLineNumberMap = {};
// `http.js` -> `http`
const baseName = basename(program.path, '.js');
const exports = extractExports(program, baseName, nameToLineNumberMap);
findDefinitions(program, baseName, nameToLineNumberMap, exports);
checkIndirectReferences(program, exports, nameToLineNumberMap);
const fullGitUrl = `${populate(GITHUB_BLOB_URL, config)}lib/${baseName}.js`;
// Add the exports we found in this program to our output
Object.keys(nameToLineNumberMap).forEach(key => {
const lineNumber = nameToLineNumberMap[key];
definitions[key] = `${fullGitUrl}#L${lineNumber}`;
});
});
if (config.output) {
const out = join(config.output, 'apilinks.json');
await writeFile(
out,
config.minify
? JSON.stringify(definitions)
: JSON.stringify(definitions, null, 2)
);
}
return definitions;
}