From 5520224f1f467175070ff92302db53c2e4a6cd2f Mon Sep 17 00:00:00 2001 From: Eugene Kuzmenko Date: Sat, 11 Feb 2017 18:02:00 +0100 Subject: [PATCH] [added] the `watch` method to the `Documentation` class --- bin/docs-watch.js | 5 +++++ bin/docs.js | 14 +------------- package.json | 1 + src/Documentation.js | 30 +++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 bin/docs-watch.js diff --git a/bin/docs-watch.js b/bin/docs-watch.js new file mode 100644 index 0000000..b1eb3e4 --- /dev/null +++ b/bin/docs-watch.js @@ -0,0 +1,5 @@ +/* @flow */ + +import {Documentation} from '../src/Documentation'; + +new Documentation().watch(); diff --git a/bin/docs.js b/bin/docs.js index 1fe4b0d..343beab 100644 --- a/bin/docs.js +++ b/bin/docs.js @@ -1,17 +1,5 @@ /* @flow */ import {Documentation} from '../src/Documentation'; -import {log, consoleStyles} from '../src/logger'; -import {createReadStream, createWriteStream} from 'fs'; -import {join} from 'path'; -const rootDir = join(__dirname, '..'), - docsDir = join(rootDir, 'docs'), - docs = new Documentation(), - {green} = consoleStyles; - -docs.run(() => { - createReadStream(join(rootDir, 'LICENSE')).pipe(createWriteStream(join(docsDir, 'LICENSE'))); - createReadStream(join(rootDir, 'README.md')).pipe(createWriteStream(join(docsDir, 'README.md'))); - log(green('Generated API documentation!')); -}); +new Documentation().run(); diff --git a/package.json b/package.json index 3299b9f..a1a90b6 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "scripts": { "test": "babel-node ./node_modules/.bin/isparta cover _mocha --root src -- -R nyan", "docs-build": "babel-node bin/docs", + "docs-watch": "babel-node bin/docs-watch", "lint": "flow && eslint ./", "build": "babel-node bin/build", "release": "release" diff --git a/src/Documentation.js b/src/Documentation.js index 26174b8..7e496cb 100644 --- a/src/Documentation.js +++ b/src/Documentation.js @@ -5,8 +5,11 @@ import {join} from 'path'; import noop from 'lodash/noop'; import {logError} from './logger'; import {findBinary} from './findBinary'; +import {watch} from './watch'; +import tinylr from 'tiny-lr'; -const cwd = process.cwd(), +const LIVERELOAD_PORT = 35729, + cwd = process.cwd(), defaultOptions = { inputDir: join(cwd, 'src'), outputDir: join(cwd, 'docs'), @@ -83,4 +86,29 @@ export class Documentation { }); } + /** + * Watch for changes and automatically re-build the documentation. + * + * Please install, enable and, optionally, allow access to file urls (if you want to be able to browse the generated + * documentation without the need for a server) to the LiveReload browser extension. + * + * @memberof Documentation + * @instance + * @method watch + * @param {Function} [callback=function () {}] - a callback function + */ + watch(callback: () => void = noop) { + const lr = tinylr(); + + lr.listen(LIVERELOAD_PORT); + + watch(this.options.inputDir, 'js', () => { + this.run(() => { + callback(); + lr.changed({body: {files: '*'}}); + }); + }); + this.run(callback); + } + }