UNPKG

barrelbot

Version:

a small utility to maintain barrel files

145 lines (144 loc) 5.92 kB
#! /usr/bin/env node "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const chokidar_1 = __importDefault(require("chokidar")); const yargs_1 = __importDefault(require("yargs")); const checkDir_1 = require("./checks/checkDir"); const writeIndex_1 = require("./writes/writeIndex"); const checkIndexFile_1 = require("./checks/checkIndexFile"); const builder = (yargs) => { // console.log('builder', yargs); return yargs.positional('folder', { describe: 'folder', default: 'src' }); }; const handler = () => { // console.log('handler', argv); // if (argv.verbose) console.info(`start server on :${argv.port}`); // serve(argv.port); }; function run() { return __awaiter(this, void 0, void 0, function* () { const temp = yargs_1.default // list of stuff .command('watch [folder]', 'have a watcher', builder, handler) .option('verbose', { alias: 'v', default: false }).argv; // console.log(temp); // { _: [ 'watch' ], // verbose: false, // v: false, // folder: 'testfolder', // '$0': '/usr/local/bin/barrelbot' } const srcfolder = temp.folder; // const src = (s: string) => path.join(srcfolder, s); if (!fs_1.default.existsSync(srcfolder)) { return console.error(`path '${srcfolder}' does not exist, terminating...`); } const EXTENSION = 'ts'; // TODO: PARAMETERIZE // Something to use when events are received. const log = console.log.bind(console); let isLoading = true; let watchedDirs = []; chokidar_1.default .watch(srcfolder, { ignored: /(^|[\/\\])\../, persistent: true }) .on('add', _path => { if (isLoading) { // should not do anything } else { log(`File ${_path} has been added`); if (checkIndexFile_1.isIndexFile(_path)) { // noop } else { // rerun writeIndex const dirname = path_1.default.dirname(_path); writeIndex_1.writeIndex(dirname, EXTENSION); } } }) .on('unlink', _path => { if (isLoading) { throw new Error(`unexpected unlink event while loading, please investigate ${_path}`); } else { log(`File ${_path} has been removed`); if (checkIndexFile_1.isIndexFile(_path)) { // noop } else { // rerun writeIndex const dirname = path_1.default.dirname(_path); const { indexFilePath, allFilesExceptIndex, hasIndexFile } = checkDir_1.checkDir(dirname, EXTENSION); if (allFilesExceptIndex.length) { writeIndex_1.writeIndex(dirname, EXTENSION); } else { // there is no longer any linkable file, lets delete index file as well if exists if (hasIndexFile) { console.log('TODO: DELETE INDEX FILE: ' + indexFilePath); } } } } }) .on('addDir', _path => { if (isLoading) { log(`Adding dir ${_path}...`); watchedDirs.push(_path); // const { indexFile_path, allFilesExceptIndex } = checkDir_1.checkDir(_path, EXTENSION); // console.log({ indexFile_path, allFilesExceptIndex }); } else { watchedDirs.push(_path); log(`Directory ${_path} has been added`); } }) .on('unlinkDir', _path => { if (isLoading) { throw new Error(`unexpected unlinkDir event while loading, please investigate ${_path}`); } else { watchedDirs = watchedDirs.filter(x => x !== _path); log(`Directory ${_path} has been removed`); } }) .on('error', error => log(`Watcher error: ${error}`)) .on('ready', () => __awaiter(this, void 0, void 0, function* () { log(`Scanned and validated ${srcfolder}. Checking and writing barrels...`); isLoading = false; yield Promise.all(watchedDirs.map(dir => writeIndex_1.writeIndex(dir, EXTENSION))); log(`Watching for changes...`); })); // // unused // .on('all', (event, _path) => { // console.log(event, _path); // })) // .on('change', _path => log(`File ${_path} has been changed`)) // .on('raw', (event, _path, details) => { // log('Raw event info:', event, _path, details); // }); }); } module.exports = { run };