A cli tool to quickly create and manage nginx server blocks. https://up.js.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

var fs = require('fs-extra');
var removeFromArray = require('./removeFromArray');
var listFilePath = require('./nginxPath').serversUp;
function appendToList(domain, outPort, inPort) {
inPort = inPort || undefined;
var jsonFile = { "domains": [] };
var domBlock = {
"domain": domain,
"outPort": outPort
};
if (!inPort) {
domBlock.type = "static";
} else {
domBlock.type = "proxy";
domBlock.inPort = inPort;
}
if (fs.existsSync(listFilePath())) {
var jsonBuffer = JSON.parse(fs.readFileSync(listFilePath()));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
}
jsonFile.domains.push(domBlock);
jsonFile = JSON.stringify(jsonFile, null, '\t');
fs.writeFileSync(listFilePath(), jsonFile);
}
function removeFromList (domain, outPort) {
if (fs.existsSync(listFilePath())) {
var jsonFile = { "domains": [] };
var jsonBuffer = JSON.parse(fs.readFileSync(listFilePath()));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
jsonFile = JSON.stringify(jsonBuffer, null, '\t');
fs.writeFileSync(listFilePath(), jsonFile);
}
else console.log("\nNo servers were created using `up` yet.\n");
}
function readServers () {
var serversList = JSON.parse(fs.readFileSync(listFilePath()));
if(!serversList.domains[0]) return undefined;
return serversList;
}
module.exports.appendToList = appendToList;
module.exports.readServers = readServers;
module.exports.removeFromList = removeFromList;