Skip to content

Commit ddea033

Browse files
authored
DOP-3340: Initial implementation of OAS page builder module (#714)
1 parent 75dca40 commit ddea033

File tree

19 files changed

+10795
-0
lines changed

19 files changed

+10795
-0
lines changed

modules/oas-page-builder/.babelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env",
4+
"@babel/preset-typescript"
5+
],
6+
"env": {
7+
"test": {
8+
"plugins": ["@babel/plugin-transform-runtime"]
9+
}
10+
}
11+
}

modules/oas-page-builder/.eslintrc.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es6: true,
5+
},
6+
extends: 'plugin:@typescript-eslint/recommended',
7+
globals: {
8+
Atomics: 'readonly',
9+
SharedArrayBuffer: 'readonly',
10+
},
11+
parser: '@typescript-eslint/parser',
12+
parserOptions: {
13+
ecmaVersion: 2018,
14+
sourceType: 'module',
15+
},
16+
rules: {
17+
'@typescript-eslint/ban-ts-comment': 'off',
18+
'no-console': 'off',
19+
},
20+
};

modules/oas-page-builder/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v14.17.6

modules/oas-page-builder/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# OpenAPI Content Page Builder
2+
3+
The OpenAPI Content Page Builder is a module dedicated to building OpenAPI content
4+
pages on the MongoDB Documentation site out of OpenAPI spec (OAS) files.
5+
6+
This module is intended to be called after the Snooty [parser](https://github.com/mongodb/snooty-parser) and [frontend](https://github.com/mongodb/snooty) have completed their
7+
tasks, but before [mut](https://github.com/mongodb/mut) uploads files to S3.
8+
9+
## Installation
10+
11+
If using `nvm`, run `nvm use` to ensure you're using the intended version
12+
of Node found in `.nvmrc`.
13+
14+
To install dependencies, run:
15+
16+
```
17+
npm ci
18+
```
19+
20+
Ensure that the proper environment variables are set up locally. `sample.env`
21+
contains environment variables that can be copied to a local `.env` file.
22+
23+
### Building
24+
25+
The module is compiled by `tsc` to `./dist/index.js`. Run the following to build:
26+
27+
```
28+
npm run build
29+
```
30+
31+
If developing locally, build in between runs to ensure the compiled JS file contains
32+
the latest changes.
33+
34+
### Running
35+
36+
Make sure the module has been built/compiled with an existing `./dist/index.js` file.
37+
Run the following to run the module:
38+
39+
```
40+
npm run start -- --bundle <path> --output <path> --redoc <path> --repo <path>
41+
```
42+
43+
All of the options above are required for the module to run properly. Run
44+
`npm run start -- --help` for more information or see [(Required) Options](#required-options).
45+
46+
**Example**
47+
48+
The following example shows what the command would be like if ran through the
49+
autobuilder. If running locally, update the paths according to your local setup.
50+
51+
```
52+
npm run start -- --bundle /home/docsworker-xlarge/repos/cloud-docs/bundle.zip --output /home/docsworker-xlarge/repos/cloud-docs/public/ --redoc /home/docsworker-xlarge/redoc/cli/index.js --repo /home/docsworker-xlarge/repos/cloud-docs/
53+
```
54+
55+
### (Required) Options
56+
57+
| Option | Description |
58+
| ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
59+
| bundle | Path to the parsed bundle zip file. This should be the output of the parser and will be used to obtain build metadata about OpenAPI content pages. |
60+
| output | Path to the directory that the OpenAPI content pages should be built to. Typically, this would be the same output `public/` directory of a Snooty frontend build. |
61+
| redoc | Path to the local installation of Redoc CLI to use. This should point to the team's [fork of Redoc](https://github.com/mongodb-forks/redoc), with the target being a compiled JS file. |
62+
| repo | Path to the parsed docs repo's directory. This is to ensure that OpenAPI content pages using local OAS files can be properly sourced and passed down as an argument to Redoc CLI. |

modules/oas-page-builder/index.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import dotenv from 'dotenv';
2+
// Ensures that env variables are loaded in immediately, before other imports
3+
dotenv.config();
4+
import { Command } from 'commander';
5+
import { getOASMetadata } from './src/services/buildMetadata';
6+
import { buildOpenAPIPages } from './src/services/pageBuilder';
7+
import { ModuleOptions } from './src/types';
8+
9+
const program = new Command();
10+
program
11+
.usage('-- [options]')
12+
.requiredOption('-b, --bundle <path>', 'path to parsed bundle zip')
13+
.requiredOption('-o, --output <path>', 'path to the directory to output generated files')
14+
.requiredOption('--redoc <path>', 'path to the Redoc CLI program to run. Must be a JS file')
15+
.requiredOption('--repo <path>', 'path to repo being built');
16+
17+
program.parse();
18+
const options = program.opts<ModuleOptions>();
19+
20+
const app = async (options: ModuleOptions) => {
21+
const { bundle: bundlePath } = options;
22+
const oasMetadata = getOASMetadata(bundlePath);
23+
if (!oasMetadata) {
24+
console.log('No OpenAPI content pages found.');
25+
return;
26+
}
27+
28+
const oasMetadataEntries = Object.entries(oasMetadata);
29+
const numOASPages = oasMetadataEntries.length;
30+
console.log(`OpenAPI content pages found: ${numOASPages}.`);
31+
32+
await buildOpenAPIPages(oasMetadataEntries, options);
33+
};
34+
35+
app(options)
36+
.then(() => {
37+
console.log('Finished building OpenAPI content pages.');
38+
process.exit(0);
39+
})
40+
.catch((e) => {
41+
console.error(e);
42+
process.exit(1);
43+
});

0 commit comments

Comments
 (0)