Browse Source

Working on reading and writing json

pull/3/head
Muthu Kumar 7 years ago
parent
commit
1e3e404cc5
  1. 0
      etc/up-serve/servers.up
  2. 6
      index.js
  3. 2
      package-lock.json
  4. 83
      utils/listFile.js

0
etc/up-serve/servers.up

6
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`

2
package-lock.json

@ -1,6 +1,6 @@
{
"name": "up-serve",
"version": "0.1.3",
"version": "0.1.5",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

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

Loading…
Cancel
Save