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

Commit

Permalink
[added] the watch method to the Documentation class
Browse files Browse the repository at this point in the history
  • Loading branch information
thealjey committed Feb 11, 2017
1 parent 5a5aad1 commit 5520224
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
5 changes: 5 additions & 0 deletions bin/docs-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* @flow */

import {Documentation} from '../src/Documentation';

new Documentation().watch();
14 changes: 1 addition & 13 deletions bin/docs.js
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 29 additions & 1 deletion src/Documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 5520224

Please sign in to comment.