From 6d7523b3fb21b40dcc3f208bb7bc3e62a7520f3f Mon Sep 17 00:00:00 2001 From: shunmusanjay Date: Tue, 7 Nov 2017 03:52:39 +0530 Subject: [PATCH] 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