This repository was archived by the owner on May 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathinstaller.js
138 lines (109 loc) · 4.2 KB
/
installer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Generated by CoffeeScript 1.6.2
(function() {
var Installer, InstallerFile, async, chown, daemonSource, firewallSource, fs, mkdirp, path, resolverSource, util,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
async = require("async");
fs = require("fs");
path = require("path");
mkdirp = require("./util").mkdirp;
chown = require("./util").chown;
util = require("util");
resolverSource = require("./templates/installer/resolver");
firewallSource = require("./templates/installer/cx.pow.firewall.plist");
daemonSource = require("./templates/installer/cx.pow.powd.plist");
InstallerFile = (function() {
function InstallerFile(path, source, root, mode) {
this.path = path;
this.root = root != null ? root : false;
this.mode = mode != null ? mode : 0x1a4;
this.setPermissions = __bind(this.setPermissions, this);
this.setOwnership = __bind(this.setOwnership, this);
this.writeFile = __bind(this.writeFile, this);
this.vivifyPath = __bind(this.vivifyPath, this);
this.source = source.trim();
}
InstallerFile.prototype.isStale = function(callback) {
var _this = this;
return fs.exists(this.path, function(exists) {
if (exists) {
return fs.readFile(_this.path, "utf8", function(err, contents) {
if (err) {
return callback(true);
} else {
return callback(_this.source !== contents.trim());
}
});
} else {
return callback(true);
}
});
};
InstallerFile.prototype.vivifyPath = function(callback) {
return mkdirp(path.dirname(this.path), callback);
};
InstallerFile.prototype.writeFile = function(callback) {
return fs.writeFile(this.path, this.source, "utf8", callback);
};
InstallerFile.prototype.setOwnership = function(callback) {
if (this.root) {
return chown(this.path, "root:wheel", callback);
} else {
return callback(false);
}
};
InstallerFile.prototype.setPermissions = function(callback) {
return fs.chmod(this.path, this.mode, callback);
};
InstallerFile.prototype.install = function(callback) {
return async.series([this.vivifyPath, this.writeFile, this.setOwnership, this.setPermissions], callback);
};
return InstallerFile;
})();
module.exports = Installer = (function() {
Installer.getSystemInstaller = function(configuration) {
var domain, files, _i, _len, _ref;
this.configuration = configuration;
files = [new InstallerFile("/Library/LaunchDaemons/cx.pow.firewall.plist", firewallSource(this.configuration), true)];
_ref = this.configuration.domains;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
domain = _ref[_i];
files.push(new InstallerFile("/etc/resolver/" + domain, resolverSource(this.configuration), true));
}
return new Installer(files);
};
Installer.getLocalInstaller = function(configuration) {
this.configuration = configuration;
return new Installer([new InstallerFile("" + process.env.HOME + "/Library/LaunchAgents/cx.pow.powd.plist", daemonSource(this.configuration))]);
};
function Installer(files) {
this.files = files != null ? files : [];
}
Installer.prototype.getStaleFiles = function(callback) {
return async.select(this.files, function(file, proceed) {
return file.isStale(proceed);
}, callback);
};
Installer.prototype.needsRootPrivileges = function(callback) {
return this.getStaleFiles(function(files) {
return async.detect(files, function(file, proceed) {
return proceed(file.root);
}, function(result) {
return callback(result != null);
});
});
};
Installer.prototype.install = function(callback) {
return this.getStaleFiles(function(files) {
return async.forEach(files, function(file, proceed) {
return file.install(function(err) {
if (!err) {
util.puts(file.path);
}
return proceed(err);
});
}, callback);
});
};
return Installer;
})();
}).call(this);