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); }