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.
61 lines
1.6 KiB
61 lines
1.6 KiB
'use strict';
|
|
|
|
const { EOL } = require('os');
|
|
|
|
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 domBlock = {
|
|
"domain": domain,
|
|
"outPort": outPort
|
|
};
|
|
|
|
if (!inPort) {
|
|
domBlock.type = "static";
|
|
} else {
|
|
domBlock.type = "proxy";
|
|
domBlock.inPort = inPort;
|
|
}
|
|
|
|
if (fs.existsSync(listFilePath())) {
|
|
const 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())) {
|
|
let jsonFile = { "domains": [] };
|
|
const 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(EOL + "No servers were created using `up` yet." + EOL);
|
|
}
|
|
|
|
function readServers () {
|
|
let serversList;
|
|
if (fs.existsSync(listFilePath())) {
|
|
serversList = JSON.parse(fs.readFileSync(listFilePath()));
|
|
if(!serversList.domains[0]) return undefined;
|
|
}
|
|
else {
|
|
return "Servers were not created";
|
|
}
|
|
return serversList;
|
|
}
|
|
|
|
module.exports.appendToList = appendToList;
|
|
module.exports.readServers = readServers;
|
|
module.exports.removeFromList = removeFromList;
|
|
|