masq
Version:
A simple local dns server extracted from Pow
123 lines (98 loc) • 3.23 kB
JavaScript
// Generated by CoffeeScript 2.0.0-beta2
(function() {
var Installer, InstallerFile, async, chown, daemonSource, fs, mkdirp, path, resolverSource, util;
async = require("async");
fs = require("fs");
path = require("path");
mkdirp = require("mkdirp");
({chown} = require("./utils"));
util = require("util");
resolverSource = require("./templates/resolver");
daemonSource = require("./templates/cx.masq.masqd.plist");
InstallerFile = class InstallerFile {
constructor(path1, source, root = false, mode = 0o644) {
this.path = path1;
this.root = root;
this.mode = mode;
this.source = source.trim();
}
isStale(callback) {
return fs.exists(this.path, (exists) => {
if (exists) {
return fs.readFile(this.path, "utf8", (err, contents) => {
if (err) {
return callback(true);
} else {
return callback(this.source !== contents.trim());
}
});
} else {
return callback(true);
}
});
}
vivifyPath(callback) {
return mkdirp(path.dirname(this.path), 0o755, callback);
}
writeFile(callback) {
return fs.writeFile(this.path, this.source, "utf8", callback);
}
setOwnership(callback) {
if (this.root) {
return chown(this.path, "root:wheel", callback);
} else {
return callback(false);
}
}
setPermissions(callback) {
return fs.chmod(this.path, this.mode, callback);
}
install(callback) {
return async.series([this.vivifyPath.bind(this), this.writeFile.bind(this), this.setOwnership.bind(this), this.setPermissions.bind(this)], callback);
}
};
module.exports = Installer = class Installer {
static getSystemInstaller(configuration) {
var domain, files, i, len, ref;
files = [];
ref = configuration.domains;
for (i = 0, len = ref.length; i < len; i++) {
domain = ref[i];
files.push(new InstallerFile(`/etc/resolver/${domain}`, resolverSource(configuration), true));
}
return new Installer(files);
}
static getLocalInstaller(configuration) {
return new Installer([new InstallerFile(`${process.env.HOME}/Library/LaunchAgents/cx.masq.masqd.plist`, daemonSource(configuration))]);
}
constructor(files1 = []) {
this.files = files1;
}
getStaleFiles(callback) {
return async.select(this.files, function(file, proceed) {
return file.isStale(proceed);
}, callback(this.files));
}
needsRootPrivileges(callback) {
return this.getStaleFiles(function(files) {
return async.detect(files, function(file, proceed) {
return proceed(file.root);
}, function(result) {
return callback(result != null);
});
});
}
install(callback) {
return this.getStaleFiles(function(files) {
return async.forEach(files, function(file, proceed) {
return file.install(function(err) {
if (!err) {
console.log(file.path);
}
return proceed(err);
});
}, callback);
});
}
};
}).call(this);