Skip to content

Commit 69d5f05

Browse files
committed
chore: add script that prints out async driver api
1 parent 77ad03a commit 69d5f05

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
!mongodb-legacy.d.ts
22
.vscode/
33
*.tgz
4+
etc/node-mongodb-native

etc/print_async_api.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#! /usr/bin/env node
2+
/* eslint-disable no-console */
3+
4+
'use strict';
5+
6+
const { stat, readFile, writeFile } = require('fs/promises');
7+
const { join: joinPath } = require('path');
8+
const { spawn } = require('child_process');
9+
const typescript = require('typescript');
10+
11+
class APIPrinterError extends Error {}
12+
13+
const readJSON = async filePath => JSON.parse(await readFile(filePath, { encoding: 'utf8' }));
14+
const writeJSON = async (filePath, content) =>
15+
await writeFile(filePath, Buffer.from(JSON.stringify(content, undefined, 4), 'utf8'), {
16+
encoding: 'utf8'
17+
});
18+
const awaitableSpawn = async (command, options) => {
19+
const [program, ...cliArgs] = command.split(' ');
20+
const child = spawn(program, cliArgs, { ...options, stdio: 'inherit' });
21+
return new Promise((resolve, reject) => {
22+
child.addListener('exit', code => resolve(code));
23+
child.addListener('error', error => reject(error));
24+
});
25+
};
26+
27+
const DRIVER_REPO_DIR = joinPath(__dirname, 'node-mongodb-native');
28+
const API_CONFIG = joinPath(DRIVER_REPO_DIR, 'api-extractor.json');
29+
const DOC_MODEL = joinPath(DRIVER_REPO_DIR, 'etc/api.json');
30+
31+
async function getDriverAPI() {
32+
const apiExists = await stat(DOC_MODEL).then(statRes => statRes.isFile());
33+
if (apiExists) {
34+
return await readJSON(DOC_MODEL);
35+
}
36+
37+
const repoExists = await stat(DRIVER_REPO_DIR).then(statRes => statRes.isDirectory());
38+
if (!repoExists) {
39+
throw new APIPrinterError('You must clone the driver repo next to this script');
40+
}
41+
42+
await awaitableSpawn('npm install', { cwd: DRIVER_REPO_DIR });
43+
44+
const apiExtractorConfig = await readJSON(API_CONFIG);
45+
apiExtractorConfig.docModel.enabled = true;
46+
apiExtractorConfig.docModel.apiJsonFilePath = 'etc/api.json';
47+
await writeJSON(API_CONFIG, apiExtractorConfig);
48+
49+
await awaitableSpawn('npm run build:dts', { cwd: DRIVER_REPO_DIR });
50+
51+
return await readJSON(DOC_MODEL);
52+
}
53+
54+
async function main() {
55+
const api = await getDriverAPI();
56+
57+
console.log(api.name);
58+
const packageMembers = api.members[0].members;
59+
60+
for (const classDescription of packageMembers.filter(m => m.kind === 'Class')) {
61+
const className = classDescription.name;
62+
for (const methodDescription of classDescription.members.filter(m => m.kind === 'Method')) {
63+
/** @type {string} */
64+
const returnType = methodDescription.excerptTokens
65+
.slice(
66+
methodDescription.returnTypeTokenRange.startIndex,
67+
methodDescription.returnTypeTokenRange.endIndex
68+
)
69+
.map(token => token.text.replaceAll('\n', '').replace(/\s\s+/g, ' '))
70+
.join('');
71+
if (returnType.includes('Promise')) {
72+
const apiString = `${className}.${methodDescription.name}(): ${returnType}`;
73+
console.log(apiString);
74+
}
75+
}
76+
}
77+
}
78+
79+
main()
80+
.then(() => {
81+
process.exitCode = 0;
82+
})
83+
.catch(error => {
84+
console.error(`Fatal: ${error.message}\n${error.stack}`);
85+
process.exitCode = 1;
86+
});

0 commit comments

Comments
 (0)