mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-21 16:16:33 +03:00
chore: remove redoc-cli (#2320)
This commit is contained in:
parent
874b0d6f81
commit
d1c9155ae1
7
.github/CONTRIBUTING.md
vendored
7
.github/CONTRIBUTING.md
vendored
|
@ -30,13 +30,6 @@ After cloning the repo, run:
|
|||
$ npm install # or npm
|
||||
```
|
||||
|
||||
To run the dev server, you will also need to install the cli's dependencies:
|
||||
|
||||
```bash
|
||||
$ cd cli
|
||||
$ npm install # or npm
|
||||
```
|
||||
|
||||
### Commonly used NPM scripts
|
||||
|
||||
``` bash
|
||||
|
|
2
.github/workflows/e2e-tests.yml
vendored
2
.github/workflows/e2e-tests.yml
vendored
|
@ -7,6 +7,6 @@ jobs:
|
|||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm ci && npm ci --prefix cli
|
||||
- run: npm ci
|
||||
- run: npm run bundle
|
||||
- run: npm run e2e
|
||||
|
|
40
.github/workflows/main.yml
vendored
40
.github/workflows/main.yml
vendored
|
@ -3,46 +3,6 @@ on:
|
|||
release:
|
||||
types: [published]
|
||||
jobs:
|
||||
# push_to_registry:
|
||||
# name: Push Docker image to GitHub Packages
|
||||
# runs-on: ubuntu-latest
|
||||
# permissions:
|
||||
# packages: write
|
||||
# contents: read
|
||||
# steps:
|
||||
# - name: Check out the repo
|
||||
# uses: actions/checkout@v3
|
||||
# - name: Login to GitHub Container Registry
|
||||
# uses: docker/login-action@v1
|
||||
# with:
|
||||
# registry: ghcr.io
|
||||
# username: ${{ github.repository_owner }}
|
||||
# password: ${{ secrets.GITHUB_TOKEN }}
|
||||
# - name: Prepare
|
||||
# id: prep
|
||||
# run: |
|
||||
# DOCKER_IMAGE=ghcr.io/redocly/redoc/cli
|
||||
# VERSION=edge
|
||||
# if [[ $GITHUB_REF == refs/tags/* ]]; then
|
||||
# VERSION=${GITHUB_REF#refs/tags/}
|
||||
# elif [[ $GITHUB_REF == refs/heads/* ]]; then
|
||||
# VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
|
||||
# elif [[ $GITHUB_REF == refs/pull/* ]]; then
|
||||
# VERSION=pr-${{ github.event.number }}
|
||||
# fi
|
||||
# TAGS="${DOCKER_IMAGE}:${VERSION},${DOCKER_IMAGE}:latest"
|
||||
# if [ "${{ github.event_name }}" = "push" ]; then
|
||||
# TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
|
||||
# fi
|
||||
# echo ::set-output name=version::${VERSION}
|
||||
# echo ::set-output name=tags::${TAGS}
|
||||
# echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
||||
# - name: Push to GitHub Packages
|
||||
# uses: docker/build-push-action@v3
|
||||
# with:
|
||||
# context: ./cli
|
||||
# push: ${{ github.event_name != 'pull_request' }}
|
||||
# tags: ${{ steps.prep.outputs.tags }}
|
||||
dockerhub:
|
||||
name: Publish redoc image to DockerHub
|
||||
runs-on: ubuntu-latest
|
||||
|
|
120
.github/workflows/publish-cli.yml
vendored
120
.github/workflows/publish-cli.yml
vendored
|
@ -1,120 +0,0 @@
|
|||
name: Publish cli
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
bundle:
|
||||
needs: [check-version-cli]
|
||||
if: needs.check-version-cli.outputs.changed == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v3
|
||||
- name: Cache node modules
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
|
||||
key: npm-${{ hashFiles('package-lock.json') }}
|
||||
restore-keys: |
|
||||
npm-${{ hashFiles('package-lock.json') }}
|
||||
npm-
|
||||
- run: npm ci && npm ci --prefix cli
|
||||
- run: npm run bundle
|
||||
- name: Store bundle artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: bundles-cli
|
||||
path: bundles
|
||||
retention-days: 1
|
||||
unit-tests:
|
||||
needs: [check-version-cli]
|
||||
if: needs.check-version-cli.outputs.changed == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm ci && npm ci --prefix cli
|
||||
- run: npm test
|
||||
e2e-tests:
|
||||
needs: [bundle]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm ci && npm ci --prefix cli
|
||||
- name: Download bundled artifact
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: bundles-cli
|
||||
path: bundles
|
||||
- run: npm run e2e
|
||||
bundle-cli:
|
||||
needs: [check-version-cli]
|
||||
if: needs.check-version-cli.outputs.changed == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v3
|
||||
- name: Cache node modules
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.npm
|
||||
key: npm-${{ hashFiles('package-lock.json') }}
|
||||
restore-keys: |
|
||||
npm-${{ hashFiles('package-lock.json') }}
|
||||
npm-
|
||||
- name: Install dependencies
|
||||
run: npm ci && npm ci --prefix cli
|
||||
- name: Bundle
|
||||
run: npm run compile:cli
|
||||
- name: Store bundle artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: cli
|
||||
path: cli
|
||||
retention-days: 1
|
||||
check-version-cli:
|
||||
name: Check Version
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
changed: ${{ steps.check.outputs.changed }}
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v3
|
||||
- name: Check if version has been updated
|
||||
id: check
|
||||
uses: EndBug/version-check@v2.0.1
|
||||
with:
|
||||
file-name: ./cli/package.json
|
||||
file-url: https://unpkg.com/redoc-cli/package.json
|
||||
static-checking: localIsNew
|
||||
publish-cli:
|
||||
needs: [bundle-cli, unit-tests, e2e-tests]
|
||||
if: needs.check-version-cli.outputs.changed == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '14.x'
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
- uses: actions/checkout@v3
|
||||
- name: Download cli bundled artifact
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: cli
|
||||
path: cli
|
||||
- name: Cache node modules
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
|
||||
key: npm-${{ hashFiles('package-lock.json') }}
|
||||
restore-keys: |
|
||||
npm-${{ hashFiles('package-lock.json') }}
|
||||
npm-
|
||||
- name: Publish to NPM
|
||||
run: cd cli/ && npm publish
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
6
.github/workflows/publish.yml
vendored
6
.github/workflows/publish.yml
vendored
|
@ -19,7 +19,7 @@ jobs:
|
|||
restore-keys: |
|
||||
npm-${{ hashFiles('package-lock.json') }}
|
||||
npm-
|
||||
- run: npm ci && npm ci --prefix cli
|
||||
- run: npm ci
|
||||
- run: npm run bundle
|
||||
- name: Store bundle artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
|
@ -31,14 +31,14 @@ jobs:
|
|||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm ci && npm ci --prefix cli
|
||||
- run: npm ci
|
||||
- run: npm test
|
||||
e2e-tests:
|
||||
needs: [bundle]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm ci && npm ci --prefix cli
|
||||
- run: npm ci
|
||||
- name: Download bundled artifact
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
|
|
2
.github/workflows/unit-tests.yml
vendored
2
.github/workflows/unit-tests.yml
vendored
|
@ -7,6 +7,6 @@ jobs:
|
|||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm ci && npm ci --prefix cli
|
||||
- run: npm ci
|
||||
- run: npm run bundle
|
||||
- run: npm test
|
||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -27,8 +27,6 @@ cypress/
|
|||
bundles/
|
||||
typings/*
|
||||
!typings/styled-patch.d.ts
|
||||
cli/index.js
|
||||
cli/__test__/*/**/*.html
|
||||
|
||||
/benchmark/revisions
|
||||
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
*/
|
||||
!index.js
|
||||
!package.json
|
||||
!README.md
|
|
@ -1,23 +0,0 @@
|
|||
# Package the 'redoc-cli' as a docker image.
|
||||
#
|
||||
# To build:
|
||||
# $ cd <Redoc project directory>
|
||||
# $ docker build -t redoc-cli -f cli/Dockerfile .
|
||||
#
|
||||
# To run:
|
||||
# To display the command line options:
|
||||
# $ docker run --rm -it redoc-cli --help
|
||||
# .. will display the command line help
|
||||
#
|
||||
# To turn `swagger.yml` file in the current directory, to html documentation 'redoc-static.html'
|
||||
# $ docker run --rm -it -v $PWD:/data redoc-cli bundle swagger.yml
|
||||
|
||||
FROM node:alpine
|
||||
|
||||
RUN npm install -g redoc-cli
|
||||
|
||||
WORKDIR /data
|
||||
EXPOSE 8080
|
||||
|
||||
ENTRYPOINT ["redoc-cli"]
|
||||
CMD []
|
|
@ -1,42 +0,0 @@
|
|||
# redoc-cli
|
||||
|
||||
**DEPRECATED: this package is deprecated. Use `npx @redocly/cli build-docs <api>` instead.**
|
||||
|
||||
**[ReDoc](https://github.com/Redocly/redoc)'s Command Line Interface**
|
||||
|
||||
## Installation
|
||||
|
||||
You can use `redoc-cli` by installing [the package](https://www.npmjs.com/package/redoc-cli) globally,
|
||||
or using [npx](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b).
|
||||
|
||||
## Usage
|
||||
|
||||
The two following commands are available:
|
||||
|
||||
- `redoc-cli serve [spec]` - starts the server with `spec` rendered with ReDoc.
|
||||
Supports a server-side rendering mode (`--ssr`)
|
||||
and can watch the spec (`--watch`) to automatically reload the page whenever it changes.\
|
||||
Deprecated. Use `npx @redocly/cli preview-docs [spec]`
|
||||
- `redoc-cli bundle [spec]` - bundles `spec` and Redoc into a **zero-dependency** HTML file.\
|
||||
Deprecated. Use "build" command instead.
|
||||
- `redoc-cli build [spec]` - build `spec` and Redoc into a **zero-dependency** HTML file.
|
||||
|
||||
Some examples:
|
||||
|
||||
- Bundle with the main color changed to `orange`:<br/>
|
||||
`$ redoc-cli build [spec] --options.theme.colors.primary.main=orange`
|
||||
- Serve with the `nativeScrollbars` option set to true:<br/>
|
||||
`$ redoc-cli serve [spec] --options.nativeScrollbars`
|
||||
- Bundle using a custom [Handlebars](https://handlebarsjs.com/) template
|
||||
(check the [default template](https://github.com/Redocly/redoc/blob/main/cli/template.hbs) for an example):<br/>
|
||||
`$ redoc-cli build [spec] -t custom.hbs`
|
||||
- Bundle using a custom template and add custom `templateOptions`:<br/>
|
||||
`$ redoc-cli build [spec] -t custom.hbs --templateOptions.metaDescription "Page meta description"`
|
||||
|
||||
#### With a Redocly configuration file ([more info](https://redocly.com/docs/cli/configuration/#redocly-configuration-file)):
|
||||
|
||||
1. Go to folder with your Redocly configuration file (`.redocly.yaml` or `redocly.yaml`) and your OpenAPI definition file.
|
||||
2. Build the site using the `build` command (options from the Redocly configuration file will be automatically fetched):
|
||||
`redoc build openapi.yaml`
|
||||
|
||||
For more details, run `redoc-cli --help`.
|
|
@ -1,2 +0,0 @@
|
|||
features.openapi:
|
||||
disableSearch: true
|
|
@ -1,30 +0,0 @@
|
|||
import { spawnSync } from 'child_process';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
describe('build', () => {
|
||||
it('should use .redocly.yaml', () => {
|
||||
const r = spawnSync(
|
||||
'ts-node',
|
||||
['../../../index.ts', 'build', ' ../../../../demo/openapi.yaml', '--output=redoc-test.html'],
|
||||
{
|
||||
cwd: __dirname,
|
||||
shell: true,
|
||||
},
|
||||
);
|
||||
|
||||
const out = r.stdout.toString('utf-8');
|
||||
const err = r.stderr.toString('utf-8');
|
||||
const result = `${out}\n${err}`;
|
||||
|
||||
try {
|
||||
const redocStaticFile = readFileSync(`${__dirname}/redoc-test.html`, 'utf8');
|
||||
expect(redocStaticFile).toContain('"options":{"disableSearch":true}');
|
||||
expect(redocStaticFile).not.toContain('role="search"');
|
||||
} catch (err) {
|
||||
expect(err.toString()).toContain('{"options":{"disableSearch":"true"}');
|
||||
}
|
||||
|
||||
expect(result).toContain('Found .redocly.yaml and using features.openapi options');
|
||||
expect(result).toContain('bundled successfully');
|
||||
});
|
||||
});
|
|
@ -1,34 +0,0 @@
|
|||
import { spawnSync } from 'child_process';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
describe('build with inline options', () => {
|
||||
it('should use inline options and ignore .redocly.yaml', () => {
|
||||
const r = spawnSync(
|
||||
'ts-node',
|
||||
[
|
||||
'../../../index.ts',
|
||||
'build',
|
||||
' ../../../../demo/openapi.yaml',
|
||||
'--options.disableSearch="false" ',
|
||||
],
|
||||
{
|
||||
cwd: __dirname,
|
||||
shell: true,
|
||||
},
|
||||
);
|
||||
|
||||
const out = r.stdout.toString('utf-8');
|
||||
const err = r.stderr.toString('utf-8');
|
||||
const result = `${out}\n${err}`;
|
||||
expect(result).not.toContain('Found .redocly.yaml and using features.openapi options');
|
||||
expect(result).toContain('bundled successfully');
|
||||
|
||||
try {
|
||||
const redocStaticFile = readFileSync(`${__dirname}/redoc-static.html`, 'utf8');
|
||||
expect(redocStaticFile).toContain('"options":{"disableSearch":"false"}');
|
||||
expect(redocStaticFile).toContain('role="search"');
|
||||
} catch (err) {
|
||||
expect(err.toString()).toContain('"options":{"disableSearch":"false"}');
|
||||
}
|
||||
});
|
||||
});
|
|
@ -1,24 +0,0 @@
|
|||
import { spawnSync } from 'child_process';
|
||||
|
||||
describe('build with url', () => {
|
||||
it('should not fail on resolving url', () => {
|
||||
const r = spawnSync(
|
||||
'ts-node',
|
||||
[
|
||||
'../../../index.ts',
|
||||
'build',
|
||||
'http://petstore.swagger.io/v2/swagger.json',
|
||||
'--output=url-test.html',
|
||||
],
|
||||
{
|
||||
cwd: __dirname,
|
||||
shell: true,
|
||||
},
|
||||
);
|
||||
|
||||
const out = r.stdout.toString('utf-8');
|
||||
const err = r.stderr.toString('utf-8');
|
||||
const result = `${out}\n${err}`;
|
||||
expect(result).toContain('bundled successfully');
|
||||
});
|
||||
});
|
490
cli/index.ts
490
cli/index.ts
|
@ -1,490 +0,0 @@
|
|||
#!/usr/bin/env node
|
||||
/* tslint:disable:no-implicit-dependencies */
|
||||
import { createElement } from 'react';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import { ServerStyleSheet } from 'styled-components';
|
||||
|
||||
import { compile } from 'handlebars';
|
||||
import { createServer, IncomingMessage, ServerResponse } from 'http';
|
||||
import { dirname, join, resolve, extname as getExtName } from 'path';
|
||||
|
||||
import * as zlib from 'zlib';
|
||||
import * as boxen from 'boxen';
|
||||
|
||||
// @ts-ignore
|
||||
import { createStore, loadAndBundleSpec, Redoc } from 'redoc';
|
||||
|
||||
import { watch } from 'chokidar';
|
||||
import {
|
||||
createReadStream,
|
||||
existsSync,
|
||||
lstatSync,
|
||||
readFileSync,
|
||||
ReadStream,
|
||||
writeFileSync,
|
||||
} from 'fs';
|
||||
import * as mkdirp from 'mkdirp';
|
||||
|
||||
import * as YargsParser from 'yargs';
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||
import { findConfig } from '@redocly/openapi-core';
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||
import { parseYaml } from '@redocly/openapi-core';
|
||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
||||
import { Config } from '@redocly/openapi-core';
|
||||
|
||||
interface Options {
|
||||
ssr?: boolean;
|
||||
watch?: boolean;
|
||||
cdn?: boolean;
|
||||
output?: string;
|
||||
title?: string;
|
||||
disableGoogleFont?: boolean;
|
||||
port?: number;
|
||||
templateFileName?: string;
|
||||
templateOptions?: any;
|
||||
redocOptions?: any;
|
||||
}
|
||||
|
||||
export const mimeTypes = {
|
||||
'.html': 'text/html',
|
||||
'.js': 'text/javascript',
|
||||
'.css': 'text/css',
|
||||
'.json': 'application/json',
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpg',
|
||||
'.gif': 'image/gif',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.wav': 'audio/wav',
|
||||
'.mp4': 'video/mp4',
|
||||
'.woff': 'application/font-woff',
|
||||
'.ttf': 'application/font-ttf',
|
||||
'.eot': 'application/vnd.ms-fontobject',
|
||||
'.otf': 'application/font-otf',
|
||||
'.wasm': 'application/wasm',
|
||||
};
|
||||
|
||||
const BUNDLES_DIR = dirname(require.resolve('redoc'));
|
||||
|
||||
const boxenOptions = {
|
||||
title: 'DEPRECATED',
|
||||
titleAlignment: 'center',
|
||||
padding: 1,
|
||||
margin: 1,
|
||||
borderColor: 'red',
|
||||
} as boxen.Options;
|
||||
|
||||
const builderForBuildCommand = yargs => {
|
||||
yargs.positional('spec', {
|
||||
describe: 'path or URL to your spec',
|
||||
});
|
||||
|
||||
yargs.option('o', {
|
||||
describe: 'Output file',
|
||||
alias: 'output',
|
||||
type: 'string',
|
||||
default: 'redoc-static.html',
|
||||
});
|
||||
|
||||
yargs.options('title', {
|
||||
describe: 'Page Title',
|
||||
type: 'string',
|
||||
});
|
||||
|
||||
yargs.options('disableGoogleFont', {
|
||||
describe: 'Disable Google Font',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
});
|
||||
|
||||
yargs.option('cdn', {
|
||||
describe: 'Do not include ReDoc source code into html page, use link to CDN instead',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
});
|
||||
|
||||
yargs.demandOption('spec');
|
||||
return yargs;
|
||||
};
|
||||
|
||||
const handlerForBuildCommand = async (argv: any) => {
|
||||
const config = {
|
||||
ssr: true,
|
||||
output: argv.o as string,
|
||||
cdn: argv.cdn as boolean,
|
||||
title: argv.title as string,
|
||||
disableGoogleFont: argv.disableGoogleFont as boolean,
|
||||
templateFileName: argv.template as string,
|
||||
templateOptions: argv.templateOptions || {},
|
||||
redocOptions: getObjectOrJSON(argv.options),
|
||||
};
|
||||
|
||||
try {
|
||||
await bundle(argv.spec, config);
|
||||
} catch (e) {
|
||||
handleError(e);
|
||||
}
|
||||
};
|
||||
|
||||
YargsParser.command(
|
||||
'serve <spec>',
|
||||
'start the server [deprecated]',
|
||||
yargs => {
|
||||
yargs.positional('spec', {
|
||||
describe: 'path or URL to your spec',
|
||||
});
|
||||
|
||||
yargs.options('title', {
|
||||
describe: 'Page Title',
|
||||
type: 'string',
|
||||
});
|
||||
|
||||
yargs.option('s', {
|
||||
alias: 'ssr',
|
||||
describe: 'Enable server-side rendering',
|
||||
type: 'boolean',
|
||||
});
|
||||
|
||||
yargs.option('h', {
|
||||
alias: 'host',
|
||||
type: 'string',
|
||||
default: '127.0.0.1',
|
||||
});
|
||||
|
||||
yargs.option('p', {
|
||||
alias: 'port',
|
||||
type: 'number',
|
||||
default: 8080,
|
||||
});
|
||||
|
||||
yargs.option('w', {
|
||||
alias: 'watch',
|
||||
type: 'boolean',
|
||||
});
|
||||
|
||||
yargs.options('disable-google-font', {
|
||||
describe: 'Disable Google Font',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
});
|
||||
|
||||
yargs.demandOption('spec');
|
||||
return yargs;
|
||||
},
|
||||
async argv => {
|
||||
const config: Options = {
|
||||
ssr: argv.ssr as boolean,
|
||||
title: argv.title as string,
|
||||
watch: argv.watch as boolean,
|
||||
disableGoogleFont: argv.disableGoogleFont as boolean,
|
||||
templateFileName: argv.template as string,
|
||||
templateOptions: argv.templateOptions || {},
|
||||
redocOptions: getObjectOrJSON(argv.options),
|
||||
};
|
||||
|
||||
try {
|
||||
await serve(argv.host as string, argv.port as number, argv.spec as string, config);
|
||||
} catch (e) {
|
||||
handleError(e);
|
||||
}
|
||||
},
|
||||
[
|
||||
res => {
|
||||
console.log(`
|
||||
${boxen(
|
||||
'This package is deprecated.\n\nUse `npx @redocly/cli preview-docs <api>` instead.',
|
||||
boxenOptions,
|
||||
)}`);
|
||||
return res;
|
||||
},
|
||||
],
|
||||
true,
|
||||
)
|
||||
.command(
|
||||
'build <spec>',
|
||||
'build definition into zero-dependency HTML-file [deprecated]',
|
||||
builderForBuildCommand,
|
||||
handlerForBuildCommand,
|
||||
[notifyDeprecation],
|
||||
true,
|
||||
)
|
||||
.command(
|
||||
'bundle <spec>',
|
||||
'bundle spec into zero-dependency HTML-file [deprecated]',
|
||||
builderForBuildCommand,
|
||||
handlerForBuildCommand,
|
||||
[notifyDeprecation],
|
||||
true,
|
||||
)
|
||||
.demandCommand()
|
||||
.options('t', {
|
||||
alias: 'template',
|
||||
describe: 'Path to handlebars page template, see https://git.io/vh8fP for the example ',
|
||||
type: 'string',
|
||||
})
|
||||
.options('templateOptions', {
|
||||
describe:
|
||||
'Additional options that you want pass to template. Use dot notation, e.g. templateOptions.metaDescription',
|
||||
})
|
||||
.options('options', {
|
||||
describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars',
|
||||
}).argv;
|
||||
|
||||
async function serve(host: string, port: number, pathToSpec: string, options: Options = {}) {
|
||||
let spec = await loadAndBundleSpec(isURL(pathToSpec) ? pathToSpec : resolve(pathToSpec));
|
||||
let pageHTML = await getPageHTML(spec, pathToSpec, options);
|
||||
const server = createServer((request, response) => {
|
||||
console.time('GET ' + request.url);
|
||||
if (request.url === '/redoc.standalone.js') {
|
||||
respondWithGzip(
|
||||
createReadStream(join(BUNDLES_DIR, 'redoc.standalone.js'), 'utf8'),
|
||||
request,
|
||||
response,
|
||||
{
|
||||
'Content-Type': 'application/javascript',
|
||||
},
|
||||
);
|
||||
} else if (request.url === '/') {
|
||||
respondWithGzip(pageHTML, request, response, {
|
||||
'Content-Type': 'text/html',
|
||||
});
|
||||
} else if (request.url === '/spec.json') {
|
||||
const specStr = JSON.stringify(spec, null, 2);
|
||||
respondWithGzip(specStr, request, response, {
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
const filePath = join(dirname(pathToSpec), request.url || '');
|
||||
const extname = String(getExtName(filePath)).toLowerCase() as keyof typeof mimeTypes;
|
||||
|
||||
const contentType = mimeTypes[extname] || 'application/octet-stream';
|
||||
respondWithGzip(createReadStream(filePath), request, response, {
|
||||
'Content-Type': contentType,
|
||||
});
|
||||
} catch (e) {
|
||||
response.writeHead(404);
|
||||
response.write('Not found');
|
||||
response.end();
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd('GET ' + request.url);
|
||||
});
|
||||
|
||||
console.log();
|
||||
|
||||
server.listen(port, host, () => console.log(`Server started: http://${host}:${port}`));
|
||||
|
||||
if (options.watch && existsSync(pathToSpec)) {
|
||||
const pathToSpecDirectory = resolve(dirname(pathToSpec));
|
||||
const watchOptions = {
|
||||
ignored: [/(^|[\/\\])\../, /___jb_[a-z]+___$/],
|
||||
ignoreInitial: true,
|
||||
};
|
||||
|
||||
const watcher = watch(pathToSpecDirectory, watchOptions);
|
||||
const log = console.log.bind(console);
|
||||
|
||||
const handlePath = async _path => {
|
||||
try {
|
||||
spec = await loadAndBundleSpec(resolve(pathToSpec));
|
||||
pageHTML = await getPageHTML(spec, pathToSpec, options);
|
||||
log('Updated successfully');
|
||||
} catch (e) {
|
||||
console.error('Error while updating: ', e.message);
|
||||
}
|
||||
};
|
||||
|
||||
watcher
|
||||
.on('change', async path => {
|
||||
log(`${path} changed, updating docs`);
|
||||
handlePath(path);
|
||||
})
|
||||
.on('add', async path => {
|
||||
log(`File ${path} added, updating docs`);
|
||||
handlePath(path);
|
||||
})
|
||||
.on('addDir', path => {
|
||||
log(`↗ Directory ${path} added. Files in here will trigger reload.`);
|
||||
})
|
||||
.on('error', error => console.error(`Watcher error: ${error}`))
|
||||
.on('ready', () => log(`👀 Watching ${pathToSpecDirectory} for changes...`));
|
||||
}
|
||||
}
|
||||
|
||||
async function bundle(pathToSpec, options: Options = {}) {
|
||||
const start = Date.now();
|
||||
const spec = await loadAndBundleSpec(isURL(pathToSpec) ? pathToSpec : resolve(pathToSpec));
|
||||
const pageHTML = await getPageHTML(spec, pathToSpec, { ...options, ssr: true });
|
||||
|
||||
mkdirp.sync(dirname(options.output!));
|
||||
writeFileSync(options.output!, pageHTML);
|
||||
const sizeInKiB = Math.ceil(Buffer.byteLength(pageHTML) / 1024);
|
||||
const time = Date.now() - start;
|
||||
console.log(
|
||||
`\n🎉 bundled successfully in: ${options.output!} (${sizeInKiB} KiB) [⏱ ${time / 1000}s]`,
|
||||
);
|
||||
}
|
||||
|
||||
async function getPageHTML(
|
||||
spec: any,
|
||||
pathToSpec: string,
|
||||
{
|
||||
ssr,
|
||||
cdn,
|
||||
title,
|
||||
disableGoogleFont,
|
||||
templateFileName,
|
||||
templateOptions,
|
||||
redocOptions = {},
|
||||
}: Options,
|
||||
) {
|
||||
let html;
|
||||
let css;
|
||||
let state;
|
||||
let redocStandaloneSrc;
|
||||
if (ssr) {
|
||||
console.log('Prerendering docs');
|
||||
|
||||
const specUrl = redocOptions.specUrl || (isURL(pathToSpec) ? pathToSpec : undefined);
|
||||
const store = await createStore(spec, specUrl, redocOptions);
|
||||
const sheet = new ServerStyleSheet();
|
||||
// @ts-ignore
|
||||
html = renderToString(sheet.collectStyles(createElement(Redoc, { store })));
|
||||
css = sheet.getStyleTags();
|
||||
state = await store.toJS();
|
||||
|
||||
if (!cdn) {
|
||||
redocStandaloneSrc = readFileSync(join(BUNDLES_DIR, 'redoc.standalone.js'));
|
||||
}
|
||||
}
|
||||
|
||||
templateFileName = templateFileName ? templateFileName : join(__dirname, './template.hbs');
|
||||
const template = compile(readFileSync(templateFileName).toString());
|
||||
return template({
|
||||
redocHTML: `
|
||||
<div id="redoc">${(ssr && html) || ''}</div>
|
||||
<script>
|
||||
${(ssr && `const __redoc_state = ${sanitizeJSONString(JSON.stringify(state))};`) || ''}
|
||||
|
||||
var container = document.getElementById('redoc');
|
||||
Redoc.${
|
||||
ssr
|
||||
? 'hydrate(__redoc_state, container)'
|
||||
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
|
||||
};
|
||||
|
||||
</script>`,
|
||||
redocHead: ssr
|
||||
? (cdn
|
||||
? '<script src="https://unpkg.com/redoc@latest/bundles/redoc.standalone.js"></script>'
|
||||
: `<script>${redocStandaloneSrc}</script>`) + css
|
||||
: '<script src="redoc.standalone.js"></script>',
|
||||
title: title || spec.info.title || 'ReDoc documentation',
|
||||
disableGoogleFont,
|
||||
templateOptions,
|
||||
});
|
||||
}
|
||||
|
||||
// credits: https://stackoverflow.com/a/9238214/1749888
|
||||
function respondWithGzip(
|
||||
contents: string | ReadStream,
|
||||
request: IncomingMessage,
|
||||
response: ServerResponse,
|
||||
headers = {},
|
||||
) {
|
||||
let compressedStream;
|
||||
const acceptEncoding = (request.headers['accept-encoding'] as string) || '';
|
||||
if (acceptEncoding.match(/\bdeflate\b/)) {
|
||||
response.writeHead(200, { ...headers, 'content-encoding': 'deflate' });
|
||||
compressedStream = zlib.createDeflate();
|
||||
} else if (acceptEncoding.match(/\bgzip\b/)) {
|
||||
response.writeHead(200, { ...headers, 'content-encoding': 'gzip' });
|
||||
compressedStream = zlib.createGzip();
|
||||
} else {
|
||||
response.writeHead(200, headers);
|
||||
if (typeof contents === 'string') {
|
||||
response.write(contents);
|
||||
response.end();
|
||||
} else {
|
||||
contents.pipe(response);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof contents === 'string') {
|
||||
compressedStream.write(contents);
|
||||
compressedStream.pipe(response);
|
||||
compressedStream.end();
|
||||
return;
|
||||
} else {
|
||||
contents.pipe(compressedStream).pipe(response);
|
||||
}
|
||||
}
|
||||
|
||||
function isURL(str: string): boolean {
|
||||
return /^(https?:)\/\//m.test(str);
|
||||
}
|
||||
|
||||
function sanitizeJSONString(str: string) {
|
||||
return escapeClosingScriptTag(escapeUnicode(str));
|
||||
}
|
||||
|
||||
// see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
|
||||
function escapeClosingScriptTag(str) {
|
||||
return str.replace(/<\/script>/g, '<\\/script>');
|
||||
}
|
||||
|
||||
// see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
|
||||
function escapeUnicode(str) {
|
||||
return str.replace(/\u2028|\u2029/g, m => '\\u202' + (m === '\u2028' ? '8' : '9'));
|
||||
}
|
||||
|
||||
function handleError(error: Error) {
|
||||
console.error(error.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
function getObjectOrJSON(options) {
|
||||
switch (typeof options) {
|
||||
case 'object':
|
||||
return options;
|
||||
case 'string':
|
||||
try {
|
||||
if (existsSync(options) && lstatSync(options).isFile()) {
|
||||
return JSON.parse(readFileSync(options, 'utf-8'));
|
||||
} else {
|
||||
return JSON.parse(options);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(
|
||||
`Encountered error:\n\n${options}\n\nis neither a file with a valid JSON object neither a stringified JSON object.`,
|
||||
);
|
||||
handleError(e);
|
||||
}
|
||||
default:
|
||||
const configFile = findConfig();
|
||||
if (configFile) {
|
||||
console.log(`Found ${configFile} and using features.openapi options`);
|
||||
try {
|
||||
const config = parseYaml(readFileSync(configFile, 'utf-8')) as Config;
|
||||
|
||||
return config['features.openapi'];
|
||||
} catch (e) {
|
||||
console.warn(`Found ${configFile} but failed to parse: ${e.message}`);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function notifyDeprecation(res: YargsParser.Arguments): YargsParser.Arguments {
|
||||
console.log(
|
||||
boxen(
|
||||
'This package is deprecated.\n\nUse `npx @redocly/cli build-docs <api>` instead.',
|
||||
boxenOptions,
|
||||
),
|
||||
);
|
||||
return res;
|
||||
}
|
5651
cli/npm-shrinkwrap.json
generated
5651
cli/npm-shrinkwrap.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -1,32 +0,0 @@
|
|||
{
|
||||
"name": "redoc-cli",
|
||||
"version": "0.13.21",
|
||||
"description": "ReDoc's Command Line Interface",
|
||||
"main": "index.js",
|
||||
"bin": "index.js",
|
||||
"repository": "https://github.com/Redocly/redoc",
|
||||
"author": "Roman Hotsiy <gotsijroman@gmail.com>",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=12.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"boxen": "5.1.2",
|
||||
"chokidar": "^3.5.1",
|
||||
"handlebars": "^4.7.7",
|
||||
"mkdirp": "^1.0.4",
|
||||
"mobx": "^6.3.2",
|
||||
"node-libs-browser": "^2.2.1",
|
||||
"react": "^17.0.1",
|
||||
"react-dom": "^17.0.1",
|
||||
"redoc": "2.0.0",
|
||||
"styled-components": "^5.3.0",
|
||||
"yargs": "^17.3.1"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mkdirp": "^1.0.1"
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf8" />
|
||||
<title>{{title}}</title>
|
||||
<!-- needed for adaptive design -->
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style>
|
||||
body {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
</style>
|
||||
{{{redocHead}}}
|
||||
{{#unless disableGoogleFont}}<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">{{/unless}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{{{redocHTML}}}
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -44,15 +44,14 @@
|
|||
"bundle:standalone": "webpack --env production --env standalone --mode=production",
|
||||
"bundle:lib": "webpack --mode=production && npm run declarations",
|
||||
"bundle:browser": "webpack --env production --env browser --mode=production",
|
||||
"bundle": "npm run bundle:clean && npm run bundle:lib && npm run bundle:browser && npm run bundle:standalone && npm run compile:cli",
|
||||
"bundle": "npm run bundle:clean && npm run bundle:lib && npm run bundle:browser && npm run bundle:standalone",
|
||||
"declarations": "tsc --emitDeclarationOnly -p tsconfig.lib.json && cp -R src/types typings/",
|
||||
"stats": "webpack --env production --env standalone --json --profile --mode=production > stats.json",
|
||||
"prettier": "prettier --write \"cli/index.ts\" \"src/**/*.{ts,tsx}\"",
|
||||
"prettier": "prettier --write \"src/**/*.{ts,tsx}\"",
|
||||
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
|
||||
"lint": "eslint --fix 'src/**/*.{js,ts,tsx}' --cache",
|
||||
"benchmark": "node ./benchmark/benchmark.js",
|
||||
"start:demo": "webpack serve --hot --config demo/webpack.config.ts --mode=development",
|
||||
"compile:cli": "tsc custom.d.ts cli/index.ts --target es6 --module commonjs --types yargs",
|
||||
"build:demo": "webpack --mode=production --config demo/webpack.config.ts",
|
||||
"publish-cdn": "scripts/publish-cdn.sh",
|
||||
"deploy:demo": "aws s3 sync demo/dist s3://production-redoc-demo --acl=public-read",
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
"compileOnSave": false,
|
||||
"exclude": ["node_modules", ".tmp", "lib", "e2e/**"],
|
||||
"include": [
|
||||
"cli/index.ts",
|
||||
"./custom.d.ts",
|
||||
"./demo/playground/hmr-playground.tsx",
|
||||
"./src/**/*.ts?",
|
||||
|
|
Loading…
Reference in New Issue
Block a user