UNPKG

poku

Version:

🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.

91 lines (90 loc) 3.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startWatch = void 0; const node_process_1 = __importDefault(require("process")); const poku_js_1 = require("../configs/poku.js"); const poku_js_2 = require("../modules/essentials/poku.js"); const get_arg_js_1 = require("../parsers/get-arg.js"); const os_js_1 = require("../polyfills/os.js"); const format_js_1 = require("../services/format.js"); const map_tests_js_1 = require("../services/map-tests.js"); const poku_js_3 = require("../services/reporters/poku.js"); const watch_js_1 = require("../services/watch.js"); const write_js_1 = require("../services/write.js"); const startWatch = async (dirs) => { let isRunning = false; const { configs } = poku_js_1.GLOBAL; const watchers = new Set(); const executing = new Set(); const interval = Number((0, get_arg_js_1.getArg)('watchInterval')) || 1500; const setIsRunning = (value) => { isRunning = value; }; const resultsClear = () => { poku_js_3.errors.length = 0; }; const listenStdin = async (input) => { if (isRunning || executing.size > 0) return; if (String(input).trim() === 'rs') { for (const watcher of watchers) watcher.stop(); watchers.clear(); resultsClear(); await (0, poku_js_2.poku)(dirs); } }; node_process_1.default.stdin.removeListener('data', listenStdin); node_process_1.default.removeListener('SIGINT', poku_js_2.onSigint); resultsClear(); const mappedTests = await (0, map_tests_js_1.mapTests)('.', dirs, configs.filter, configs.exclude); for (const mappedTest of Array.from(mappedTests.keys())) { const currentWatcher = (0, watch_js_1.watch)(mappedTest, async (file, event) => { if (event === 'change') { const filePath = (0, map_tests_js_1.normalizePath)(file); if (executing.has(filePath) || isRunning || executing.size > 0) return; setIsRunning(true); executing.add(filePath); resultsClear(); const tests = mappedTests.get(filePath); if (!tests) return; await (0, poku_js_2.poku)(Array.from(tests), { ...configs, concurrency: configs.concurrency ?? Math.max(Math.floor((0, os_js_1.availableParallelism)() / 2), 1), }); setTimeout(() => { executing.delete(filePath); setIsRunning(false); }, interval); } }); currentWatcher.then((watcher) => watchers.add(watcher)); } for (const dir of dirs) { const currentWatcher = (0, watch_js_1.watch)(dir, (file, event) => { if (event === 'change') { if (executing.has(file) || isRunning || executing.size > 0) return; setIsRunning(true); executing.add(file); resultsClear(); (0, poku_js_2.poku)(file).then(() => setTimeout(() => { executing.delete(file); setIsRunning(false); }, interval)); } }); currentWatcher.then((watcher) => watchers.add(watcher)); } (0, write_js_1.hr)(); (0, write_js_1.log)(`${(0, format_js_1.format)('Watching:').bold()} ${(0, format_js_1.format)(dirs.join(', ')).underline()}`); node_process_1.default.stdin.setEncoding('utf8'); node_process_1.default.stdin.on('data', listenStdin); }; exports.startWatch = startWatch;