UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

122 lines (121 loc) 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chokidar = require("chokidar"); const path = require("path"); const env_1 = require("../env"); function attachChokidar(props) { const defaultOpts = { ignoreInitial: true, ignored: props.ignored, persistent: true, }; const userOptions = props.chokidarOptions || {}; const finalOptions = Object.assign(Object.assign({}, defaultOpts), userOptions); var watcher = chokidar.watch(props.root, finalOptions); watcher.on('all', (event, path) => { props.cb(event, path); }); } exports.attachChokidar = attachChokidar; function ignoredPath2Regex(input) { const str = input.replace(/(\/|\\)/g, '(\\/|\\\\)'); return new RegExp(str); } exports.ignoredPath2Regex = ignoredPath2Regex; var WatcherAction; (function (WatcherAction) { WatcherAction["FATAL_ERROR"] = "FATAL_ERROR"; WatcherAction["RELOAD_TS_CONFIG"] = "RELOAD_TS_CONFIG"; WatcherAction["RELOAD_ONE_FILE"] = "RELOAD_ONE_FILE"; WatcherAction["HARD_RELOAD_MODULES"] = "HARD_RELOAD_MODULES"; WatcherAction["SOFT_RELOAD_FILES"] = "SOFT_RELOAD_FILES"; WatcherAction["HARD_RELOAD_ALL"] = "HARD_RELOAD_ALL"; WatcherAction["RESTART_PROCESS"] = "RESTART_PROCESS"; })(WatcherAction = exports.WatcherAction || (exports.WatcherAction = {})); function detectAction(file, homeDir) { const isInsideHomeDir = !/\.\./.test(path.relative(homeDir, file)); if (file === env_1.env.SCRIPT_FILE) { return WatcherAction.RESTART_PROCESS; } if (path.basename(file) === 'tsconfig.json') { return WatcherAction.RELOAD_TS_CONFIG; } if (/(package.lock.json|yarn.lock)$/.test(file)) { return WatcherAction.HARD_RELOAD_MODULES; } if (!isInsideHomeDir) { return; } return WatcherAction.RELOAD_ONE_FILE; } exports.detectAction = detectAction; function getEventData(props) { const evt = props.input.event; const file = path.normalize(props.input.path); const homeDir = props.ctx.config.homeDir; const events = { add: () => detectAction(file, homeDir), change: () => detectAction(file, homeDir), unlink: () => detectAction(file, homeDir), addDir: () => detectAction(file, homeDir), unlinkDir: () => detectAction(file, homeDir), error: () => { }, }; if (events[evt]) { return events[evt](); } } exports.getEventData = getEventData; function getRelevantEvent(actions) { if (actions.includes(WatcherAction.RESTART_PROCESS)) { return WatcherAction.RESTART_PROCESS; } if (actions.includes(WatcherAction.HARD_RELOAD_MODULES)) { return WatcherAction.HARD_RELOAD_MODULES; } if (actions.includes(WatcherAction.RELOAD_TS_CONFIG)) { return WatcherAction.RELOAD_TS_CONFIG; } if (actions.includes(WatcherAction.SOFT_RELOAD_FILES)) { return WatcherAction.SOFT_RELOAD_FILES; } if (actions.includes(WatcherAction.RELOAD_ONE_FILE)) { return WatcherAction.RELOAD_ONE_FILE; } } exports.getRelevantEvent = getRelevantEvent; function createWatcher(props, externalProps) { const ctx = props.ctx; const ict = ctx.ict; externalProps = externalProps || {}; const ignored = externalProps.ignored ? externalProps.ignored : []; const paths = externalProps.paths ? externalProps.paths : props.ctx.config.homeDir; if (!externalProps.skipRecommendedIgnoredPaths) { ignored.push('/node_modules/', /(\/|\\)\./, 'dist/', 'build/', props.ctx.writer.outputDirectory); } const ignoredRegEx = ignored.map(str => (typeof str === 'string' ? ignoredPath2Regex(str) : str)); //console.log(props.ctx.config.cache.root); let events = []; let tm; ict.on('complete', data => { attachChokidar({ root: paths, chokidarOptions: externalProps.chokidar, ignored: ignoredRegEx, cb: (event, path) => { clearTimeout(tm); const action = getEventData({ input: { event, path }, ctx }); if (action) { events.push(action); } tm = setTimeout(() => { const action = getRelevantEvent(events); events = []; props.onEvent(action, path); }, 50); }, }); return data; }); } exports.createWatcher = createWatcher;