/
Project.server.bones
73 lines (64 loc) · 2.48 KB
/
Project.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
var fs = require('fs'),
path = require('path');
/**
* The only method we need to override for on the server is `sync` which is
* called to load the resource. By default Backbone models will attempt to
* retrieve their state from a URI returned from their 'URL' method. Clearly
* that won't work on the server, as we're trying to power that very same
* URI. Here we do the hard work!
*/
models.Project.prototype.sync = function(method, model, options) {
// Project data can only be read, so return an error it the client is
// trying to do anything else.
if (method != 'read') return options.error('Unsupported method');
var projectDir = path.dirname(require.resolve('bones')) + '/node_modules/' + model.id,
resp = {id: model.id};
var fetchPackage = function(callback) {
fs.readFile(projectDir +'/package.json', 'utf8', function(err, data) {
if (err) return callback('Could not retrieve project information.');
data = JSON.parse(data);
resp = _.extend(resp, data);
callback();
});
};
var fetchReadme = function(callback) {
// Setup the regex we'll use for detecting README files.
var re = /^readme(|\.\w*)$/i;
// Scan the project directory, and send a readme back to the client.
fs.readdir(projectDir, function(err, files) {
if (err) return callback('Project not found.');
for (var i = 0; i < files.length; i++) {
var match = re.exec(files[i]);
if (match) {
fs.readFile(projectDir +'/'+ match.input, 'utf8', function(err, data) {
if (err) return callback('Could not retrieve project information.');
// 80 chars folks...
data = wrap(data);
resp.readme = data;
callback();
});
break;
}
}
});
}
fetchPackage(function(err) {
if (err) return options.error(err);
fetchReadme(function(err) {
if (err) return options.error(err);
options.success(resp);
});
});
};
var wrap = function(str) {
var lines = [];
_(str.split(/\n/)).each(function(v) {
if (v.length > 80) {
var parts = v.match(/.{80}|.+$/g);
lines.push(parts.join('\n'));
} else {
lines.push(v);
}
});
return lines.join('\n');
};