mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-22 16:46:34 +03:00
refactor(cli): Use chokidar for watching
Caveat: could not get this to work with a debounce. Fixes: recursive file watching on Linux (and thus Docker).
This commit is contained in:
parent
97db54aa2e
commit
df43cfbe64
51
cli/index.ts
51
cli/index.ts
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,6 +8,7 @@
|
||||||
"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",
|
||||||
|
@ -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"
|
||||||
|
|
1231
cli/yarn.lock
1231
cli/yarn.lock
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user