webcompiler
Version:
Lint, type-check, compile, package and gzip JavaScript (ES6 + Flow static types + JSX), for the browser as well as NodeJS; lint, compile, auto-prefix, minify and gzip SASS.
80 lines (65 loc) • 2.37 kB
JavaScript
;
exports.__esModule = true;
exports.watch = watch;
var _logger = require('./logger');
var _fbWatchman = require('fb-watchman');
/* eslint-disable camelcase */
const client = new _fbWatchman.Client(),
ALPHANUMERIC_BASE = 36,
{ yellow } = _logger.consoleStyles;
/**
* Using the [Facebook Watchman](https://facebook.github.io/watchman/), watches the directory `dir` for changes of files
* with extension `type` and runs `callback` when a change is detected.
*
* This watcher's only goal is performance, hence the simplicity.
*
* @function watch
* @param {string} dir - a full system path to a directory to watch
* @param {string} type - a file extension
* @param {WatchCallback} callback - a callback function
* @see {@link https://facebook.github.io/watchman/ Watchman}
* @example
* import {watch} from 'webcompiler';
* // or - import {watch} from 'webcompiler/lib/watch';
* // or - var watch = require('webcompiler').watch;
* // or - var watch = require('webcompiler/lib/watch').watch;
* import {join} from 'path';
*
* watch(join(__dirname, 'src'), 'js', someFunction);
*/
function watch(dir, type, callback) {
const subscription = Date.now().toString(ALPHANUMERIC_BASE);
client.capabilityCheck({ optional: [], required: ['relative_root'] }, capabilityErr => {
if (capabilityErr) {
client.end();
return (0, _logger.logError)(capabilityErr);
}
client.command(['watch-project', dir], (watchErr, { watch: watcher, relative_path: relative_root, warning }) => {
if (watchErr) {
return (0, _logger.logError)(watchErr);
}
if (warning) {
(0, _logger.log)(yellow('Warning: ', warning));
}
client.command(['clock', watcher], (clockErr, { clock: since }) => {
if (clockErr) {
return (0, _logger.logError)(clockErr);
}
client.command(['subscribe', watcher, subscription, {
expression: ['suffix', type],
since,
relative_root
}], subscribeErr => {
if (subscribeErr) {
(0, _logger.logError)(subscribeErr);
}
});
client.on('subscription', subscriptionResp => {
if (subscription === subscriptionResp.subscription) {
callback(subscriptionResp);
}
});
});
});
});
}