Skip to content
This repository was archived by the owner on Jul 9, 2021. It is now read-only.

Commit 5520224

Browse files
committed
[added] the watch method to the Documentation class
1 parent 5a5aad1 commit 5520224

4 files changed

Lines changed: 36 additions & 14 deletions

File tree

bin/docs-watch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* @flow */
2+
3+
import {Documentation} from '../src/Documentation';
4+
5+
new Documentation().watch();

bin/docs.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
/* @flow */
22

33
import {Documentation} from '../src/Documentation';
4-
import {log, consoleStyles} from '../src/logger';
5-
import {createReadStream, createWriteStream} from 'fs';
6-
import {join} from 'path';
74

8-
const rootDir = join(__dirname, '..'),
9-
docsDir = join(rootDir, 'docs'),
10-
docs = new Documentation(),
11-
{green} = consoleStyles;
12-
13-
docs.run(() => {
14-
createReadStream(join(rootDir, 'LICENSE')).pipe(createWriteStream(join(docsDir, 'LICENSE')));
15-
createReadStream(join(rootDir, 'README.md')).pipe(createWriteStream(join(docsDir, 'README.md')));
16-
log(green('Generated API documentation!'));
17-
});
5+
new Documentation().run();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"scripts": {
1111
"test": "babel-node ./node_modules/.bin/isparta cover _mocha --root src -- -R nyan",
1212
"docs-build": "babel-node bin/docs",
13+
"docs-watch": "babel-node bin/docs-watch",
1314
"lint": "flow && eslint ./",
1415
"build": "babel-node bin/build",
1516
"release": "release"

src/Documentation.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import {join} from 'path';
55
import noop from 'lodash/noop';
66
import {logError} from './logger';
77
import {findBinary} from './findBinary';
8+
import {watch} from './watch';
9+
import tinylr from 'tiny-lr';
810

9-
const cwd = process.cwd(),
11+
const LIVERELOAD_PORT = 35729,
12+
cwd = process.cwd(),
1013
defaultOptions = {
1114
inputDir: join(cwd, 'src'),
1215
outputDir: join(cwd, 'docs'),
@@ -83,4 +86,29 @@ export class Documentation {
8386
});
8487
}
8588

89+
/**
90+
* Watch for changes and automatically re-build the documentation.
91+
*
92+
* Please install, enable and, optionally, allow access to file urls (if you want to be able to browse the generated
93+
* documentation without the need for a server) to the LiveReload browser extension.
94+
*
95+
* @memberof Documentation
96+
* @instance
97+
* @method watch
98+
* @param {Function} [callback=function () {}] - a callback function
99+
*/
100+
watch(callback: () => void = noop) {
101+
const lr = tinylr();
102+
103+
lr.listen(LIVERELOAD_PORT);
104+
105+
watch(this.options.inputDir, 'js', () => {
106+
this.run(() => {
107+
callback();
108+
lr.changed({body: {files: '*'}});
109+
});
110+
});
111+
this.run(callback);
112+
}
113+
86114
}

0 commit comments

Comments
 (0)