UNPKG

pp-parachute

Version:
100 lines (83 loc) 2.33 kB
var fs = require('fs'); var path = require('path'); var mkdirp = require('mkdirp'); var parachuteJSONPath = "/tmp/pp-parachute/chute.json"; var json = null; var defaultConfig = { config: { port: 8080, host: 'localhost', proxyHost: 'localhost', proxyPort: 3000 }, applications: {} }; var ensureJSON = function () { if (json) return; if (!fs.existsSync(parachuteJSONPath)) { var path = parachuteJSONPath.split('/'); path.pop(); mkdirp.sync(path.join('/')); fs.writeFileSync(parachuteJSONPath, JSON.stringify(defaultConfig, null, 4)); } json = require(parachuteJSONPath); }; var reset = function () { json = null; }; var getProxyHost = function() { ensureJSON(); return json.config["proxyHost"] || defaultConfig.config.proxyHost; }; var getProxyPort = function() { ensureJSON(); return json.config["proxyPort"] || defaultConfig.config.proxyPort; }; var getHost = function () { ensureJSON(); return json.config["host"] || defaultConfig.config.host; }; var getPort = function () { ensureJSON(); return json.config["port"] || defaultConfig.config.port; }; var getHostAndPort = function() { ensureJSON(); return getHost() + ":" + getPort(); }; var getBaseUrl = function() { return 'http://' + getHostAndPort(); }; var getApplications = function () { ensureJSON(); return json["applications"] || {}; }; var addApplication = function(appName, appPath) { json["applications"][appName] = appPath; writeJson(); }; var removeApplication = function(appName) { delete json["applications"][appName]; writeJson(); }; var renameApplication = function(appName, newName) { json["applications"][newName] = json["applications"][appName]; delete json["applications"][appName]; writeJson(); }; var writeJson = function() { fs.writeFileSync(parachuteJSONPath, JSON.stringify(json, null, 4)); }; module.exports = { reset: reset, getHost: getHost, getPort: getPort, getHostAndPort: getHostAndPort, getProxyHost: getProxyHost, getProxyPort: getProxyPort, getBaseUrl: getBaseUrl, getApplications: getApplications, addApplication: addApplication, removeApplication: removeApplication, renameApplication: renameApplication };