Skip to content

Commit

Permalink
DOP-3340: Initial implementation of OAS page builder module (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayangler authored Nov 29, 2022
1 parent 75dca40 commit ddea033
Show file tree
Hide file tree
Showing 19 changed files with 10,795 additions and 0 deletions.
11 changes: 11 additions & 0 deletions modules/oas-page-builder/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}
20 changes: 20 additions & 0 deletions modules/oas-page-builder/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
env: {
browser: true,
es6: true,
},
extends: 'plugin:@typescript-eslint/recommended',
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
rules: {
'@typescript-eslint/ban-ts-comment': 'off',
'no-console': 'off',
},
};
1 change: 1 addition & 0 deletions modules/oas-page-builder/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v14.17.6
62 changes: 62 additions & 0 deletions modules/oas-page-builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# OpenAPI Content Page Builder

The OpenAPI Content Page Builder is a module dedicated to building OpenAPI content
pages on the MongoDB Documentation site out of OpenAPI spec (OAS) files.

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
tasks, but before [mut](https://github.com/mongodb/mut) uploads files to S3.

## Installation

If using `nvm`, run `nvm use` to ensure you're using the intended version
of Node found in `.nvmrc`.

To install dependencies, run:

```
npm ci
```

Ensure that the proper environment variables are set up locally. `sample.env`
contains environment variables that can be copied to a local `.env` file.

### Building

The module is compiled by `tsc` to `./dist/index.js`. Run the following to build:

```
npm run build
```

If developing locally, build in between runs to ensure the compiled JS file contains
the latest changes.

### Running

Make sure the module has been built/compiled with an existing `./dist/index.js` file.
Run the following to run the module:

```
npm run start -- --bundle <path> --output <path> --redoc <path> --repo <path>
```

All of the options above are required for the module to run properly. Run
`npm run start -- --help` for more information or see [(Required) Options](#required-options).

**Example**

The following example shows what the command would be like if ran through the
autobuilder. If running locally, update the paths according to your local setup.

```
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/
```

### (Required) Options

| Option | Description |
| ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 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. |
| 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. |
| 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. |
| 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. |
43 changes: 43 additions & 0 deletions modules/oas-page-builder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import dotenv from 'dotenv';
// Ensures that env variables are loaded in immediately, before other imports
dotenv.config();
import { Command } from 'commander';
import { getOASMetadata } from './src/services/buildMetadata';
import { buildOpenAPIPages } from './src/services/pageBuilder';
import { ModuleOptions } from './src/types';

const program = new Command();
program
.usage('-- [options]')
.requiredOption('-b, --bundle <path>', 'path to parsed bundle zip')
.requiredOption('-o, --output <path>', 'path to the directory to output generated files')
.requiredOption('--redoc <path>', 'path to the Redoc CLI program to run. Must be a JS file')
.requiredOption('--repo <path>', 'path to repo being built');

program.parse();
const options = program.opts<ModuleOptions>();

const app = async (options: ModuleOptions) => {
const { bundle: bundlePath } = options;
const oasMetadata = getOASMetadata(bundlePath);
if (!oasMetadata) {
console.log('No OpenAPI content pages found.');
return;
}

const oasMetadataEntries = Object.entries(oasMetadata);
const numOASPages = oasMetadataEntries.length;
console.log(`OpenAPI content pages found: ${numOASPages}.`);

await buildOpenAPIPages(oasMetadataEntries, options);
};

app(options)
.then(() => {
console.log('Finished building OpenAPI content pages.');
process.exit(0);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
Loading

0 comments on commit ddea033

Please sign in to comment.