forked from tilemill-project/tilemill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.server.bones
94 lines (90 loc) · 3.37 KB
/
Config.server.bones
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
var fs = require('fs');
var path = require('path');
var Step = require('step');
var paths = {
user: path.join(process.env.HOME, '.tilemill/config.json'),
vendor: path.resolve(__dirname + '/../lib/config.defaults.json')
};
var create_files = require('../lib/create_files');
models.Config.defaults = JSON.parse(fs.readFileSync(paths.vendor, 'utf8'));
models.Config.prototype.sync = function(method, model, success, error) {
if (method === 'delete') return error(new Error('Method not supported.'));
switch (method) {
case 'defaults':
var data = _(defaults).clone();
if (data.files.indexOf(process.env.HOME) === 0)
data.files = data.files.replace(process.env.HOME, '~');
return success(data);
break;
case 'read':
var data = _(Bones.plugin.config).clone();
if (data.files.indexOf(process.env.HOME) === 0)
data.files = data.files.replace(process.env.HOME, '~');
return success(data);
break;
case 'create':
case 'update':
// Filter out keys that may not be written.
var allowedKeys = [
'files',
'syncAccount',
'syncAccessToken',
'updates',
'updatesTime',
'updatesVersion',
'profile',
'guid',
'httpProxy',
'verbose'
];
var data = _(model.toJSON()).reduce(function(memo, val, key) {
if (key === 'files') val = val.replace(/^~/, process.env.HOME);
if (_(allowedKeys).include(key)) memo[key] = val;
return memo;
}, {});
Step(function() {
// validate that the 'files' option is okay to write to
// https://github.com/tilemill-project/tilemill/issues/2024
if (data.files) {
try {
create_files.init_dirs(['export','cache'],data);
this();
} catch (err) {
throw new Error('The Documents path "' + data.files + '" cannot be written to, please choose a valid location (' + err.message + ')');
}
} else {
this();
}
}, function(err) {
if (err) throw err;
fs.readFile(paths.user, 'utf8', this);
// Write changes to user config file.
}, function(err, current) {
if (err && err.code !== 'ENOENT') throw err;
// Catch & blow away invalid user JSON.
try { current = JSON.parse(current); }
catch (e) { current = {}; }
data = _(current).extend(data);
fs.writeFile(paths.user, JSON.stringify(data, null, 2), this);
// May contain sensitive info. Set secure permissions.
}, function(err) {
if (err) throw err;
fs.chmod(paths.user, 0600, this);
// Update runtime config in memory.
}, function(err) {
if (err) return error(err);
Bones.plugin.config = _(Bones.plugin.config).extend(data);
return success({});
});
break;
case 'delete':
return error(new Error('Method not supported.'));
break;
}
};
models.Config.prototype.validate = function(attr) {
if (attr.httpProxy && attr.httpProxy.indexOf('http://') !== 0) {
return "HTTP Proxy must start with http://";
}
return this.validateAttributes(attr);
}