/
index.js
144 lines (135 loc) · 4.38 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
// Generated by CoffeeScript 1.3.3
var clients, formatters, fs, options, packAssets, path, systemAssets, templateEngine;
fs = require('fs');
path = require('path');
systemAssets = require('./system');
templateEngine = require('./template_engine');
formatters = require('./formatters');
packAssets = process.env['SS_PACK'];
options = {
packedAssets: packAssets || false,
liveReload: ['code', 'css', 'static', 'templates', 'views'],
dirs: {
code: '/client/code',
css: '/client/css',
"static": '/client/static',
assets: '/client/static/assets',
templates: '/client/templates',
views: '/client/views',
workers: '/client/workers'
}
};
clients = {};
module.exports = function(ss, router) {
var determineLatestId, http;
determineLatestId = function(client) {
var files, id, latestId;
try {
files = fs.readdirSync(path.join(ss.root, options.dirs.assets, client.name));
latestId = files.sort().pop();
id = latestId.split('.')[0];
if (id.length !== 13) {
throw 'Invalid Client ID length';
}
return id;
} catch (e) {
return false;
}
};
http = require('./http')(ss, clients, options);
systemAssets.load();
return {
formatters: formatters.init(ss.root),
templateEngine: templateEngine.init(ss, options),
assets: systemAssets,
options: options,
set: function(newOption) {
var k, v, x, y, _results;
if (typeof newOption !== 'object') {
throw new Error('ss.client.set() takes an object e.g. {liveReload: false}');
}
_results = [];
for (k in newOption) {
v = newOption[k];
if (v instanceof Object) {
_results.push((function() {
var _results1;
_results1 = [];
for (x in v) {
y = v[x];
_results1.push(options[k][x] = y);
}
return _results1;
})());
} else {
_results.push(options[k] = v);
}
}
return _results;
},
packAssets: function(opts) {
if (opts && typeof opts !== 'object') {
throw new Error('Options passed to ss.client.packAssets() must be an object');
}
return options.packedAssets = opts || true;
},
define: function(name, paths) {
var _this = this;
if (clients[name] != null) {
throw new Error("Client name '" + name + "' has already been defined");
}
if (typeof paths.view !== 'string') {
throw new Error("You may only define one HTML view per single-page client. Please pass a filename as a string, not an Array");
}
if (paths.view.indexOf('.') === -1) {
throw new Error("The '" + paths.view + "' view must have a valid HTML extension (such as .html or .jade)");
}
if (paths.templates) {
paths.tmpl = paths.templates;
}
['css', 'code', 'tmpl'].forEach(function(assetType) {
if (!(paths[assetType] instanceof Array)) {
return paths[assetType] = [paths[assetType]];
}
});
return clients[name] = {
id: Number(Date.now()),
name: name,
paths: paths
};
},
load: function() {
var client, id, name, pack;
ss.client.formatters = formatters.load();
systemAssets.send('code', 'init', "require('/entry');");
if (options.packedAssets) {
if (!packAssets) {
ss.log('i'.green, "Attempting to find pre-packed assets... (force repack with SS_PACK=1)".grey);
for (name in clients) {
client = clients[name];
if (id = options.packedAssets.id || determineLatestId(client)) {
client.id = id;
ss.log('✓'.green, ("Serving client '" + client.name + "' from pre-packed assets ID " + client.id).grey);
} else {
ss.log('!'.red, ("Unable to find pre-packed assets for '" + client.name + "'. All assets will be repacked").grey);
packAssets = true;
}
}
}
if (packAssets) {
pack = require('./pack');
for (name in clients) {
client = clients[name];
pack(ss, client, options);
}
}
} else {
require('./serve/dev')(ss, router, options);
if (options.liveReload) {
require('./live_reload')(ss, options);
}
}
return require('./serve/ondemand')(ss, router, options);
}
};
};