This repository has been archived by the owner on Nov 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volofile
198 lines (169 loc) · 6.52 KB
/
volofile
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*jslint regexp: true */
/*global define, console, process */
var crypto = require('crypto');
var fs = require('fs');
var path = require('path');
var buildDir = 'www-built';
var pagesDir = 'www-ghpages';
try {
var ghdeploy = require('volo-ghdeploy')('www-built', 'www-ghdeploy');
}
catch(e) {
console.log("You don't have the volo-ghdeploy command installed.\n" +
'You must install this first:\n\n' +
'npm install -g volo-ghdeploy');
process.exit(1);
}
module.exports = {
//Builds the JS and CSS into one file each. If you want to do
//dynamic loading of scripts, pass -dynamic to the build, and
//require.js will be used to load scripts.
build: {
flags: {
//Does not print the build output.
'q': 'quiet'
},
depends: ['less'],
run: 'node tools/r.js -o tools/build.js'
},
//Generates an SHA1 digest that represents the contents of the
//a directory. Call it like so: "volo digest dir=path/to/directory"
digest: {
validate: function (namedArgs) {
var dir = namedArgs.dir;
if (!dir) {
return new Error('Please specify a target directory for ' +
'the digest');
}
if (!path.existsSync(dir)) {
return new Error('Target directory for digest does ' +
'not exist: ' + dir);
}
return undefined;
},
run: function (d, v, namedArgs) {
var q = v.require('q');
var dir = namedArgs.dir,
files = v.getFilteredFileList(dir),
digests = [],
i = 0;
function getDigest(fileName) {
var shaSum = crypto.createHash('sha1'),
d = q.defer(),
stream = fs.ReadStream(fileName);
stream.on('data', function(data) {
shaSum.update(data);
});
stream.on('end', function() {
d.resolve(shaSum.digest('base64'));
});
return d.promise;
}
function digestFile(fileName) {
getDigest(fileName).then(function (digest) {
var shaSum;
digests[i] = digest;
i += 1;
if (i < files.length) {
digestFile(files[i]);
} else {
//All done, now generate the final digest,
//using the combination of the other digests
shaSum = crypto.createHash('sha1');
shaSum.update(digests.join(','));
d.resolve(shaSum.digest('base64'));
}
});
}
digestFile(files[0]);
}
},
//Compile all the less files into css
less: function(d, v, namedArgs) {
var q = v.require('q');
var files = v.getFilteredFileList('www/css');
return q.all([
files.map(function (path) {
if (/\.less$/.test(path)) {
var dest = path.replace(/\.less$/, '.css');
return v.exec(['node tools/oneless.js ' + path + ' > ' + dest]);
}
})
])
.then(function() {
d.resolve();
});
},
ghdeploy: ghdeploy,
//Runs less on the .less files in tools/less to generate the CSS files.
bootstrap_less: function (d, v, namedArgs) {
q.all([
v.exec('node tools/oneless.js tools/less/bootstrap.less > www/css/bootstrap.css'),
v.exec('node tools/oneless.js tools/less/responsive.less > www/css/bootstrap-responsive.css')
])
.then(function () {
d.resolve();
})
.fail(d.reject);
},
appcache: function (d, v, namedArgs) {
var q = v.require('q');
var hasBuilt = v.exists(buildDir);
v.command('build')
.then(function () {
var manifest = v.read('tools/manifest.appcache'),
master = v.read(buildDir + '/index.html'),
appFiles;
appFiles = v.getFilteredFileList(buildDir);
appFiles = appFiles.map(function (file) {
var start = file.indexOf('/' + buildDir + '/');
start = (start !== -1) ? (start + 11) : 0;
return file.substr(start, file.length);
});
master = master
.replace(/<html\s?/, '<html manifest="manifest.appcache" ')
.replace(/manifest\.appcache"\s>/, 'manifest.appcache">');
v.write(buildDir + '/index.html', master);
return v.command('digest', 'dir=' + buildDir)
.then(function (stamp) {
manifest = v.template(manifest, {
files : appFiles.join('\n'),
stamp : stamp
});
v.write(buildDir + '/manifest.appcache', manifest);
});
})
.then(function () {
//Inform the user of the right mime type, but only do it if
//there was not a previous build done.
d.resolve(hasBuilt ? '': 'Be sure to set the mime type for ' +
'.appcache files to be: text/cache-manifest');
})
.fail(d.reject);
},
serve: function(d, v, namedArgs) {
try {
var connect = require('connect');
}
catch(e) {
console.log('To use the `serve` command, you must ' +
'install the connect module:\n\n' +
'npm install connect');
return;
}
var lessMiddleware = require('less-middleware');
var port = 8008;
var base = path.join(process.cwd(), namedArgs.base || 'www');
var middleware = [
lessMiddleware({ src: base }),
connect.static(base),
connect.directory(base),
];
connect.logger.format("OpenWebApp",
"[D] server :method :url :status " +
":res[content-length] - :response-time ms");
middleware.unshift(connect.logger("OpenWebApp"));
console.log("starting web server on port " + port);
connect.apply(null, middleware).listen(port);
}
};