UNPKG

@acot/cli

Version:
123 lines (122 loc) 4.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const document_1 = require("@acot/document"); const boxen_1 = __importDefault(require("boxen")); const chokidar_1 = __importDefault(require("chokidar")); const portfinder_1 = require("portfinder"); const chalk_1 = __importDefault(require("chalk")); const opener_1 = __importDefault(require("opener")); const command_1 = require("../command"); const constants_1 = require("../constants"); exports.default = (0, command_1.createCommand)({ name: 'serve', summary: 'Launch a server that delivers the documentation created for the rules provided by the preset as HTML.', args: {}, options: { project: { type: 'string', alias: 'p', default: '.', description: 'Directory path that contains the package.json that makes up the preset.', }, docs: { type: 'string', alias: 'd', default: path_1.default.join('docs', 'rules'), description: 'Directory path that contains the rule documentation.', }, port: { type: 'number', description: 'Port number for preview server.', }, watch: { type: 'boolean', alias: 'w', description: 'Watch document files.', }, 'no-open': { type: 'boolean', default: false, description: 'Does not open the browser automatically.', }, }, })(({ cwd, logger, args }) => new Promise((resolve, reject) => { (async () => { try { const port = args.port || (await (0, portfinder_1.getPortPromise)({ port: constants_1.DEFAULT_PORT })); const watcher = args.watch ? chokidar_1.default.watch(path_1.default.join(cwd, args.docs)) : null; const loader = new document_1.DocProjectLoader({ docs: args.docs }); const server = new document_1.DocServer({ port, dev: !!args.watch, }); const load = async () => { try { return await loader.load(cwd); } catch (e) { logger.error('DocLoader#load Error:', e); return null; } }; const bootstrap = async () => { const project = await load(); if (project == null) { return resolve(1); } await server.bootstrap(project); const url = `http://localhost:${port}`; logger.print(''); logger.print((0, boxen_1.default)([ (0, chalk_1.default) `Serving {green.bold ${project.name}}`, watcher == null ? (0, chalk_1.default) `Watch mode is {gray.bold disabled}` : (0, chalk_1.default) `Watch mode is {green.bold enabled}`, '', (0, chalk_1.default) `Listening on: {blue.underline ${url}}`, ].join('\n'), { borderColor: 'gray', padding: 1 })); logger.print(''); if (args['no-open'] !== true) { (0, opener_1.default)(url); } }; const handleChange = async () => { logger.print(chalk_1.default.gray('Reloading server...')); const project = await load(); if (project == null) { return resolve(1); } server.update(project); logger.print(chalk_1.default.bold.green('Restarted!')); }; process.on('SIGINT', async () => { try { await (watcher === null || watcher === void 0 ? void 0 : watcher.close()); await server.terminate(); } catch (e) { logger.error('SIGINT Error: ', e); } resolve(0); }); await bootstrap(); if (watcher != null) { watcher.on('ready', () => { watcher .on('change', handleChange) .on('add', handleChange) .on('unlink', handleChange); }); } } catch (e) { reject(e); } })(); }));