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.
 

69 lines
1.9 KiB

'use strict';
const fs = require('fs-extra');
const removeFromArray = require('./removeFromArray');
const listFilePath = require('./nginxPath').serversUp;
function appendToList(domain, outPort, inPort) {
inPort = inPort || undefined;
let jsonFile = { "domains": [] };
const domainBlock = {
"domain": domain,
"outPort": outPort
};
if (!inPort) {
domainBlock.type = "static/server";
} else {
domainBlock.type = "proxy server";
domainBlock.inPort = inPort;
}
if (fs.existsSync(listFilePath())) {
const jsonBuffer = JSON.parse(fs.readFileSync(listFilePath()));
jsonFile.domains = removeFromArray(jsonBuffer.domains, domain, outPort);
}
jsonFile.domains.push(domainBlock);
jsonFile = JSON.stringify(jsonFile, null, '\t');
fs.writeFileSync(listFilePath(), jsonFile);
}
function removeFromList (domain, outPort) {
if (fs.existsSync(listFilePath())) {
let jsonFile = { "domains": [] };
const jsonContent = fs.readFileSync(listFilePath(), "utf-8");
const jsonBuffer = JSON.parse(jsonContent);
for (let i = 0; i < (jsonBuffer.domains).length; i ++) {
if(jsonBuffer.domains[i].domain == domain){
jsonFile.domains =
removeFromArray(jsonBuffer.domains, domain, outPort);
jsonFile = JSON.stringify(jsonFile, null, '\t');
fs.writeFileSync(listFilePath(), jsonFile);
return;
}
}
throw new Error("This domain does not exist in servers.up");
}
else throw new Error("No servers were created using `up` yet.");
}
function readServers () {
let serversList;
if (fs.existsSync(listFilePath())) {
serversList = JSON.parse(fs.readFileSync(listFilePath()));
if(!serversList.domains[0]) {
throw new Error("No domains exist in servers.up");
}
}
else {
throw new Error("No servers were created using `up` yet.");
}
return serversList;
}
module.exports.appendToList = appendToList;
module.exports.readServers = readServers;
module.exports.removeFromList = removeFromList;