From f8553d78cb527d97118c0cf1c2dc18fb5888d839 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Wed, 8 Nov 2017 00:50:19 +0530 Subject: [PATCH] Working on adding and removing list items --- actions/createProxyServer.js | 6 ++++-- actions/createStaticServer.js | 5 +++-- actions/killServer.js | 2 +- index.js | 24 +++++++++++++++--------- package-lock.json | 19 +++++++++++++++++++ package.json | 1 + utils/listFile.js | 33 ++++++++++++++++++++++++++++++--- utils/nginxPath.js | 12 ++++++------ 8 files changed, 79 insertions(+), 23 deletions(-) diff --git a/actions/createProxyServer.js b/actions/createProxyServer.js index 666cd29..d38f984 100644 --- a/actions/createProxyServer.js +++ b/actions/createProxyServer.js @@ -4,11 +4,12 @@ var shell = require('shelljs'); var npath = require('../utils/nginxPath'); var conf = require('../utils/nginxConf'); var nginxReload = require('../utils/nginxReload'); +var appendToList = require('../utils/listFile').appendToList; var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. function createProxyServer(domain, inPort, outPort) { - fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), + fs.outputFileSync((conf(npath.confD(), domain, outPort)), "server {" + EOL + " listen " + outPort + ";" + EOL + " listen [::]:" + outPort + ";" + EOL + @@ -26,8 +27,9 @@ function createProxyServer(domain, inPort, outPort) { "}" ); shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist - shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled + shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled + appendToList(domain, outPort, inPort); nginxReload(); } diff --git a/actions/createStaticServer.js b/actions/createStaticServer.js index 8c09997..5ad99b4 100644 --- a/actions/createStaticServer.js +++ b/actions/createStaticServer.js @@ -11,7 +11,7 @@ var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. function createStaticServer(domain, outPort) { outPort = outPort || 80; - fs.outputFileSync((conf(npath.availableSites(), domain, outPort)), // Gets nginx's paths from nginxPath.js + fs.outputFileSync((conf(npath.confD(), domain, outPort)), // Gets nginx's paths from nginxPath.js "server {" + EOL + " listen " + outPort + ";" + EOL + " listen [::]:" + outPort + ";" + EOL + @@ -26,11 +26,12 @@ function createStaticServer(domain, outPort) { ); shell.mkdir('-p', npath.enabledSites()); // Creates directory if doesn't exist shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); // Removes domain from sites-enabled if exists - shell.ln('-sf', conf(npath.availableSites(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled + shell.ln('-sf', conf(npath.confD(), domain, outPort), conf(npath.enabledSites(), domain, outPort)); // Symlink the conf file from sites-available to sites-enabled shell.rm('-rf', npath.webRootDomain(domain, outPort)); // Removes domain from webroot if exists shell.mkdir('-p', npath.webRoot()); // Creating the nginx www path if it doesn't exist so symlink doesn't fail shell.ln('-sf', currentPath, npath.webRootDomain(domain, outPort)); // Symlink current directory to nginx's web root + appendToList(domain, outPort); nginxReload(); } diff --git a/actions/killServer.js b/actions/killServer.js index ed0d25d..065e9da 100644 --- a/actions/killServer.js +++ b/actions/killServer.js @@ -6,7 +6,7 @@ var nginxReload = require('../utils/nginxReload'); function killServer(domain, outPort) { shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); - shell.rm('-rf', conf(npath.availableSites(), domain, outPort)); + shell.rm('-rf', conf(npath.confD(), domain, outPort)); shell.rm('-rf', npath.webRootDomain(domain, outPort)); nginxReload(); diff --git a/index.js b/index.js index efb8cfd..2b1bea1 100755 --- a/index.js +++ b/index.js @@ -3,17 +3,22 @@ // Requiring npm modules var program = require('commander'); var chalk = require('chalk'); - -// Requiring utils -var validate = require('./utils/validate'); -var requirements = require('./utils/requirements'); -var appendToList = require('./utils/listFile'); +var fs = require('fs-extra'); +var prettyjson = require('prettyjson'); // Requiring Actions var createProxyServer = require('./actions/createProxyServer'); var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); +// Requiring utils +var validate = require('./utils/validate'); +var requirements = require('./utils/requirements'); +var appendToList = require('./utils/listFile').appendToList; +var readServers = require('./utils/listFile').readServers; + +var EOL = require('os').EOL; + // Check for requirements such as OS version and nginx install. Throw and exit if requirements not found. #Roadmap: Add ability to satisfy any possible requirements. requirements(); // Comment in development and uncomment this line in production. This should check whether the OS is compatible with this version of `up` @@ -28,7 +33,7 @@ program if (!validate(domain, outPort)) return; //Validates domain and outport, and if invalid, throws and returns. createStaticServer(domain, outPort); if (outPort != "80" || "443") domain = domain + ":" + outPort; - console.log("Done! Your static server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!"); + console.log("\nDone! Your static server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!"); }); program @@ -39,14 +44,15 @@ program if (!validate(domain, inPort, outPort)) return; createProxyServer(domain, inPort, outPort); if (outPort != "80" || "443") domain = domain + ":" + outPort; - console.log("Done! Your reverse proxy server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!"); + console.log("\nDone! Your reverse proxy server has been set up!\nPoint your domain to this server and check " + chalk.cyan(domain) + " to verify!"); }); program .command('list') .description('List all available servers.') .action(function () { - // Stuff happens here + var serversList = readServers(); + console.log(EOL + prettyjson.render(serversList) + EOL); }); program @@ -61,7 +67,7 @@ program program .command('*') // This should pick invalid commands, but it doesn't, yet. .action(function () { - console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help."); + console.log("\nInvalid command. Type " + chalk.cyan('up --help') + " for help.\n"); }); // Adds custom help text to the automatically generated help. diff --git a/package-lock.json b/package-lock.json index 27e3a0c..e584f36 100644 --- a/package-lock.json +++ b/package-lock.json @@ -49,6 +49,11 @@ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, + "colors": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.1.2.tgz", + "integrity": "sha1-FopHAXVran9RoSzgyXv6KMCE7WM=" + }, "commander": { "version": "2.11.0", "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", @@ -142,6 +147,11 @@ "brace-expansion": "1.1.8" } }, + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -160,6 +170,15 @@ "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz", "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=" }, + "prettyjson": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/prettyjson/-/prettyjson-1.2.1.tgz", + "integrity": "sha1-/P+rQdGcq0365eV15kJGYZsS0ok=", + "requires": { + "colors": "1.1.2", + "minimist": "1.2.0" + } + }, "rechoir": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.6.2.tgz", diff --git a/package.json b/package.json index e4f5998..bfed2d4 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "commander": "^2.11.0", "fs-extra": "^4.0.2", "json-beautify": "^1.0.1", + "prettyjson": "^1.2.1", "shelljs": "^0.7.8", "validator": "^9.1.1" } diff --git a/utils/listFile.js b/utils/listFile.js index cbd6770..ae59f0b 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -7,7 +7,8 @@ var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. var listFilePath = "/etc/up-serve/servers.up"; function appendToList(domain, outPort, inPort) { - + + inPort = inPort || undefined; var jsonFile = {}; var domBlock; @@ -43,8 +44,34 @@ function appendToList(domain, outPort, inPort) { jsonFile[domain] = domBlock; jsonFile = beautifyJSON(jsonFile); } - fs.writeFileSync(listFilePath, jsonFile); + fs.writeFileSync(listFilePath, jsonFile); } -module.exports = appendToList; +function readServers () { + return JSON.parse(fs.readFileSync(listFilePath)); +} + +function removeFromList (domain, outPort) { + var jsonFile = {}; + if (fs.existsSync(listFilePath)) { + jsonFile = fs.readFileSync(listFilePath); + jsonFile = JSON.parse(jsonFile); + + for (block in jsonFile) { + if (block.domain == domain && block.domain.outPort == outPort) { + delete jsonFile.block; + return; + } + } + + jsonFile = beautifyJSON(jsonFile, null, 2, 30); + } + else { + console.log("\Domain was not in my list. Are you sure?\n") + } + fs.writeFileSync(listFilePath, jsonFile); +} + +module.exports.appendToList = appendToList; +module.exports.readServers = readServers; \ No newline at end of file diff --git a/utils/nginxPath.js b/utils/nginxPath.js index 91dbe8c..97b32af 100644 --- a/utils/nginxPath.js +++ b/utils/nginxPath.js @@ -1,17 +1,17 @@ // These functions just return paths. Later, these should be modified to poll from nginx's config. -var available = "/etc/nginx/sites-available/"; var enabled = "/etc/nginx/sites-enabled/"; +var conf = "/etc/nginx/conf.d/"; var wwwRoot = "/var/www/"; -function availableSites() { - return available; -} - function enabledSites() { return enabled; } +function confD() { + return conf; +} + function webRoot() { return wwwRoot; } @@ -21,7 +21,7 @@ function webRootDomain(domain, outPort) { return rootWithDomain; } -module.exports.availableSites = availableSites; +module.exports.confD = confD; module.exports.enabledSites = enabledSites; module.exports.webRoot = webRoot; module.exports.webRootDomain = webRootDomain; \ No newline at end of file