UNPKG

@router-cli/react-router-dev

Version:

File based routing cli for react-router-dom.

55 lines (54 loc) 2.5 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as chokidar from 'chokidar'; import { Generator } from '../generator'; import { verboseLog } from '../utils/logging'; export function watch(config, verbose) { return __awaiter(this, void 0, void 0, function* () { let watcher = new chokidar.FSWatcher(); const generator = new Generator(config, verbose); const generatorWatcher = () => __awaiter(this, void 0, void 0, function* () { watcher.close(); console.log(`router-cli: Watching routes (${config.source})...`); const log = (type, path) => { if (verbose) verboseLog(`"${path}" ${type}. regenerating...`); }; watcher = chokidar.watch(config.source); watcher.on('ready', () => __awaiter(this, void 0, void 0, function* () { const handle = () => __awaiter(this, void 0, void 0, function* () { try { yield generator.generate(); } catch (err) { console.error(err); } }); const checkPath = (path) => (path.endsWith('.page.tsx') || path.endsWith('_layout.tsx')); handle(); watcher.on('add', (path) => { if (checkPath(path)) { log("created", path); handle(); } }); watcher.on('unlink', (path) => { log("removed", path); handle(); }); watcher.on('unlinkDir', (path) => { log("removed", path); handle(); }); })); }); generatorWatcher(); }); }