ng-spec-shot-reviewer
Version:
combined server of spec shot reviewer backend and optionally multiple static websites (e.g. compiled angular apps)
100 lines • 4.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const express = require("express");
const fs = require("fs");
const path_1 = require("path");
const app_config_model_1 = require("./app-config.model");
const endpoint_1 = require("./endpoint");
const server_config_model_1 = require("./server-config.model");
const utility_functions_1 = require("./utility.functions");
const app = express();
const cfg = new app_config_model_1.SsrAppConfig(process.cwd());
console.log(chalk_1.default.green('\nstarting spec shot reviewer ...'));
function findProtractorCfg(dir) {
const cfgInProjectRooPath = path_1.join(dir, 'protractor.conf.js');
if (utility_functions_1.canReadFsNode(cfgInProjectRooPath)) {
return cfgInProjectRooPath;
}
const cfgInE2eDir = path_1.join(dir, 'e2e', 'protractor.conf.js');
if (utility_functions_1.canReadFsNode(cfgInE2eDir)) {
return cfgInE2eDir;
}
return false;
}
const protractorCfgPath = findProtractorCfg(process.cwd());
try {
if (!protractorCfgPath) {
throw new Error('protractor conf file not found');
}
const config = require(protractorCfgPath).config;
const imgCfg = config.plugins.find((plugin) => plugin.package === 'protractor-image-comparison');
if (imgCfg.options.screenshotPath) {
cfg.endpoint.directories = new server_config_model_1.SsrImageDirectoriesConfig(imgCfg.options.screenshotPath);
cfg.endpoint.approvedFilePath = path_1.join(imgCfg.options.screenshotPath, 'approvements.json');
}
if (imgCfg.options.baselineFolder) {
cfg.endpoint.directories.baseline = imgCfg.options.baselineFolder;
}
}
catch (e) {
console.warn('no protractor conf found');
}
function deployAngularApp(distFolder) {
const indexFile = fs.readFileSync(path_1.join(distFolder, 'index.html'), 'utf-8');
const baseHref = '/' + (indexFile.match(/<base.*href=['"]?\/?([^'"]+)\/?['"]?/) || [null, 'app'])[1];
const alreadyUsed = cfg.reservedPathes.some((path) => baseHref.startsWith(path));
if (alreadyUsed) {
throw new Error(`invalid config: can't use '${baseHref}' as angular app context. '${alreadyUsed}' already used!`);
}
const targetFolder = path_1.resolve(cfg.angularAppsFolder, distFolder);
console.log(chalk_1.default.yellow(baseHref), 'serve static', ':', targetFolder);
app.use(baseHref, express.static(targetFolder), (_, res) => {
res.sendFile(targetFolder + '/index.html');
});
}
function deployOwnFrontends() {
const debugMode = process.env.DEBUG_MODE;
const ownFrontends = path_1.join(__dirname, debugMode ? 'dist' : '..', 'frontend');
console.log('\nown frontends in ', ownFrontends);
const angularApps = findAngularApps(ownFrontends);
if (angularApps.length > 0) {
angularApps.forEach(deployAngularApp);
}
else {
console.error('no other angular apps found');
}
}
deployOwnFrontends();
function findAngularApps(dir) {
if (!fs.statSync(dir).isDirectory()) {
return [];
}
if (utility_functions_1.canReadFsNode(path_1.join(dir, 'index.html'))) {
return [dir];
}
else {
return fs.readdirSync(dir)
.map((subdir) => findAngularApps(path_1.join(dir, subdir)))
.reduce((allApps, moreApps) => allApps.concat(moreApps), []);
}
}
if (cfg.angularAppsFolder && utility_functions_1.canReadFsNode(cfg.angularAppsFolder)) {
console.log('\nload local angular apps from', cfg.angularAppsFolder);
const angularApps = findAngularApps(cfg.angularAppsFolder);
if (angularApps.length > 0) {
angularApps.forEach(deployAngularApp);
}
else {
console.error('no other angular apps found');
}
}
console.log('\ncore services');
if (cfg.testPageUrl) {
console.log(chalk_1.default.yellow(cfg.testPageUrl), 'server is alive test page');
app.get(cfg.testPageUrl, (_req, res) => res.send('Ok'));
}
console.log(chalk_1.default.yellow(cfg.backendUrl), 'ssr endpoint');
app.use(cfg.backendUrl, new endpoint_1.SsrEndpoint(cfg.endpoint).middleware);
app.listen(cfg.port, cfg.hostname, () => console.log(chalk_1.default.green('\nspec shot reviewer listening on ' + cfg.port + '\n')));
//# sourceMappingURL=app.js.map