UNPKG

@underpostnet/underpost

Version:

Underpost Platform — end-to-end CI/CD and application-delivery toolchain CLI. Covers bare metal, Kubernetes, K3s, kubeadm, LXD, container/image orchestration, secrets, databases, cron jobs, monitoring, SSH, runners, PWA + Workbox delivery, and release orc

89 lines (78 loc) 3.25 kB
/** * Module for creating a client-side development server * @module src/client-builder/client-dev-server.js * @namespace clientDevServer */ import fs from 'fs-extra'; import nodemon from 'nodemon'; import dotenv from 'dotenv'; import { shellExec } from '../server/process.js'; import { loggerFactory } from '../server/logger.js'; import Underpost from '../index.js'; const logger = loggerFactory(import.meta); /** * @function createClientDevServer * @description Creates a client-side development server. * @memberof clientDevServer * @param {string} deployId - The deployment ID. * @param {string} subConf - The sub-configuration. * @param {string} host - The host. * @param {string} path - The path. * @returns {void} * @memberof clientDevServer */ const createClientDevServer = async ( deployId = process.argv[2] || 'dd-default', subConf = process.argv[3] || '', host = process.argv[4] || 'default.net', path = process.argv[5] || '/', ) => { const devClientEnvPath = `./engine-private/conf/${deployId}/.env.${process.env.NODE_ENV}.${subConf}-dev-client`; if (fs.existsSync(devClientEnvPath)) dotenv.config({ path: devClientEnvPath, override: true }); await Underpost.repo.client(deployId, `${subConf}-dev-client`.trim(), host, path); shellExec(`node src/server ${deployId} ${subConf}-dev-client`.trim(), { async: true, }); // https://github.com/remy/nodemon/blob/main/doc/events.md // States // start - child process has started // crash - child process has crashed (nodemon will not emit exit) // exit - child process has cleanly exited (ie. no crash) // restart([ array of files triggering the restart ]) - child process has restarted // config:update - nodemon's config has changed if (fs.existsSync(`/tmp/client.build.json`)) fs.removeSync(`/tmp/client.build.json`); let buildPathScope = []; const nodemonOptions = { script: './src/client.build', args: [`${deployId}`, `${subConf}-dev-client`, `${host}`, `${path}`], watch: 'src/client', }; logger.info('nodemon option', { nodemonOptions }); nodemon(nodemonOptions) .on('start', function (...args) { logger.info(args, 'nodemon started'); }) .on('restart', function (...args) { logger.info(args, 'nodemon restart'); const eventPath = args[0][0]; const indexPath = buildPathScope.findIndex((buildObjScope) => buildObjScope.path === eventPath); const buildObj = { timestamp: new Date().getTime(), path: eventPath, }; if (indexPath > -1) { buildPathScope[indexPath].timestamp = buildObj.timestamp; } else buildPathScope.push(buildObj); setTimeout(() => { buildPathScope = buildPathScope.filter((buildObjScope) => buildObjScope.timestamp !== buildObj.timestamp); }, 2500); const buildPathScopeBuild = buildPathScope.map((o) => o.path); logger.info('buildPathScopeBuild', buildPathScopeBuild); fs.writeFileSync(`/tmp/client.build.json`, JSON.stringify(buildPathScopeBuild, null, 4)); }) .on('crash', function (error) { if (error) logger.error(error, error.message || 'nodemon crash'); else logger.error('nodemon process crashed'); }); }; export { createClientDevServer };