UNPKG

zents-cli

Version:

ZenTS is a Node.js & TypeScript MVC-Framework for building rich web applications, released as free and open-source software under the MIT License. It is designed for building web applications with modern tools and design patterns.

159 lines (158 loc) 6.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const AbstractCommand_1 = require("../classes/AbstractCommand"); const browser_sync_1 = tslib_1.__importDefault(require("browser-sync")); const chalk_1 = tslib_1.__importDefault(require("chalk")); const execa_1 = tslib_1.__importDefault(require("execa")); const command_1 = require("@oclif/command"); const portfinder_1 = require("portfinder"); const path_1 = require("path"); const nodemon_1 = tslib_1.__importDefault(require("nodemon")); const LOG_PREFIX = { SERVER: chalk_1.default.bgMagenta.white.bold('[WEB-SERVER]'), TSC: chalk_1.default.bgBlue.white.bold('[TSC-WATCH]'), SYNC: chalk_1.default.bgGreen.white.bold('[BROWSER-SYNC]'), }; class Dev extends AbstractCommand_1.AbstractCommand { async run() { this.welcome('Running ZenTS in development mode.'); const { flags } = this.parse(Dev); if (!flags.tsc && !flags.server) { this.log(`${chalk_1.default.bgMagenta.white.bold('[ZenTS-CLI]')} All features have been deactived by flags. Nothing to do here. Quitting now...`); process.exit(); } this.zenConfig = (await this.getZenConfig()); if (flags.tsc) { this.tscWatch(); } if (flags.server) { await this.startDevServer(flags.sync, { syncPortFlag: flags['sync-port'], syncUiPortFlag: flags['sync-ui-port'], }); } } tscWatch() { const tscWatchProcess = execa_1.default('tsc', ['--watch', '--preserveWatchOutput'], { preferLocal: true, localDir: process.cwd(), }); tscWatchProcess.stdout.on('data', (data) => this.log(LOG_PREFIX.TSC, this.getLogMessage(data))); } async startDevServer(activeBrowserSync, syncPortFlags) { const { src, dist } = await this.getCompilerDirectories(); const server = nodemon_1.default({ exec: 'ts-node', script: path_1.join(src, 'app.ts'), stdout: false, colours: false, ext: 'ts tsx js jsx json njk nunjucks nunjs nj html htm template tmpl tpl', ignore: [dist], env: { NODE_ENV: 'development', }, }); let browserSyncInstance; let startBrowserInstance = false; let reloadBrowserInstance = false; server .on('start', () => { this.log(`${LOG_PREFIX.SERVER} Server has started!`); if (!browserSyncInstance && activeBrowserSync) { startBrowserInstance = true; } }) .on('quit', () => this.log(`${LOG_PREFIX.SERVER} Server has quit!`)) .on('restart', (files) => { this.log(`${LOG_PREFIX.SERVER} Server restarted due to: `, files); if (browserSyncInstance && activeBrowserSync) { reloadBrowserInstance = true; } }) // eslint-disable-next-line @typescript-eslint/no-misused-promises .on('stdout', async (data) => { const msg = data.toString(); this.log(`${LOG_PREFIX.SERVER} ${msg}`); if (activeBrowserSync && msg.includes('ZenTS web-server listening on')) { if (startBrowserInstance) { startBrowserInstance = false; browserSyncInstance = await this.startBrowserSync(syncPortFlags); } else if (reloadBrowserInstance) { reloadBrowserInstance = false; browserSyncInstance.reload(); } } }) .on('stderr', (data) => this.log(`${LOG_PREFIX.SERVER} ${data.toString()}`)); } async startBrowserSync(ports) { var _a, _b, _c; const instance = browser_sync_1.default.create(); const zenAppProxy = `http://localhost:${(_c = (_b = (_a = this.zenConfig) === null || _a === void 0 ? void 0 : _a.web) === null || _b === void 0 ? void 0 : _b.port) !== null && _c !== void 0 ? _c : 3000}`; const browserSyncPort = !ports.syncPortFlag ? await portfinder_1.getPortPromise({ port: 8000, stopPort: 8999, }) : ports.syncPortFlag; const browserSyncUIPort = !ports.syncUiPortFlag ? await portfinder_1.getPortPromise({ port: 9000, stopPort: 9999, }) : ports.syncUiPortFlag; instance.init({ proxy: zenAppProxy, port: browserSyncPort, notify: false, watch: false, ui: { port: browserSyncUIPort, }, logLevel: 'silent', }); this.log(`${LOG_PREFIX.SYNC} Serving under http://localhost:${browserSyncPort} (Proxying: ${zenAppProxy})`); this.log(`${LOG_PREFIX.SYNC} Access browser-sync UI: http://localhost:${browserSyncUIPort}`); this.log(`${LOG_PREFIX.SYNC} Opening http://localhost:${browserSyncPort} in default browser`); return instance; } getLogMessage(data) { return data instanceof Buffer ? data.toString().replace(/\n/g, '') : ''; } } exports.default = Dev; Dev.description = 'run a ZenTS project with a dev server, tsc-watch and browser-sync.'; Dev.examples = [`$ zen dev`]; Dev.flags = { 'tsc': command_1.flags.boolean({ description: 'enable/Disable the TypeScript watch compiler. Enabled by default, set --no-tsc flag to disable.', default: true, allowNo: true, required: false, }), 'server': command_1.flags.boolean({ description: 'enable/Disable the web-server. Enabled by default, set --no-server flag to disable.', default: true, allowNo: true, required: false, }), 'sync': command_1.flags.boolean({ description: 'enable/Disable the browser-sync with the web-server. Enabled by default, set --no-sync flag to disable.', default: true, allowNo: true, required: false, }), 'sync-port': command_1.flags.integer({ description: 'the port browser-sync will use (if none is provided will determine a open port between 8000 and 8999 automatically)', required: false, default: 0, }), 'sync-ui-port': command_1.flags.integer({ description: 'the port browser-sync ui will use (if none is provided will determine a open port between 9000 and 9999 automatically)', required: false, default: 0, }), 'help': command_1.flags.help({ char: 'h' }), };