@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
101 lines (92 loc) • 3.76 kB
JavaScript
/**
* Module for live building client-side code
* @module src/client-builder/client-build-live.js
* @namespace clientLiveBuild
*/
import fs from 'fs-extra';
import { Config, loadConf, readConfJson } from '../server/conf.js';
import { loggerFactory } from '../server/logger.js';
import { buildClient } from './client-build.js';
const logger = loggerFactory(import.meta);
/**
* @function clientLiveBuild
* @description Initiates a live build of client-side code.
* @memberof clientLiveBuild
*/
const clientLiveBuild = async () => {
if (fs.existsSync(`/tmp/client.build.json`)) {
const deployId = process.argv[2];
const subConf = process.argv[3];
let clientId = 'default';
let host = 'default.net';
let path = '/';
let baseHost = `${host}${path === '/' ? '' : path}`;
let views;
let apiBaseHost;
let apiBaseProxyPath;
if (
deployId &&
(fs.existsSync(`./engine-private/conf/${deployId}`) || fs.existsSync(`./engine-private/replica/${deployId}`))
) {
loadConf(deployId, subConf);
const confClient = readConfJson(deployId, 'client');
const confServer = readConfJson(deployId, 'server');
host = process.argv[4];
path = process.argv[5];
clientId = confServer[host][path].client;
views = confClient[clientId].views;
baseHost = `${host}${path === '/' ? '' : path}`;
apiBaseHost = confServer[host][path].apiBaseHost;
apiBaseProxyPath = confServer[host][path].apiBaseProxyPath;
} else {
views = Config.default.client[clientId].views;
}
logger.info('Live build config', {
deployId,
subConf,
host,
path,
clientId,
baseHost,
views: views.length,
apiBaseHost,
apiBaseProxyPath,
});
const updates = JSON.parse(fs.readFileSync(`/tmp/client.build.json`, 'utf8'));
const liveClientBuildPaths = [];
for (let srcPath of updates) {
srcPath = srcPath.replaceAll('/', `\\`);
const srcBuildPath = `./src${srcPath.split('src')[1].replace(/\\/g, '/')}`;
if (
srcPath.split('src')[1].startsWith(`\\client\\components`) ||
srcPath.split('src')[1].startsWith(`\\client\\services`)
) {
const publicBuildPath = `./public/${baseHost}/${srcPath.split('src')[1].slice(8)}`.replace(/\\/g, '/');
liveClientBuildPaths.push({ srcBuildPath, publicBuildPath });
} else if (srcPath.split('src')[1].startsWith(`\\client\\sw`)) {
const publicBuildPath = `./public/${baseHost}/sw.js`;
liveClientBuildPaths.push({ srcBuildPath, publicBuildPath });
} else if (
srcPath.split('src')[1].startsWith(`\\client\\offline`) &&
srcPath.split('src')[1].startsWith(`index.js`)
) {
const publicBuildPath = `./public/${baseHost}/offline.js`;
liveClientBuildPaths.push({ srcBuildPath, publicBuildPath });
} else if (srcPath.split('src')[1].startsWith(`\\client`) && srcPath.slice(-9) === '.index.js') {
for (const view of views) {
const publicBuildPath = `./public/${baseHost}${view.path === '/' ? '' : view.path}/${clientId}.index.js`;
liveClientBuildPaths.push({ srcBuildPath, publicBuildPath });
}
} else if (srcPath.split('src')[1].startsWith(`\\client\\ssr`)) {
for (const view of views) {
const publicBuildPath = `./public/${baseHost}${view.path === '/' ? '' : view.path}/index.html`;
liveClientBuildPaths.push({ srcBuildPath, publicBuildPath });
}
}
}
logger.info('liveClientBuildPaths', liveClientBuildPaths);
await buildClient({ liveClientBuildPaths, instances: [{ host, path }] });
fs.removeSync(`/tmp/client.build.json`);
}
};
export { clientLiveBuild };