Merge 'mes3fork/watch-backend'

This commit is contained in:
Roman Hotsiy 2018-11-06 10:14:57 +02:00
commit bba40cd34a
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 1007 additions and 863 deletions

View File

@ -6,14 +6,15 @@ import { ServerStyleSheet } from 'styled-components';
import { compile } from 'handlebars'; import { compile } from 'handlebars';
import { createServer, IncomingMessage, ServerResponse } from 'http'; import { createServer, IncomingMessage, ServerResponse } from 'http';
import { dirname, join } from 'path'; import { dirname, join, resolve } from 'path';
import * as zlib from 'zlib'; import * as zlib from 'zlib';
// @ts-ignore // @ts-ignore
import { createStore, loadAndBundleSpec, Redoc } from 'redoc'; import { createStore, loadAndBundleSpec, Redoc } from 'redoc';
import { createReadStream, existsSync, readFileSync, ReadStream, watch, writeFileSync } from 'fs'; import {watch} from 'chokidar';
import { createReadStream, existsSync, readFileSync, ReadStream, writeFileSync } from 'fs';
import * as mkdirp from 'mkdirp'; import * as mkdirp from 'mkdirp';
import * as YargsParser from 'yargs'; import * as YargsParser from 'yargs';
@ -167,28 +168,25 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
server.listen(port, () => console.log(`Server started: http://127.0.0.1:${port}`)); server.listen(port, () => console.log(`Server started: http://127.0.0.1:${port}`));
if (options.watch && existsSync(pathToSpec)) { if (options.watch && existsSync(pathToSpec)) {
const pathToSpecDirectory = dirname(pathToSpec); const pathToSpecDirectory = resolve(dirname(pathToSpec));
const watchOptions = { const watchOptions = {
recursive: true, ignored: /(^|[\/\\])\../,
}; };
watch( const watcher = watch(pathToSpecDirectory, watchOptions);
pathToSpecDirectory, const log = console.log.bind(console);
watchOptions, watcher
debounce(async (event, filename) => { .on('change', async path => {
if (event === 'change' || event === 'rename') { log(`${path} changed, updating docs`);
console.log(`${join(pathToSpecDirectory, filename)} changed, updating docs`); try {
try { spec = await loadAndBundleSpec(pathToSpec);
spec = await loadAndBundleSpec(pathToSpec); pageHTML = await getPageHTML(spec, pathToSpec, options);
pageHTML = await getPageHTML(spec, pathToSpec, options); log('Updated successfully');
console.log('Updated successfully'); } catch (e) {
} catch (e) { console.error('Error while updating: ', e.message);
console.error('Error while updating: ', e.message); }})
} .on('error', error => console.error(`Watcher error: ${error}`))
} .on('ready', () => log(`👀 Watching ${pathToSpecDirectory} for changes...`));
}, 2200),
);
console.log(`👀 Watching ${pathToSpecDirectory} for changes...`);
} }
} }
@ -291,17 +289,6 @@ function respondWithGzip(
} }
} }
function debounce(callback: (...args) => void, time: number) {
let interval;
return (...args) => {
clearTimeout(interval);
interval = setTimeout(() => {
interval = null;
callback(...args);
}, time);
};
}
function isURL(str: string): boolean { function isURL(str: string): boolean {
return /^(https?:)\/\//m.test(str); return /^(https?:)\/\//m.test(str);
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "redoc-cli", "name": "redoc-cli",
"version": "0.6.4", "version": "0.7.0",
"description": "ReDoc's Command Line Interface", "description": "ReDoc's Command Line Interface",
"main": "index.js", "main": "index.js",
"bin": "index.js", "bin": "index.js",
@ -8,13 +8,14 @@
"author": "Roman Hotsiy <gotsijroman@gmail.com>", "author": "Roman Hotsiy <gotsijroman@gmail.com>",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"chokidar": "^2.0.4",
"handlebars": "^4.0.11", "handlebars": "^4.0.11",
"isarray": "^2.0.4", "isarray": "^2.0.4",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"mobx": "^4.2.0", "mobx": "^4.2.0",
"react": "^16.4.2", "react": "^16.4.2",
"react-dom": "^16.4.2", "react-dom": "^16.4.2",
"redoc": "^2.0.0-alpha.37", "redoc": "^2.0.0-alpha.41",
"styled-components": "^4.0.2", "styled-components": "^4.0.2",
"tslib": "^1.9.3", "tslib": "^1.9.3",
"yargs": "^12.0.1" "yargs": "^12.0.1"
@ -26,6 +27,7 @@
"access": "public" "access": "public"
}, },
"devDependencies": { "devDependencies": {
"@types/chokidar": "^1.7.5",
"@types/handlebars": "^4.0.39", "@types/handlebars": "^4.0.39",
"@types/mkdirp": "^0.5.2", "@types/mkdirp": "^0.5.2",
"ci-publish": "^1.3.1" "ci-publish": "^1.3.1"

File diff suppressed because it is too large Load Diff