Skip to content

Commit

Permalink
fix: move cli to a separate npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Mar 18, 2018
1 parent ed20ac1 commit 95c7585
Show file tree
Hide file tree
Showing 10 changed files with 1,850 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ stats.json
e2e/.build/
cypress/
bundles
bin/cli.js
cli/index.js

/benchmark/revisions

Expand Down
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
!bundles/
!package.json
!README.md
!bin/cli.js
!README.md
3 changes: 3 additions & 0 deletions cli/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!index.js
!package.json
!README.md
Empty file added cli/README.md
Empty file.
18 changes: 12 additions & 6 deletions bin/cli.ts → cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as zlib from 'zlib';
import { resolve } from 'path';

// @ts-ignore
import { Redoc, loadAndBundleSpec, createStore } from '../';
import { Redoc, loadAndBundleSpec, createStore } from 'redoc';

import { createReadStream, writeFileSync, ReadStream, readFileSync, watch, existsSync } from 'fs';

Expand Down Expand Up @@ -53,7 +53,7 @@ yargs
try {
await serve(argv.port, argv.spec, { ssr: argv.ssr, watch: argv.watch });
} catch (e) {
console.log(e.message);
console.log(e.stack);
}
},
)
Expand Down Expand Up @@ -142,19 +142,24 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
}

async function bundle(pathToSpec, options: Options = {}) {
const start = Date.now();
const spec = await loadAndBundleSpec(pathToSpec);
const pageHTML = await getPageHTML(spec, pathToSpec, { ...options, ssr: true });

writeFileSync(options.output!, pageHTML);
const sizeInKb = Math.ceil(Buffer.byteLength(pageHTML) / 1024);
console.log(`\n🎉 bundled successfully in: ${options.output!} (${sizeInKb} kB)`);
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 }: Options) {
let html, css, state;
let redocStandaloneSrc;
if (ssr) {
console.log('Prerendering docs');
let store = await createStore(spec, pathToSpec);
const store = await createStore(spec, pathToSpec);
const sheet = new ServerStyleSheet();
html = renderToString(sheet.collectStyles(React.createElement(Redoc, { store })));
css = sheet.getStyleTags();
Expand All @@ -165,7 +170,8 @@ async function getPageHTML(spec: any, pathToSpec: string, { ssr, cdn }: Options)
}
}

return `<html>
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>ReDoc</title>
Expand Down
Loading

0 comments on commit 95c7585

Please sign in to comment.