-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
executable file
·494 lines (434 loc) · 16.6 KB
/
index.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var utils = require('./lib/utils');
var serial = require('./lib/serial');
var exists = require('fs').exists || require('path').exists;
// Helper: Determine if an error should just be logged.
function triageError(err) {
if (err && err.name == 'ProjectMill') {
console.warn(err.toString());
err = null;
}
return err;
}
// Helper: Process a file.
function fileprocess(source, dest, processor, callback) {
fs.readFile(source, 'utf8', function(err, data){
data = processor(data);
fs.writeFile(dest, data, 'utf8', callback);
});
}
// Processor: MML
function processMML(config) {
// add 'b' to 'a'
function assign(a, b, k) {
switch (typeof b[k]) {
case 'object':
iterObject(a[k], b[k]);
break;
case 'array':
iterArray(a[k], b[k]);
break;
case 'number':
case 'string':
case 'boolean':
a[k] = b[k];
break;
}
}
function iterObject(a, b) {
for (var k in b) {
assign(a, b, k);
}
}
function iterArray(a, b) {
b.forEach(function(v, i) {
assign(a, b, i);
});
}
return function(o) {
o = JSON.parse(o);
iterObject(o, config.mml);
return JSON.stringify(o, null, 2);
}
}
// Processor: MSS
function processMSS(config) {
var varMatch = /^@([\w-]+):[\W]?([^;]+);$/;
return function(o) {
var lines = o.split("\n");
for (var i = 0; i < lines.length; i++) {
lines[i] = lines[i].replace(varMatch, function(m, n, v, o, s) {
if (config.cartoVars[n] != undefined) {
return '@' + n +': '+ config.cartoVars[n] +';';
}
return s;
});
}
return lines.join("\n");
}
}
var usage = 'Usage: ./index.js <command> [-c ./config.json] [-t /path/to/tilemill]';
// Closure vars
var config = {},
tilemill = '',
argv = require('optimist').usage(usage).argv,
fileDir = argv.p || path.join(process.env.HOME, 'Documents', 'MapBox'),
replaceExisting = argv.f || false;
// If no command was issued bail.
if (!argv.mill && !argv.render && !argv.upload) {
console.warn('Error: missing command. Available commands are `--mill`, `--render`, `--upload`');
console.warn(usage);
process.exit(1);
}
// Try to locate TileMill
var tilemillPath = argv.t;
if (!tilemillPath && require('os').type() == 'Darwin') {
tilemillPath = '/Applications/TileMill.app/Contents/Resources';
}
else if (!tilemillPath) {
tilemillPath = '/usr/share/tilemill';
}
try {
tilemillPath = require.resolve(tilemillPath);
if (!argv.t) {
console.warn("Notice: using TileMill from '"+ tilemillPath +"'");
}
}
catch(err) {
console.warn("Error: could not locate TileMill. Looking in '"+ tilemillPath +"'");
console.warn(usage);
process.exit(1);
}
// Assemble the main actions.
var actions = [];
// Get our configuration
actions.push(function(next, err) {
if (err) return next(err);
var configFile = argv.c || 'config.json';
if (configFile.indexOf('/') !== 0) {
configFile = path.join(process.cwd(), configFile);
}
fs.readFile(configFile, 'utf8', next);
});
actions.push(function(next, err, data) {
if (err) return next(err);
try {
data = JSON.parse(data);
}
catch(err) {
return next(err);
}
data.forEach(function(v) {
// TODO check for other required elements.
if (v.source && v.destination) {
config[v.destination] = v;
v.source = path.join(fileDir, 'project', v.source);
v.destination = path.join(fileDir, 'project', v.destination);
}
else {
console.warn("Error: project missing required elements >> " + JSON.stringify(v));
}
});
next();
});
// Mill projects defined in configuration.
if (argv.mill) {
// Validate source paths.
actions.push(function(next, err) {
if (err) return next(err);
var projectDir = path.join(fileDir, 'project');
fs.readdir(projectDir, next);
});
actions.push(function(next, err, files) {
if (err) return next(err);
var paths = {};
files.forEach(function(v) {
if (v[0] == '.') return;
paths[path.join(fileDir, 'project', v)] = path.join(fileDir, 'project', v);
});
for (var i in config) {
if (paths[config[i].source] == undefined) {
console.warn("Error: source project doesn't exist >> " + JSON.stringify(config[i]));
delete config[i];
}
}
next();
});
actions.push(function(next, err) {
if (err) return next(err);
var mill = [];
Object.keys(config).forEach(function(i) {
mill.push(function(cb, err) {
err = triageError(err);
if (err) return cb(err);
exists(config[i].destination, cb);
});
mill.push(function(cb, found) {
if (!found) return cb();
if (replaceExisting) {
console.log('Notice: removing project '+ config[i].destination);
utils.recursiveDelete(config[i].destination, cb);
}
else {
var e = new Error('Skipping project '+ i);
e.name = "ProjectMill";
cb(e);
}
});
mill.push(function(cb, err) {
if (err) return cb(err);
utils.readdirr(config[i].source, cb);
});
mill.push(function(cb, err, files) {
if (err) return cb(err);
var setup = [];
files.forEach(function(filename) {
// Handle symlinks which come in as an object.
var linkSource = '';
if (typeof filename != 'string') {
linkSource = filename.source;
filename = filename.target
}
var destfile = path.join(config[i].destination, filename),
destdir = path.dirname(destfile),
sourcefile = path.join(config[i].source, filename);
// In the future the 'mml' file will always be called
// 'project.mml', but currently this isn't the case.
// TODO delete this when https://github.com/mapbox/tilemill/pull/970
// is merged.
if (path.extname(filename) == '.mml' && filename != 'project.mml') {
destfile = path.join(config[i].destination, i +'.mml');
}
setup.push(function(next) {
exists(destdir, next);
});
setup.push(function(next, err, found) {
if (found) return next();
utils.mkdirp(destdir, '0777', next);
});
setup.push(function(next, err) {
if (err) next(err);
if (linkSource) {
return fs.symlink(linkSource, destfile, next);
}
else if (config[i].mml && path.extname(filename) == '.mml') {
return fileprocess(sourcefile, destfile, processMML(config[i]), next);
}
else if (config[i].cartoVars && path.extname(filename) == '.mss') {
return fileprocess(sourcefile, destfile, processMSS(config[i]), next);
}
else {
return utils.filecopy(sourcefile, destfile, next);
}
});
})
serial(setup, function(err) {
if (err) console.warn(err);
cb();
});
});
mill.push(function(cb, err) {
if (!err) console.log('Notice: created project '+ config[i].destination);
cb(err);
});
});
serial(mill, function(err) {
next(triageError(err));
});
});
}
// Render all available projects.
if (argv.render) {
var spawn = require('child_process').spawn,
sqlite3 = require('sqlite3');
actions.push(function(next, err) {
if (err) return next(err);
var render = [];
Object.keys(config).forEach(function(i) {
var data = config[i],
destfile = path.join(fileDir, 'export', i + '.' + data.format);
render.push(function(cb, err) {
err = triageError(err);
if (err) return cb(err);
exists(destfile, cb);
});
render.push(function(cb, found) {
if (!found) return cb();
if (replaceExisting) {
console.log("Notice: deleting " + destfile);
fs.unlink(destfile, cb);
}
else {
var e = new Error('Skipping export ' + i);
e.name = "ProjectMill";
cb(e);
}
});
render.push(function(cb, err) {
if (err) return cb(err);
if (data.format == undefined) {
var err = new Error('export format not specified for `'+i+'`');
return cb(err);
}
var args = [];
// nice the export process.
args.push('-n19');
// node command
args.push(process.execPath);
// tilemill index.js
args.push(tilemillPath);
// export command
args.push('export');
// datasource
args.push(i);
// filepath
args.push(destfile);
// format, don't try to guess extension based on filepath
args.push('--format=' + data.format);
if (data.bbox) args.push('--bbox=' + data.bbox.join(','));
if (data.width) args.push('--width=' + data.width);
if (data.height) args.push('--height=' + data.height);
if (data.minzoom) args.push('--minzoom=' + data.minzoom);
if (data.maxzoom) args.push('--maxzoom=' + data.maxzoom);
if (data.maxzoom) args.push('--files=' + fileDir);
console.log('Notice: spawning nice ' + args.join(' '));
// todo get the actual name of the database written to.
var nice = spawn('nice', args);
nice.stdout.on('data', function(data) {
console.log(data.toString());
});
nice.stderr.on('data', function(data) {
console.warn(data.toString());
});
nice.on('exit', function(code, signal) {
var err = null;
if (code) {
err = new Error('Render failed: '+ i);
err.name = 'ProjectMill';
}
else {
console.log('Notice: rendered ' + destfile);
}
cb(err);
});
});
// If this isn't mbtile, or we don't have meta data to add to it
// skip the remainder of the render steps.
if (data.format != 'mbtiles' || data.MBmeta == undefined) return;
render.push(function(cb, err) {
if (err) return cb(err);
var db = new sqlite3.Database(destfile, sqlite3.OPEN_READWRITE, function(err) {
cb(err, db);
});
});
render.push(function(cb, err, db) {
if (err) return cb(err);
var rows = [];
Object.keys(data.MBmeta).forEach(function(k) {
if (typeof data.MBmeta[k] != 'string') return;
rows.push(function(nextRow, err) {
if (err) return console.warn(err.toString());
var sql = 'REPLACE INTO metadata (name, value) VALUES (?, ?)';
var stmt = db.prepare(sql, function(err) {
nextRow(err, stmt);
});
});
rows.push(function(nextRow, err, stmt) {
if (err) return nextRow(err);
stmt.run(k, data.MBmeta[k], function(err){
if (err) console.warn(err);
stmt.finalize(nextRow);
})
});
});
serial(rows, function(err) {
delete db;
cb(err, db);
});
});
render.push(function(cb, err) {
if (!err) console.log('Notice: added metadata to ' + destfile);
cb(err);
});
});
serial(render, function(err) {
next(triageError(err));
});
});
}
// Upload available mbtiles files.
if (argv.upload) {
var spawn = require('child_process').spawn;
actions.push(function(next, err) {
if (err) return next(err);
var upload = [];
Object.keys(config).forEach(function(i) {
var data = config[i];
if (data.syncAccount && data.syncAccessToken) {
upload.push(function(cb, err) {
err = triageError(err);
if (err) return next(err);
// todo - mbtilesFile name is guess work.
var args = [],
mbtilesFile = path.join(fileDir, 'export', i + '.mbtiles');
// tilemill index.js
args.push(tilemillPath);
// export command
args.push('export');
// datasource
args.push(i);
// file
args.push(mbtilesFile);
// signal upload
args.push('--format=upload');
// OAuth config.
args.push('--syncAccount=' + data.syncAccount);
args.push('--syncAccessToken=' + data.syncAccessToken);
// todo get more output from the child process.
var retries = 0;
var upload = function(callback) {
var retry = false;
console.log('Notice: spawning ' + process.execPath + ' ' + args.join(' '));
var tilemill = spawn(process.execPath, args);
tilemill.stdout.on('data', function(data) {
console.log(data.toString());
});
tilemill.stderr.on('data', function(data) {
var error = data.toString();
console.warn(error);
retry = error.match(/.*S3 is not available.*/) && (++retries < 4);
});
tilemill.on('exit', function(code, signal) {
var err = null;
if (code && retry) {
console.log('Retrying upload in ' + retries + ' second(s) (attempt ' + retries + ')');
return setTimeout(function() {
upload(callback);
}, 1000 * retries);
}
if (code) {
err = new Error('Upload failed: '+ i);
err.name = 'ProjectMill';
}
else {
console.log('Notice: uploaded ' + mbtilesFile + ' to ' + data.syncAccount);
}
callback(err);
});
}
upload(cb);
});
}
});
serial(upload, function(err) {
next(triageError(err));
});
});
}
// Run the main actions.
serial(actions, function(err) {
if (err) console.warn(err.toString());
console.log('Done.');
});