UNPKG

indexr

Version:

Automated dynamic indexes for your ES6+ source code.

68 lines (49 loc) 2.12 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.watchFiles = undefined; var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar); var _ensureArray = require('../../utils/ensureArray'); var _ensureArray2 = _interopRequireDefault(_ensureArray); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _throttle = require('lodash/throttle'); var _throttle2 = _interopRequireDefault(_throttle); var _chainMap = require('../../utils/chainMap'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var watchFiles = exports.watchFiles = function watchFiles(config) { return function () { for (var _len = arguments.length, fns = Array(_len), _key = 0; _key < _len; _key++) { fns[_key] = arguments[_key]; } return function (next) { // Destructure vars var outputFilename = config.outputFilename; var modules = config.modules; var watchGlobs = config.watch; // Ignore the output files from the process to avoid infinite repeat var nullStringIfFalsy = function nullStringIfFalsy(value) { return value || ''; }; var addOutputFilename = function addOutputFilename(value) { return _path2.default.join(value, outputFilename); }; var ignored = (0, _ensureArray2.default)(modules).map(nullStringIfFalsy).map(addOutputFilename); // Run once if watching was not required var watchFunc = _chainMap.chain.apply(undefined, fns); if (!watchGlobs) return watchFunc(next); // Watch glob might be true or a glob so provide a default var watchGlob = typeof watchGlobs === 'string' ? watchGlobs : '**/*'; var handleWatchChange = (0, _throttle2.default)(watchFunc, 2000); // Run a watch _chokidar2.default.watch(watchGlob, { ignored: ignored }) // TODO: performance rebuild only sub folders from the returnPath .on('all', function () { return handleWatchChange(); }); }; }; }; exports.default = { watchFiles: watchFiles };