-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp-sugar
executable file
·114 lines (92 loc) · 3.18 KB
/
app-sugar
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
#! /usr/bin/env node
/*
Bare-bones app to make it easy to update a CouchApp.
Copyright 2010 by Tim Smith <[email protected]>
Released under the BSD license; see ./LICENSE.markdown
*/
// To keep this as uncluttered as possible, several things are hard-coded
// into this application. It can be extended in a lot of ways, with a bit of
// node.js fun.
var appPath = './bugclub/clubhouse';
var dbUri = 'http://tim:[email protected]/bugclub';
// Non-default modules, available from, among other places,
// NPM, a Node Package Manager: http://howtonode.org/introduction-to-npm
var mime = require('mime');
var step = require('step');
// Default node.js modules
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var sys = require('sys');
// Local modules
var util = require('./util');
var app = require(appPath);
var ddocUri = dbUri + '/' + app.ddoc._id;
function loadDirectory(dir, storeFunc) {
// Read all files from the directory DIR that is in
// the same directory as the application definition
var sourceDir = path.join(path.dirname(appPath), dir);
util.findFilesSync(sourceDir).forEach(function (f) {
var contents = fs.readFileSync(path.join(sourceDir, f));
storeFunc(f, contents);
util.log('Loaded ' + path.join(dir, f));
});
}
function loadAppAttachments(ddoc) {
util.setDefault(ddoc, { '_attachments': {} });
loadDirectory('attachments', function (f, contents) {
ddoc._attachments[f] = {
content_type: mime.lookup(f),
data: contents.toString('base64')
};
});
}
function loadAppFiles(ddoc, dir, stripJsExtension) {
loadDirectory(dir, function (f, contents) {
var obj = ddoc;
var term;
// CouchDB expects modules without the '.js' extension
if (stripJsExtension) { f = f.replace(/\.[^\.]*/, ''); }
// Convert nested directories into nested objects; e.g.
// templates/foo/bar.txt => ddoc.templates.foo['bar.txt']
f = [dir].concat(f.split('/'));
//sys.debug(sys.inspect(f));
while (f.length > 1) {
term = f.shift();
if (!obj[term]) { obj[term] = {}; }
obj = obj[term];
}
term = f.shift();
obj[term] = contents.toString('utf8');
});
}
loadAppAttachments(app.ddoc);
loadAppFiles(app.ddoc, 'modules', true);
loadAppFiles(app.ddoc, 'templates', false);
//sys.debug(sys.inspect(app.ddoc));
step(
function getCurrentDdoc() {
util.jsonRequest({ uri: ddocUri, expect: [200, 404] }, this);
},
function putAppDdoc(err, resp, body) {
assert.ifError(err);
if (body._rev) {
app.ddoc._rev = body._rev;
}
else {
// Warn about this, in case it was a typo
sys.debug('NOTICE: Starting a new design doc at "' + ddocUri + '"');
}
util.jsonRequest({
uri: ddocUri,
method: 'PUT',
body: app.ddoc
}, this);
},
function finish(err, resp, body) {
assert.ifError(err);
util.log(JSON.stringify({ ok: true, rev: body.rev }));
process.exit(0);
}
);
// vim:et:sts=4:sw=4:ft=javascript: