-
Notifications
You must be signed in to change notification settings - Fork 29
/
volofile
194 lines (167 loc) · 7.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
/*jslint node: true, regexp: true */
'use strict';
var crypto = require('crypto'),
fs = require('fs'),
path = require('path'),
buildDir = 'www-built',
pagesDir = 'www-ghpages';
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',
//Uses dynamic loading via require.js instead of building
//all the modules in with almond.
'dynamic': 'dynamic'
},
run: function (d, v, namedArgs) {
var q = v.require('q');
(q.fcall || q.call)(function () {
//Remove the old dir
v.rm(buildDir);
if (!namedArgs.dynamic) {
//Copy the directory for output.
v.copyDir('www', buildDir);
//Remove the js dir from the built area, will be
//replaced by an optimized app.js
v.rm(buildDir + '/js');
//Do the CSS optimization
return v.spawn('node', ['tools/r.js', '-o',
'cssIn=www/css/app.css',
'out=' + buildDir + '/css/app.css'], {
useConsole: !namedArgs.quiet
});
}
return undefined;
})
.then(function () {
//JS go time
var optimize = namedArgs.optimize || 'uglify';
if (namedArgs.dynamic) {
//Still use require.js to load the app.js file.
return v.spawn('node', ['tools/r.js', '-o',
'appDir=www',
'baseUrl=js/lib',
'paths.app=../app',
'name=app',
'dir=' + buildDir,
'optimize=' + optimize], {
useConsole: !namedArgs.quiet
});
} else {
//The all-in-one option.
return v.spawn('node', ['tools/r.js', '-o',
'baseUrl=www/js/lib',
'paths.app=../app',
'paths.almond=../../../tools/almond',
'name=almond',
'include=app',
'out=' + buildDir + '/js/app.js',
'optimize=' + optimize], {
useConsole: !namedArgs.quiet
});
}
})
.then(function (buildOutput) {
//Remove all the CSS except for the app.css, since it
//inlines all the other ones.
v.getFilteredFileList(buildDir + '/css').forEach(function (path) {
if (!/app\.css$/.test(path)) {
v.rm(path);
}
});
//If almond is in use, it is built into app.js, so need
//to update the script tag to just load app.js instead.
if (!namedArgs.dynamic) {
var indexName = buildDir + '/index.html',
contents = v.read(indexName),
scriptRegExp = /(<script[^>]+data-main="[^"]+"[^>]+)(src="[^"]+")([^>]*>\s*<\/script>)/;
contents = contents.replace(scriptRegExp,
function (match, pre, script, post) {
return pre + 'src="js/app.js"' + post;
});
v.write(indexName, contents);
}
return buildOutput;
})
.then(function (buildOutput) {
d.resolve(buildOutput);
}, d.reject);
}
},
//Runs the build, and generates the appcache manifest
appcache: require('volo-appcache')({
depends: ['build'],
dir: buildDir
}),
//Deploys the code to github pages.
ghdeploy: require('volo-ghdeploy')(buildDir, pagesDir),
//Runs less on the .less files in tools/less to generate the CSS files.
less: {
summary: 'Runs less on the .less files in tools/less to generate the CSS files.',
run: [
'node node_modules/less/bin/lessc tools/less/bootstrap.less www/css/bootstrap.css'
]
},
//Run as the result of first setting up this project via a
//"volo create" call. Gets the twitter bootstrap code, and
//jQuery.
onCreate: {
run: function (d, v, namedArgs, appName) {
var tempName = 'tempbootstrap',
q = v.require('q'),
amdify = v.require('./commands/amdify'),
jsNameRegExp = /(\w*)\.js$/;
//First replace the name in the package.json with name from
//creation.
v.write('package.json', v.read('package.json').replace(/"name": "[^"]+"/, '"name": "' + appName + '"'));
v.command('add', 'jquery/jquery', 'jquery')
.then(function () {
//Grab the twitter bootstrap and jQuery
return v.command('create', tempName, 'twbs/bootstrap');
})
.then(function () {
//Move the JS to the right location.
var jsFiles = v.getFilteredFileList(tempName + '/js', /\.js$/, /js[\/\\]tests[\/\\]/),
promises = [];
jsFiles.forEach(function (file) {
//Pull off the name part from bootstrap-name.js pattern.
var match = jsNameRegExp.exec(file),
name,
destName,
damd;
if (!match) {
return;
}
name = jsNameRegExp.exec(file)[1];
destName = 'www/js/lib/bootstrap/' + name + '.js';
damd = q.defer();
v.copyFile(file, destName);
//Convert the file to AMD style
amdify.run.apply(amdify, [damd, v, {
depends: 'jquery'
}, destName]);
promises.push(damd);
});
//Wait for all the amdify calls to finish.
return q.all(promises);
})
.then(function () {
//Copy the images over.
v.copyDir(tempName + '/img', 'www/img');
//Copy the fonts over.
v.copyDir(tempName + '/fonts', 'www/fonts');
//Copy the less files.
v.copyDir(tempName + '/less', 'tools/less');
//Compile the CSS.
return v.command('less').then(function () {
v.rm(tempName);
});
})
.then(d.resolve, d.reject);
}
}
};