From a115faab97bfa947ff1466873abbb60cfda69e3b Mon Sep 17 00:00:00 2001 From: Ryan Lee Date: Thu, 5 Mar 2020 11:57:45 -0800 Subject: [PATCH] adds watcher support to urls --- cli/index.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cli/index.ts b/cli/index.ts index 8c34bb05..5cf99f22 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -26,6 +26,8 @@ import * as mkdirp from 'mkdirp'; import * as YargsParser from 'yargs'; +import { URL } from 'url'; + interface Options { ssr?: boolean; watch?: boolean; @@ -159,8 +161,16 @@ YargsParser.command( async function serve(port: number, pathToSpec: string, options: Options = {}) { let spec = await loadAndBundleSpec(pathToSpec); let pageHTML = await getPageHTML(spec, pathToSpec, options); + let isUrl; + try { + // tslint:disable-next-line + new URL(pathToSpec); + isUrl = true; + } catch (e) { + isUrl = false; + } - const server = createServer((request, response) => { + const server = createServer(async (request, response) => { console.time('GET ' + request.url); if (request.url === '/redoc.standalone.js') { respondWithGzip( @@ -176,6 +186,12 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) { 'Content-Type': 'text/html', }); } else if (request.url === '/spec.json') { + if (options.watch && isUrl) { + spec = await loadAndBundleSpec(pathToSpec).catch(e => { + console.log('Cannot fetch specs:', e); + return {}; + }); + } const specStr = JSON.stringify(spec, null, 2); respondWithGzip(specStr, request, response, { 'Content-Type': 'application/json',