From cc474f3a2ec1b10e65dd7a4dcc6b9492f2d0b9f8 Mon Sep 17 00:00:00 2001 From: shunmusanjay Date: Tue, 7 Nov 2017 01:59:37 +0530 Subject: [PATCH 01/15] up-list feature branch --- utils/listFile.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 utils/listFile.js diff --git a/utils/listFile.js b/utils/listFile.js new file mode 100644 index 0000000..b0ab224 --- /dev/null +++ b/utils/listFile.js @@ -0,0 +1,13 @@ +// These functions just return paths. + +var list = 'etc/up-serve/servers.up'; + +function list(domain, inPort, outPort) { + if (inPort) { + return (list + ' ' + domain + ':' + outPort + ' ' + 'proxy' + ' ' + inPort); + } else { + return (list + ' ' + domain + ':' + outPort + ' ' + 'static'); + } +} + +module.exports = listFile; \ No newline at end of file From da770f9c239bcf7b85f52d6128683bb97c7c5a17 Mon Sep 17 00:00:00 2001 From: shunmusanjay Date: Tue, 7 Nov 2017 02:31:04 +0530 Subject: [PATCH 02/15] listFile.js file creation and append --- utils/listFile.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/utils/listFile.js b/utils/listFile.js index b0ab224..477acfa 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -1,13 +1,22 @@ // These functions just return paths. -var list = 'etc/up-serve/servers.up'; +var fs = require('fs-extra'); +var shell = require('shelljs'); +var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. + +var listFilePath = 'etc/up-serve/servers.up'; function list(domain, inPort, outPort) { + fs.ensureFileSync(listFilePath) // Creates directory if doesn't exist + if (inPort) { - return (list + ' ' + domain + ':' + outPort + ' ' + 'proxy' + ' ' + inPort); + var addr = domain + ':' + outPort + ' ' + 'proxy' + ' ' + inPort + EOL; } else { - return (list + ' ' + domain + ':' + outPort + ' ' + 'static'); + var addr = domain + ':' + outPort + ' ' + 'static' + EOL; } + + //wirtes to the file in path + fs.appendFileSync(listFilePath, addr); } module.exports = listFile; \ No newline at end of file From 9ba538798b8b7f95293ac4ec6ac05c23cb63361a Mon Sep 17 00:00:00 2001 From: shunmusanjay Date: Tue, 7 Nov 2017 02:49:21 +0530 Subject: [PATCH 03/15] appendToList Test --- index.js | 116 ++++++++++++++++++++++++++++-------------------------- utils/listFile.js | 6 +-- 2 files changed, 64 insertions(+), 58 deletions(-) diff --git a/index.js b/index.js index f95b0f2..fb1f0ce 100644 --- a/index.js +++ b/index.js @@ -7,74 +7,80 @@ var chalk = require('chalk'); // Requiring utils var validate = require('./utils/validate'); var requirements = require('./utils/requirements'); +var appendToList = require('./utils/listFile'); // Requiring Actions var createProxyServer = require('./actions/createProxyServer'); var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); + +appendToList("example.com", "80"); +appendToList("example2.com", "80", "4000"); + + // 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` -program - .version('0.1.5'); +// program +// .version('0.1.5'); -program - .command('static [outPort]') - .description('Create a static server at this folder.') - .action(function (domain, outPort) { //If outport is not given, 80 is set as default. Later, change this default to reflect nginx's settings. - outPort = outPort || "80"; // This is a string because regex needs to validate it. - 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!"); - }); +// program +// .command('static [outPort]') +// .description('Create a static server at this folder.') +// .action(function (domain, outPort) { //If outport is not given, 80 is set as default. Later, change this default to reflect nginx's settings. +// outPort = outPort || "80"; // This is a string because regex needs to validate it. +// 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!"); +// }); -program - .command('proxy [outPort]') - .description('Create a proxy server, listening at port number.') - .action(function (domain, inPort, outPort) { //Inbound port is necessary, but outbound is set to 80 by default. Again, will change this to reflect nginx's settings. - outPort = outPort || "80"; // This is a string because regex needs to validate it. - 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!"); - }); +// program +// .command('proxy [outPort]') +// .description('Create a proxy server, listening at port number.') +// .action(function (domain, inPort, outPort) { //Inbound port is necessary, but outbound is set to 80 by default. Again, will change this to reflect nginx's settings. +// outPort = outPort || "80"; // This is a string because regex needs to validate it. +// 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!"); +// }); -program - .command('list') - .description('List all available servers.') - .action(function () { - // Stuff happens here - }); +// program +// .command('list') +// .description('List all available servers.') +// .action(function () { +// // Stuff happens here +// }); -program - .command('kill [ourPort]') - .description('Kill a server.') - .action(function (domain, outPort) { - outPort = outPort || "80"; // This is a string because regex needs to validate it. - killServer(domain, outPort); - console.log("\nDone! Your server has been killed!\n"); - }); +// program +// .command('kill [ourPort]') +// .description('Kill a server.') +// .action(function (domain, outPort) { +// outPort = outPort || "80"; // This is a string because regex needs to validate it. +// killServer(domain, outPort); +// console.log("\nDone! Your server has been killed!\n"); +// }); -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."); - }); +// 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."); +// }); -// Adds custom help text to the automatically generated help. -program.on('--help', function () { - console.log(''); - console.log(' Usage:'); - console.log(''); - console.log(' ', chalk.yellow('$ up'), chalk.cyan('static'), chalk.blue('domain-name')); - console.log(' Set up a static server at domain-name'); - console.log(''); - console.log(' ', chalk.yellow('$ up'), chalk.cyan('proxy'), chalk.blue('domain-name port-number')); - console.log(' Set up a proxy server listening at port-number'); - console.log(''); -}); +// // Adds custom help text to the automatically generated help. +// program.on('--help', function () { +// console.log(''); +// console.log(' Usage:'); +// console.log(''); +// console.log(' ', chalk.yellow('$ up'), chalk.cyan('static'), chalk.blue('domain-name')); +// console.log(' Set up a static server at domain-name'); +// console.log(''); +// console.log(' ', chalk.yellow('$ up'), chalk.cyan('proxy'), chalk.blue('domain-name port-number')); +// console.log(' Set up a proxy server listening at port-number'); +// console.log(''); +// }); -// Parses commands passed to `up` and chooses one of the above commands. -program.parse(process.argv); \ No newline at end of file +// // Parses commands passed to `up` and chooses one of the above commands. +// program.parse(process.argv); \ No newline at end of file diff --git a/utils/listFile.js b/utils/listFile.js index 477acfa..56ddfc9 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -1,12 +1,12 @@ // These functions just return paths. - var fs = require('fs-extra'); var shell = require('shelljs'); + var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. var listFilePath = 'etc/up-serve/servers.up'; -function list(domain, inPort, outPort) { +function appendToList(domain, outPort, inPort) { fs.ensureFileSync(listFilePath) // Creates directory if doesn't exist if (inPort) { @@ -19,4 +19,4 @@ function list(domain, inPort, outPort) { fs.appendFileSync(listFilePath, addr); } -module.exports = listFile; \ No newline at end of file +module.exports = appendToList; \ No newline at end of file From 6d7523b3fb21b40dcc3f208bb7bc3e62a7520f3f Mon Sep 17 00:00:00 2001 From: shunmusanjay Date: Tue, 7 Nov 2017 03:52:39 +0530 Subject: [PATCH 04/15] listFile.js delete line if domain | outPort exist --- index.js | 114 +++++++++++++++++++++++++++--------------------------- utils/listFile.js | 42 +++++++++++++++++++- 2 files changed, 97 insertions(+), 59 deletions(-) diff --git a/index.js b/index.js index fb1f0ce..94ba248 100644 --- a/index.js +++ b/index.js @@ -15,72 +15,72 @@ var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); -appendToList("example.com", "80"); -appendToList("example2.com", "80", "4000"); +// appendToList("example.com", "80"); +// appendToList("example2.com", "80", "4000"); // 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` -// program -// .version('0.1.5'); +program + .version('0.1.5'); -// program -// .command('static [outPort]') -// .description('Create a static server at this folder.') -// .action(function (domain, outPort) { //If outport is not given, 80 is set as default. Later, change this default to reflect nginx's settings. -// outPort = outPort || "80"; // This is a string because regex needs to validate it. -// 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!"); -// }); +program + .command('static [outPort]') + .description('Create a static server at this folder.') + .action(function (domain, outPort) { //If outport is not given, 80 is set as default. Later, change this default to reflect nginx's settings. + outPort = outPort || "80"; // This is a string because regex needs to validate it. + 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!"); + }); -// program -// .command('proxy [outPort]') -// .description('Create a proxy server, listening at port number.') -// .action(function (domain, inPort, outPort) { //Inbound port is necessary, but outbound is set to 80 by default. Again, will change this to reflect nginx's settings. -// outPort = outPort || "80"; // This is a string because regex needs to validate it. -// 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!"); -// }); +program + .command('proxy [outPort]') + .description('Create a proxy server, listening at port number.') + .action(function (domain, inPort, outPort) { //Inbound port is necessary, but outbound is set to 80 by default. Again, will change this to reflect nginx's settings. + outPort = outPort || "80"; // This is a string because regex needs to validate it. + 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!"); + }); -// program -// .command('list') -// .description('List all available servers.') -// .action(function () { -// // Stuff happens here -// }); +program + .command('list') + .description('List all available servers.') + .action(function () { + // Stuff happens here + }); -// program -// .command('kill [ourPort]') -// .description('Kill a server.') -// .action(function (domain, outPort) { -// outPort = outPort || "80"; // This is a string because regex needs to validate it. -// killServer(domain, outPort); -// console.log("\nDone! Your server has been killed!\n"); -// }); +program + .command('kill [ourPort]') + .description('Kill a server.') + .action(function (domain, outPort) { + outPort = outPort || "80"; // This is a string because regex needs to validate it. + killServer(domain, outPort); + console.log("\nDone! Your server has been killed!\n"); + }); -// 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."); -// }); +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."); + }); -// // Adds custom help text to the automatically generated help. -// program.on('--help', function () { -// console.log(''); -// console.log(' Usage:'); -// console.log(''); -// console.log(' ', chalk.yellow('$ up'), chalk.cyan('static'), chalk.blue('domain-name')); -// console.log(' Set up a static server at domain-name'); -// console.log(''); -// console.log(' ', chalk.yellow('$ up'), chalk.cyan('proxy'), chalk.blue('domain-name port-number')); -// console.log(' Set up a proxy server listening at port-number'); -// console.log(''); -// }); +// Adds custom help text to the automatically generated help. +program.on('--help', function () { + console.log(''); + console.log(' Usage:'); + console.log(''); + console.log(' ', chalk.yellow('$ up'), chalk.cyan('static'), chalk.blue('domain-name')); + console.log(' Set up a static server at domain-name'); + console.log(''); + console.log(' ', chalk.yellow('$ up'), chalk.cyan('proxy'), chalk.blue('domain-name port-number')); + console.log(' Set up a proxy server listening at port-number'); + console.log(''); +}); -// // Parses commands passed to `up` and chooses one of the above commands. -// program.parse(process.argv); \ No newline at end of file +// Parses commands passed to `up` and chooses one of the above commands. +program.parse(process.argv); \ No newline at end of file diff --git a/utils/listFile.js b/utils/listFile.js index 56ddfc9..ce84905 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -14,9 +14,47 @@ function appendToList(domain, outPort, inPort) { } else { var addr = domain + ':' + outPort + ' ' + 'static' + EOL; } + //reads file to check if domain already exist + var data = fs.readFileSync(listFilePath); + + var dataString = data.toString().split("\n"); + + var isDomainUnique = dataString.search(domain); // returns -1 if not found else line count + var isOutPortUnique = dataString.search(outPort); // returns -1 if not found else line count + + if (isDomainUnique == -1 && isOutPortUnique == -1) { + //wirtes to the file in path + fs.appendFileSync(listFilePath, addr); + } else if (isDomainUnique == -1 || isOutPortUnique == -1) { + if (isDomainUnique == -1) { + var deleteOutPortLine = dataString.slice(isOutPortUnique).join('\n'); + fs.appendFileSync(listFilePath, deleteOutPortLine); + fs.appendFileSync(listFilePath, addr); + + } else { + var deleteDomainLine = dataString.slice(isDomainUnique).join('\n'); + fs.appendFileSync(listFilePath, deleteDomainLine); + fs.appendFileSync(listFilePath, addr); + } + + } else { + var deleteDomainLine = dataString.slice(isDomainUnique).join('\n'); + fs.appendFileSync(listFilePath, deleteDomainLine); + + var data = fs.readFileSync(listFilePath); + + var dataString = data.toString().split("\n"); + var isOutPortUnique = dataString.search(outPort); // returns -1 if not found else line count + + var deleteOutPortLine = dataString.slice(isOutPortUnique).join('\n'); + fs.appendFileSync(listFilePath, deleteOutPortLine); + + fs.appendFileSync(listFilePath, addr); + + } + + - //wirtes to the file in path - fs.appendFileSync(listFilePath, addr); } module.exports = appendToList; \ No newline at end of file From 1e3e404cc550ff620429b6a590c45a47ca6360f9 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 7 Nov 2017 12:49:02 +0530 Subject: [PATCH 05/15] Working on reading and writing json --- etc/up-serve/servers.up | 0 index.js | 6 ++-- package-lock.json | 2 +- utils/listFile.js | 83 +++++++++++++++++++++---------------------------- 4 files changed, 38 insertions(+), 53 deletions(-) create mode 100644 etc/up-serve/servers.up mode change 100644 => 100755 index.js diff --git a/etc/up-serve/servers.up b/etc/up-serve/servers.up new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 94ba248..cf8e6e7 --- a/index.js +++ b/index.js @@ -14,10 +14,8 @@ var createProxyServer = require('./actions/createProxyServer'); var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); - -// appendToList("example.com", "80"); -// appendToList("example2.com", "80", "4000"); - +appendToList("example.com", "80"); +appendToList("example2.com", "80", "4000"); // 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` diff --git a/package-lock.json b/package-lock.json index 0b002a6..bf62478 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "up-serve", - "version": "0.1.3", + "version": "0.1.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/utils/listFile.js b/utils/listFile.js index ce84905..fc5abdb 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -1,59 +1,46 @@ -// These functions just return paths. var fs = require('fs-extra'); var shell = require('shelljs'); var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. -var listFilePath = 'etc/up-serve/servers.up'; +var listFilePath = "/etc/up-serve/servers.up"; function appendToList(domain, outPort, inPort) { - fs.ensureFileSync(listFilePath) // Creates directory if doesn't exist - - if (inPort) { - var addr = domain + ':' + outPort + ' ' + 'proxy' + ' ' + inPort + EOL; - } else { - var addr = domain + ':' + outPort + ' ' + 'static' + EOL; - } - //reads file to check if domain already exist - var data = fs.readFileSync(listFilePath); - - var dataString = data.toString().split("\n"); - - var isDomainUnique = dataString.search(domain); // returns -1 if not found else line count - var isOutPortUnique = dataString.search(outPort); // returns -1 if not found else line count - - if (isDomainUnique == -1 && isOutPortUnique == -1) { - //wirtes to the file in path - fs.appendFileSync(listFilePath, addr); - } else if (isDomainUnique == -1 || isOutPortUnique == -1) { - if (isDomainUnique == -1) { - var deleteOutPortLine = dataString.slice(isOutPortUnique).join('\n'); - fs.appendFileSync(listFilePath, deleteOutPortLine); - fs.appendFileSync(listFilePath, addr); - - } else { - var deleteDomainLine = dataString.slice(isDomainUnique).join('\n'); - fs.appendFileSync(listFilePath, deleteDomainLine); - fs.appendFileSync(listFilePath, addr); - } - - } else { - var deleteDomainLine = dataString.slice(isDomainUnique).join('\n'); - fs.appendFileSync(listFilePath, deleteDomainLine); - - var data = fs.readFileSync(listFilePath); - - var dataString = data.toString().split("\n"); - var isOutPortUnique = dataString.search(outPort); // returns -1 if not found else line count - - var deleteOutPortLine = dataString.slice(isOutPortUnique).join('\n'); - fs.appendFileSync(listFilePath, deleteOutPortLine); - - fs.appendFileSync(listFilePath, addr); - - } - + if (!inPort) { + var domBlock = { + domain: { + "type": "static", + "outPort": outPort, + "inPort": undefined + } + } + } else { + var domBlock = { + domain: { + "type": "proxy", + "outPort": outPort, + "inPort": inPort + } + } + } + if (fs.existsSync(listFilePath)) { + var jsonFile = fs.readFileSync(listFilePath); + jsonFile = JSON.parse(jsonFile); + + for (block in jsonFile) { + if (block.domain == domain && block.outPort == outPort) { + delete jsonFile.block; + return; + } + } + + jsonFile.domain = domBlock.domain; + } + else { + var jsonFile = JSON.stringify(domBlock) + EOL; + } + fs.writeFileSync(listFilePath, jsonFile); } From edea53acd7424588f5a41bd22286acaeb4d80bc8 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 7 Nov 2017 12:50:35 +0530 Subject: [PATCH 06/15] Removed etc folder --- etc/up-serve/servers.up | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 etc/up-serve/servers.up diff --git a/etc/up-serve/servers.up b/etc/up-serve/servers.up deleted file mode 100644 index e69de29..0000000 From a3c3b08cc120b1c16ae69af6c1c41c0a6ff8b9cb Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 7 Nov 2017 23:03:24 +0530 Subject: [PATCH 07/15] appendToList works --- index.js | 7 +++++-- utils/listFile.js | 38 +++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index cf8e6e7..90fcdc1 100755 --- a/index.js +++ b/index.js @@ -14,9 +14,12 @@ var createProxyServer = require('./actions/createProxyServer'); var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); -appendToList("example.com", "80"); -appendToList("example2.com", "80", "4000"); +var jsonFile; +// appendToList("example.com", "80"); +// appendToList("example2.com", "80", "4000"); +// appendToList("example2.com", "80", "4444"); +console.log(jsonFile); // 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` diff --git a/utils/listFile.js b/utils/listFile.js index fc5abdb..2572705 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -7,41 +7,57 @@ var listFilePath = "/etc/up-serve/servers.up"; function appendToList(domain, outPort, inPort) { + var jsonFile = {}; + var domBlock; + if (!inPort) { - var domBlock = { - domain: { + domBlock = { "type": "static", "outPort": outPort, "inPort": undefined - } } } else { - var domBlock = { - domain: { + domBlock = { "type": "proxy", "outPort": outPort, "inPort": inPort - } } } + + jsonFile = { + } + + var newBlock = { + + } +/* + Object.assign(jsonFile, domain.domBlock) + return jsonFile; +*/ + + if (fs.existsSync(listFilePath)) { - var jsonFile = fs.readFileSync(listFilePath); + jsonFile = fs.readFileSync(listFilePath); jsonFile = JSON.parse(jsonFile); for (block in jsonFile) { - if (block.domain == domain && block.outPort == outPort) { + if (block.domain == domain && block.domain.outPort == outPort) { delete jsonFile.block; return; } } - jsonFile.domain = domBlock.domain; + jsonFile[domain] = domBlock; + jsonFile = JSON.stringify(jsonFile); } else { - var jsonFile = JSON.stringify(domBlock) + EOL; + jsonFile = { + } + jsonFile[domain] = domBlock; + jsonFile = JSON.stringify(jsonFile); } fs.writeFileSync(listFilePath, jsonFile); - + } module.exports = appendToList; \ No newline at end of file From af115cb81c6d8443091e3b3c9df1d4c32cb755d4 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 7 Nov 2017 23:27:20 +0530 Subject: [PATCH 08/15] Added json-beautify --- index.js | 6 +++--- package-lock.json | 5 +++++ package.json | 1 + utils/listFile.js | 20 +++++--------------- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 90fcdc1..d69294e 100755 --- a/index.js +++ b/index.js @@ -15,9 +15,9 @@ var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); var jsonFile; -// appendToList("example.com", "80"); -// appendToList("example2.com", "80", "4000"); -// appendToList("example2.com", "80", "4444"); +//appendToList("example.com", "80"); +//appendToList("example2.com", "80", "4000"); +//appendToList("example2.com", "80", "4444"); console.log(jsonFile); // 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. diff --git a/package-lock.json b/package-lock.json index bf62478..27e3a0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -121,6 +121,11 @@ "resolved": "https://registry.npmjs.org/interpret/-/interpret-1.0.4.tgz", "integrity": "sha1-ggzdWIuGj/sZGoCVBtbJyPISsbA=" }, + "json-beautify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/json-beautify/-/json-beautify-1.0.1.tgz", + "integrity": "sha1-WYtQ1Mjqm4/KWru0C34svTrUwvw=" + }, "jsonfile": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", diff --git a/package.json b/package.json index 471228b..e4f5998 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "chalk": "^2.3.0", "commander": "^2.11.0", "fs-extra": "^4.0.2", + "json-beautify": "^1.0.1", "shelljs": "^0.7.8", "validator": "^9.1.1" } diff --git a/utils/listFile.js b/utils/listFile.js index 2572705..4331a5e 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -1,5 +1,6 @@ var fs = require('fs-extra'); var shell = require('shelljs'); +var beautifyJSON = require("json-beautify"); var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. @@ -24,17 +25,7 @@ function appendToList(domain, outPort, inPort) { } } - jsonFile = { - } - - var newBlock = { - - } -/* - Object.assign(jsonFile, domain.domBlock) - return jsonFile; -*/ - + jsonFile = {} if (fs.existsSync(listFilePath)) { jsonFile = fs.readFileSync(listFilePath); @@ -48,13 +39,12 @@ function appendToList(domain, outPort, inPort) { } jsonFile[domain] = domBlock; - jsonFile = JSON.stringify(jsonFile); + jsonFile = beautifyJSON(jsonFile, null, 2, 30); } else { - jsonFile = { - } + jsonFile = {} jsonFile[domain] = domBlock; - jsonFile = JSON.stringify(jsonFile); + jsonFile = beautifyJSON(jsonFile); } fs.writeFileSync(listFilePath, jsonFile); From 38d72b12294cbded4b9a3cd232b386a54feffb73 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 7 Nov 2017 23:31:55 +0530 Subject: [PATCH 09/15] Removed redundant initialization --- utils/listFile.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/utils/listFile.js b/utils/listFile.js index 4331a5e..cbd6770 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -24,8 +24,6 @@ function appendToList(domain, outPort, inPort) { "inPort": inPort } } - - jsonFile = {} if (fs.existsSync(listFilePath)) { jsonFile = fs.readFileSync(listFilePath); @@ -42,7 +40,6 @@ function appendToList(domain, outPort, inPort) { jsonFile = beautifyJSON(jsonFile, null, 2, 30); } else { - jsonFile = {} jsonFile[domain] = domBlock; jsonFile = beautifyJSON(jsonFile); } @@ -50,4 +47,4 @@ function appendToList(domain, outPort, inPort) { } -module.exports = appendToList; \ No newline at end of file +module.exports = appendToList; From 48516a66121dea1e8d3c1174b6f46ecd71be5a92 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 7 Nov 2017 23:55:33 +0530 Subject: [PATCH 10/15] Create listFile directory if doesn't exist --- index.js | 19 +++++++++++++------ utils/listFile.js | 5 ++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index d69294e..90e5583 100755 --- a/index.js +++ b/index.js @@ -14,17 +14,19 @@ var createProxyServer = require('./actions/createProxyServer'); var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); -var jsonFile; //appendToList("example.com", "80"); //appendToList("example2.com", "80", "4000"); //appendToList("example2.com", "80", "4444"); -console.log(jsonFile); // 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` program - .version('0.1.5'); + .version('0.1.5') + .arguments(' [options]') + .action ( function(cmd) { + var cmdValue = cmd; + }); program .command('static [outPort]') @@ -65,9 +67,9 @@ program }); program - .command('*') // This should pick invalid commands, but it doesn't, yet. + .command('*') // This picks invalid commands, but doesn't pick empty commands, yet. .action(function () { - console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help."); + console.log("\n Invalid command. Type " + chalk.cyan('up --help') + " for help.\n"); }); // Adds custom help text to the automatically generated help. @@ -84,4 +86,9 @@ program.on('--help', function () { }); // Parses commands passed to `up` and chooses one of the above commands. -program.parse(process.argv); \ No newline at end of file +program.parse(process.argv); + +if (typeof cmdValue == 'undefined') { + console.log("\nNo commands given. Exiting...\n"); + process.exit(1); +} \ No newline at end of file diff --git a/utils/listFile.js b/utils/listFile.js index 4331a5e..814ec2a 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -4,10 +4,13 @@ var beautifyJSON = require("json-beautify"); var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. -var listFilePath = "/etc/up-serve/servers.up"; +var listFileDir = "/etc/up-serve/"; +var listFilePath = listFileDir + "servers.up"; function appendToList(domain, outPort, inPort) { + shell.mkdir('-p', listFileDir); + var jsonFile = {}; var domBlock; From 7df9ccf425f05d0bd945295f1c2f3f245dea6211 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 7 Nov 2017 23:58:28 +0530 Subject: [PATCH 11/15] Add listFile directory if doens't exist --- utils/listFile.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/utils/listFile.js b/utils/listFile.js index 814ec2a..c278460 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -27,8 +27,6 @@ function appendToList(domain, outPort, inPort) { "inPort": inPort } } - - jsonFile = {} if (fs.existsSync(listFilePath)) { jsonFile = fs.readFileSync(listFilePath); @@ -45,7 +43,6 @@ function appendToList(domain, outPort, inPort) { jsonFile = beautifyJSON(jsonFile, null, 2, 30); } else { - jsonFile = {} jsonFile[domain] = domBlock; jsonFile = beautifyJSON(jsonFile); } From 183680ac35d98e02c932fe2ad8b9c5fddc79ab52 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 7 Nov 2017 23:58:38 +0530 Subject: [PATCH 12/15] Revert "Create listFile directory if doesn't exist" This reverts commit 48516a66121dea1e8d3c1174b6f46ecd71be5a92. modified: index.js --- index.js | 19 ++++++------------- utils/listFile.js | 5 +---- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 90e5583..d69294e 100755 --- a/index.js +++ b/index.js @@ -14,19 +14,17 @@ var createProxyServer = require('./actions/createProxyServer'); var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); +var jsonFile; //appendToList("example.com", "80"); //appendToList("example2.com", "80", "4000"); //appendToList("example2.com", "80", "4444"); +console.log(jsonFile); // 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` program - .version('0.1.5') - .arguments(' [options]') - .action ( function(cmd) { - var cmdValue = cmd; - }); + .version('0.1.5'); program .command('static [outPort]') @@ -67,9 +65,9 @@ program }); program - .command('*') // This picks invalid commands, but doesn't pick empty commands, yet. + .command('*') // This should pick invalid commands, but it doesn't, yet. .action(function () { - console.log("\n Invalid command. Type " + chalk.cyan('up --help') + " for help.\n"); + console.log("Invalid command. Type " + chalk.cyan('up --help') + " for help."); }); // Adds custom help text to the automatically generated help. @@ -86,9 +84,4 @@ program.on('--help', function () { }); // Parses commands passed to `up` and chooses one of the above commands. -program.parse(process.argv); - -if (typeof cmdValue == 'undefined') { - console.log("\nNo commands given. Exiting...\n"); - process.exit(1); -} \ No newline at end of file +program.parse(process.argv); \ No newline at end of file diff --git a/utils/listFile.js b/utils/listFile.js index c278460..7451ac7 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -4,13 +4,10 @@ var beautifyJSON = require("json-beautify"); var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. -var listFileDir = "/etc/up-serve/"; -var listFilePath = listFileDir + "servers.up"; +var listFilePath = "/etc/up-serve/servers.up"; function appendToList(domain, outPort, inPort) { - shell.mkdir('-p', listFileDir); - var jsonFile = {}; var domBlock; From 6058823faf4847ea0608fc82851fb76578928332 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Wed, 8 Nov 2017 00:01:56 +0530 Subject: [PATCH 13/15] Removed test lines from index.js --- index.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/index.js b/index.js index d69294e..efb8cfd 100755 --- a/index.js +++ b/index.js @@ -14,12 +14,6 @@ var createProxyServer = require('./actions/createProxyServer'); var createStaticServer = require('./actions/createStaticServer'); var killServer = require('./actions/killServer'); -var jsonFile; -//appendToList("example.com", "80"); -//appendToList("example2.com", "80", "4000"); -//appendToList("example2.com", "80", "4444"); - -console.log(jsonFile); // 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` From f8553d78cb527d97118c0cf1c2dc18fb5888d839 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Wed, 8 Nov 2017 00:50:19 +0530 Subject: [PATCH 14/15] 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 From 23fd1b9e4351a6c192b7455ad584903b2817906e Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Wed, 8 Nov 2017 01:16:15 +0530 Subject: [PATCH 15/15] `up kill` works, with known exceptions: The code block that removes a server block if it exists does not work yet. Appears twice in `listFile.js` - `appendToList` and `removeFromList` --- actions/createStaticServer.js | 1 + actions/killServer.js | 2 ++ utils/listFile.js | 17 +++++++++-------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/actions/createStaticServer.js b/actions/createStaticServer.js index 5ad99b4..65e33e4 100644 --- a/actions/createStaticServer.js +++ b/actions/createStaticServer.js @@ -5,6 +5,7 @@ var path = require('path'); var npath = require('../utils/nginxPath'); var conf = require('../utils/nginxConf'); var nginxReload = require('../utils/nginxReload'); +var appendToList = require('../utils/listFile').appendToList; var currentPath = path.normalize(process.cwd()); var EOL = require('os').EOL; // \n if used on Linux, \r\n if used on Windows. diff --git a/actions/killServer.js b/actions/killServer.js index 065e9da..01d62d3 100644 --- a/actions/killServer.js +++ b/actions/killServer.js @@ -3,6 +3,7 @@ var shell = require('shelljs'); var npath = require('../utils/nginxPath'); var conf = require('../utils/nginxConf'); var nginxReload = require('../utils/nginxReload'); +var removeFromList = require('../utils/listFile').removeFromList; function killServer(domain, outPort) { shell.rm('-rf', conf(npath.enabledSites(), domain, outPort)); @@ -10,6 +11,7 @@ function killServer(domain, outPort) { shell.rm('-rf', npath.webRootDomain(domain, outPort)); nginxReload(); + removeFromList(domain, outPort); } module.exports = killServer; \ No newline at end of file diff --git a/utils/listFile.js b/utils/listFile.js index ae59f0b..66f19bf 100644 --- a/utils/listFile.js +++ b/utils/listFile.js @@ -31,7 +31,7 @@ function appendToList(domain, outPort, inPort) { jsonFile = JSON.parse(jsonFile); for (block in jsonFile) { - if (block.domain == domain && block.domain.outPort == outPort) { + if (block[domain] == domain && block[domain].outPort == outPort) { delete jsonFile.block; return; } @@ -45,11 +45,6 @@ function appendToList(domain, outPort, inPort) { jsonFile = beautifyJSON(jsonFile); } fs.writeFileSync(listFilePath, jsonFile); - -} - -function readServers () { - return JSON.parse(fs.readFileSync(listFilePath)); } function removeFromList (domain, outPort) { @@ -59,8 +54,9 @@ function removeFromList (domain, outPort) { jsonFile = JSON.parse(jsonFile); for (block in jsonFile) { - if (block.domain == domain && block.domain.outPort == outPort) { + if (block[domain] == domain && block[domain].outPort == outPort) { delete jsonFile.block; + console.log('\nDomain was deleted successfully.\n'); return; } } @@ -73,5 +69,10 @@ function removeFromList (domain, outPort) { fs.writeFileSync(listFilePath, jsonFile); } +function readServers () { + return JSON.parse(fs.readFileSync(listFilePath)); +} + module.exports.appendToList = appendToList; -module.exports.readServers = readServers; \ No newline at end of file +module.exports.readServers = readServers; +module.exports.removeFromList = removeFromList; \ No newline at end of file