Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 103 lines (97 sloc) 3.558 kb
8572226b »
2012-09-03 Removed all the socket io stuff.
1 #!/usr/bin/env node
2 var fs = require('fs'),
3 path = require('path'),
4 util = require('util'),
5 http = require('http'),
6 exec = require('child_process').exec,
7 dbox = require('dbox'),
8 R = new (require('regex-router'))(),
9 argv = require('optimist').argv,
10 host = argv.host || '127.0.0.1',
11 port = argv.port || 8051,
12 local = argv.local,
13 dropbox = argv.dropbox,
14 tld = argv.tld,
36ba01d6 »
2012-09-03 Relativify the static file refs
15 frame_html = fs.readFileSync(path.join(__dirname, 'frame.html')),
16 favicon_ico = fs.readFileSync(path.join(__dirname, 'favicon.ico')),
8572226b »
2012-09-03 Removed all the socket io stuff.
17 env = process.env;
18
19 env.LANG = 'en_US.UTF-8';
20 env.LC_ALL = 'en_US.UTF-8';
21 Array.prototype.extend = function(l) { return this.push.apply(this, l); };
22
23 // ('root' defaults to sandbox)
24 var app = dbox.app({app_key: argv.appkey, app_secret: argv.appsecret, root: 'dropbox'});
25 var client = app.client({oauth_token: argv.oauthtoken, oauth_token_secret: argv.oauthsecret});
26
27 function syncDirectory(dropbox_path, callback) {
28 // callback signature: (logs)
29 // logs is a list of strings
30 var logs = [];
31 client.metadata(dropbox_path, function(status, item) {
32 var local_path = local + item.path,
33 fileitems = item.contents;
34 logs.push('metadata: ' + status + ', ' + fileitems.length + ' items');
35 if (!fs.existsSync(local_path)) fs.mkdirSync(local_path);
36 // fileitems can be node objects representing filepaths or directories
37 (function next() {
38 var fileitem = fileitems.pop();
39 if (fileitem) {
40 if (fileitem.is_dir) {
41 syncDirectory(fileitem.path, function(err, sublogs) {
42 logs.extend(sublogs);
43 next();
44 });
45 }
46 else {
47 var local_filepath = path.join(local, fileitem.path);
48 fs.stat(local_filepath, function(err, stats) {
49 // if (err) console.error(err);
50 // if the file is missing, err != null
51 console.log('get: ' + fileitem.path);
52 logs.push('get: ' + fileitem.path);
53 if (err || stats.mtime < new Date(fileitem.modified)) {
54 client.get(fileitem.path, function(status, file_contents) {
55 fs.writeFile(local_filepath, file_contents, next);
56 });
57 }
58 else {
59 logs.push('file is already latest version: ' + fileitem.path);
60 next();
61 }
62 });
63 }
64 }
65 else {
66 callback(logs);
67 }
68 })();
69 });
70 }
71
72 R.default = function(m, req, res) {
73 res.string("text/html", frame_html);
74 };
75 R.get(/favicon/, function(m, req, res) {
76 res.string("image/x-icon", favicon_ico);
77 });
78
79 R.post(/(test|live)/, function(m, req, res) {
80 var url = 'http://' + m[1] + '.' + argv.tld + '?t=' + Date.now();
81 syncDirectory(dropbox, function(logs) {
82 console.log("Done syncing", logs.length);
83 logs.extend(['*** Done rendering ***']);
84 exec('jekyll _' + m[1], {cwd: path.join(local, dropbox), env: env}, function(err, stdout, stderr) {
85 logs.extend(["STDOUT: " + stdout, "STDERR: " + stderr]);
86 res.json({success: true, url: url, message: logs.join('\n')});
87 });
88 });
89 });
90
91 http.ServerResponse.prototype.string = function(content_type, body) {
92 this.writeHead(200, {"Content-Type": content_type});
93 this.write(body);
94 this.end();
95 };
96 http.ServerResponse.prototype.json = function(obj) {
97 this.string("application/json", JSON.stringify(obj));
98 };
99 var app = http.createServer(function(req, res) {
100 console.log('URL: ' + req.url);
101 R.route(req, res);
102 }).listen(port, host);
103 console.log('Dropyll server running at ' + host + ':' + port);
Something went wrong with that request. Please try again.