import * as plugins from './smartnginx.plugins'; import * as paths from './smartnginx.paths'; import * as snippets from './smartnginx.snippets'; import { NginxHost } from './smartnginx.classes.nginxhost'; import { NginxProcess } from './smartnginx.classes.nginxprocess'; import { IHostConfig } from './interfaces/hostconfig'; /** * main class that manages a NginxInstance */ export class SmartNginx { public logger: plugins.smartlog.Smartlog; private hosts = new plugins.lik.Objectmap(); public nginxProcess: NginxProcess = new NginxProcess(this); constructor(optionsArg: { logger?: plugins.smartlog.Smartlog }) { optionsArg.logger ? (this.logger = optionsArg.logger) : (this.logger = plugins.smartlog.defaultLogger); } // =================== // interact with Hosts // =================== /** * add a host * @param nginxHostArg */ public addHost(optionsArg: IHostConfig): NginxHost { const nginxHost = new NginxHost(this, optionsArg); this.hosts.add(nginxHost); return nginxHost; } /** * Gets a NginxHost by hostname * @param hostNameArg */ public getNginxHostByHostName(hostNameArg: string): NginxHost { return this.hosts.find(nginxHost => { return nginxHost.hostName === hostNameArg; }); } /** * listHosts */ public listHosts(): NginxHost[] { return this.hosts.getArray(); } /** * remove a Host * @param nginxHostArg */ public removeHost(nginxHostArg: NginxHost) { this.hosts.remove(nginxHostArg); } /** * clean all hosts */ public wipeHosts() { this.hosts.wipe(); } /** * deploy the current stack and restart nginx */ public async deploy() { // write base config plugins.smartfile.fs.ensureDirSync(paths.nginxConfigDirPath); plugins.smartfile.memory.toFsSync(snippets.getBaseConfigString(), paths.nginxConfFile); // deploy hosts plugins.smartfile.fs.ensureEmptyDirSync(paths.nginxHostDirPath); for (const host of this.hosts.getArray()) { await host.deploy(); this.logger.log('info', `Host ${host.hostName} deployed!`); } this.nginxProcess.reloadConfig(); } }