-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
120 lines (98 loc) · 3.25 KB
/
build.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
// Build a couchapp from the files in the src folder
var fs = require('fs'),
path = require('path'),
walk = require('walk'),
mime = require('mime');
// Start from a minimal design document
var ddoc = {
_id: '_design/psynteract',
couchapp: {
name: 'Psynteract',
description: 'Interactive experiments'
}
};
// Add attachments
var process_attachments = function() {
return new Promise(function(resolve, reject) {
// Define a handler that will add the file contents
// to the ddoc as base64-encoded attachments
var fileHandler = function(root, fileStats, next) {
var filename_full = root + '/' + fileStats.name;
var filename_attach = filename_full.substr(17);
// Ignore those pesky .DS_Store files
if (path.parse(filename_full).base != '.DS_Store') {
fs.readFile(filename_full, function(err, data) {
if (err) throw err;
o = {
'content_type': mime.lookup(filename_full),
'data': data.toString('base64')
}
ddoc['_attachments'][filename_attach] = o;
});
}
next();
};
ddoc['_attachments'] = {};
// Add files in the repository
var walker = walk.walk('src/_attachments');
walker.on('file', fileHandler);
walker.on('end', resolve);
});
}
// Create a nested object structure
var nest = function(o, path, value) {
var current_path = o;
path.forEach(function(segment, i) {
if (i < path.length - 1) {
current_path = current_path[segment] = current_path[segment] || {};
} else {
current_path[segment] = value;
}
});
return o;
};
// Add design functions: views, shows, filters, etc.
var process_functions = function() {
return new Promise(function(resolve, reject) {
var function_dirs = ['filters', 'lists', 'shows', 'updates', 'views'];
// For these directories, add any included files to the
// design document as strings
var fileHandler = function(root, fileStats, next) {
var filename_full = root + '/' + fileStats.name;
var attach_path = filename_full.substr(4).split(path.sep);
// Remove the extension from the path
attach_path[attach_path.length-1] = path.parse(filename_full).name
if (path.parse(filename_full).base != '.DS_Store') {
// Add the contents of the file to the ddoc as plain text
fs.readFile(filename_full, function(err, data) {
if (err) throw err;
// Attach the document to the design doc
nest(ddoc, attach_path, data.toString('utf-8'));
});
}
// Continue
next();
}
// Apply the handler specified above to the respective
// directories.
var p = function_dirs.map(function(d) {
ddoc[d] = {}
return new Promise(function(resolve, reject) {
var walker = walk.walk('src/' + d)
walker.on('file', fileHandler);
walker.on('end', resolve);
});
})
// Resolve the promise if all directories have been
// processed
Promise.all(p).then(resolve)
})
}
process_attachments()
.then(process_functions)
.then(function(x) {
// Write the couchapp.json to the file system
fs.writeFile('backend.json', JSON.stringify(ddoc, null, 2), function(err, written, string) {
if (err) throw err;
});
});