Skip to content

Commit df43cfb

Browse files
author
Melvyn Sopacua
committed
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).
1 parent 97db54a commit df43cfb

File tree

3 files changed

+1136
-150
lines changed

3 files changed

+1136
-150
lines changed

cli/index.ts

+19-32
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { ServerStyleSheet } from 'styled-components';
66

77
import { compile } from 'handlebars';
88
import { createServer, IncomingMessage, ServerResponse } from 'http';
9-
import { dirname, join } from 'path';
9+
import { dirname, join, resolve } from 'path';
1010

1111
import * as zlib from 'zlib';
1212

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

16-
import { createReadStream, existsSync, readFileSync, ReadStream, watch, writeFileSync } from 'fs';
16+
import {watch} from 'chokidar';
17+
import { createReadStream, existsSync, readFileSync, ReadStream, writeFileSync } from 'fs';
1718
import * as mkdirp from 'mkdirp';
1819

1920
import * as YargsParser from 'yargs';
@@ -167,28 +168,25 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
167168
server.listen(port, () => console.log(`Server started: http://127.0.0.1:${port}`));
168169

169170
if (options.watch && existsSync(pathToSpec)) {
170-
const pathToSpecDirectory = dirname(pathToSpec);
171+
const pathToSpecDirectory = resolve(dirname(pathToSpec));
171172
const watchOptions = {
172-
recursive: true,
173+
ignored: /(^|[\/\\])\../,
173174
};
174175

175-
watch(
176-
pathToSpecDirectory,
177-
watchOptions,
178-
debounce(async (event, filename) => {
179-
if (event === 'change' || event === 'rename') {
180-
console.log(`${join(pathToSpecDirectory, filename)} changed, updating docs`);
181-
try {
182-
spec = await loadAndBundleSpec(pathToSpec);
183-
pageHTML = await getPageHTML(spec, pathToSpec, options);
184-
console.log('Updated successfully');
185-
} catch (e) {
186-
console.error('Error while updating: ', e.message);
187-
}
188-
}
189-
}, 2200),
190-
);
191-
console.log(`👀 Watching ${pathToSpecDirectory} for changes...`);
176+
const watcher = watch(pathToSpecDirectory, watchOptions);
177+
const log = console.log.bind(console);
178+
watcher
179+
.on('change', async path => {
180+
log(`${path} changed, updating docs`);
181+
try {
182+
spec = await loadAndBundleSpec(pathToSpec);
183+
pageHTML = await getPageHTML(spec, pathToSpec, options);
184+
log('Updated successfully');
185+
} catch (e) {
186+
console.error('Error while updating: ', e.message);
187+
}})
188+
.on('error', error => console.error(`Watcher error: ${error}`))
189+
.on('ready', () => log(`👀 Watching ${pathToSpecDirectory} for changes...`));
192190
}
193191
}
194192

@@ -291,17 +289,6 @@ function respondWithGzip(
291289
}
292290
}
293291

294-
function debounce(callback: (...args) => void, time: number) {
295-
let interval;
296-
return (...args) => {
297-
clearTimeout(interval);
298-
interval = setTimeout(() => {
299-
interval = null;
300-
callback(...args);
301-
}, time);
302-
};
303-
}
304-
305292
function isURL(str: string): boolean {
306293
return /^(https?:)\/\//m.test(str);
307294
}

cli/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "redoc-cli",
3-
"version": "0.6.4",
3+
"version": "0.7.0",
44
"description": "ReDoc's Command Line Interface",
55
"main": "index.js",
66
"bin": "index.js",
77
"repository": "https://github.com/Rebilly/ReDoc",
88
"author": "Roman Hotsiy <gotsijroman@gmail.com>",
99
"license": "MIT",
1010
"dependencies": {
11+
"chokidar": "^2.0.4",
1112
"handlebars": "^4.0.11",
1213
"isarray": "^2.0.4",
1314
"mkdirp": "^0.5.1",
@@ -26,6 +27,7 @@
2627
"access": "public"
2728
},
2829
"devDependencies": {
30+
"@types/chokidar": "^1.7.5",
2931
"@types/handlebars": "^4.0.39",
3032
"@types/mkdirp": "^0.5.2",
3133
"ci-publish": "^1.3.1"

0 commit comments

Comments
 (0)