import * as plugins from "./smartnginx.plugins"; import * as paths from "./smartnginx.paths"; import * as command from "./smartnginx.command"; import * as snippets from "./smartnginx.snippets" import {NginxZone} from "./smartnginx.classes.nginxzone"; let allConfigs:NginxConfig[] = []; export class NginxConfig { zones:NginxZone[] = []; cert:plugins.cert.Cert; // the Cert Instance from which the config gets its certificates isDeployed:boolean = false; constructor(optionsArg:plugins.cert.ICertConstructorOptions){ this.cert = new plugins.cert.Cert({ cfEmail:optionsArg.cfEmail, cfKey:optionsArg.cfKey, sslDir:paths.nginxCertBase, gitOriginRepo:optionsArg.gitOriginRepo, testMode:optionsArg.testMode }); }; // interact with Zones addZone(zoneArg:NginxZone){ this.zones.push(zoneArg); } listZones():NginxZone[] { return this.zones; }; removeZones(zoneArg:NginxZone){ } clean(){ this.zones = []; } // handle deployment of zones deploy(nginxRestartArg:boolean = false){ let done = plugins.q.defer(); plugins.smartfile.fs.removeSync(paths.nginxConfigBase); plugins.smartfile.fs.ensureDirSync(paths.nginxConfigBase); plugins.smartfile.fs.ensureDirSync(paths.nginxZoneBase); plugins.smartfile.fs.ensureDirSync(paths.nginxCertBase); for(let config of allConfigs){ config.isDeployed = false; }; this.isDeployed = true; // write base config plugins.smartfile.memory.toFsSync( snippets.getBaseConfigString(), plugins.path.join(paths.nginxConfigBase,"nginx.conf") ); // deploy zones let promiseArray = []; for(let zone of this.zones){ promiseArray.push(zone.deploy(this.cert)); }; plugins.q.all(promiseArray) .then(() => { // restart nginx if(nginxRestartArg){ command.restart(this); }; done.resolve(); }); return done.promise; }; };