-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgulpfile.js
173 lines (164 loc) · 5.29 KB
/
gulpfile.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
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
/**
* Equivalent to makefile, with gulp.
* The only requirement is to have gulp globally installed `npm install -g gulp`,
* and to have retrieved the npm dependencies with `npm install`
*
* Available tasks:
* clean - removed hyperion/lib folder
* build - compiles coffee-script from hyperion/src to hyperion/lib
* cleanBuild - clean and then build tasks
* test - runs all tests with mocha (configuration in test/mocha.opts)
* deploy - compile, minify and make production ready version of rheia administration client
* watch (default) - clean, compiles coffee-script, and use watcher to recompile on the fly
*/
var spawn = require('child_process').spawn;
var gulp = require('gulp');
var gutil = require('gulp-util');
var del = require('del');
var stylus = require('gulp-stylus');
var coffee = require('gulp-coffee');
var plumber = require('gulp-plumber');
var requirejs = require('requirejs');
var async = require('async');
var fs = require('fs-extra');
var join = require('path').join;
var sources = 'hyperion/src/**/*.coffee';
var dest = 'hyperion/lib';
var isWin = process.platform.match(/^win/) != null;
gulp.task('default', ['watch']);
// remove hyperion/lib folder
gulp.task('clean', function(){
return del([dest]);
});
function build() {
return gulp.src(sources)
.pipe(plumber({
errorHandler: function(err) {
gutil.log(err);
gutil.beep();
}
}))
.pipe(coffee({bare: true}))
.pipe(gulp.dest(dest));
}
// Build hyperion coffee sources
gulp.task('build', function(){
return build()
});
gulp.task('cleanBuild', ['clean'], function(){
return build()
});
// Run all tests
gulp.task('test', ['cleanBuild'], function(callback){
var cmd = "mocha"
if (isWin) {
cmd += '.cmd';
}
spawn(cmd, [], {
stdio:'inherit',
env: Object.assign({}, process.env, {NODE_ENV:"test"})
}).on('exit', function(code) {
if (callback) {
callback(code === 0 ? null : code);
} else {
process.exit(code);
}
});
});
// Clean, build, and then watch for coffee files changes (default)
gulp.task('watch', ['cleanBuild'], function(){
return gulp.watch(sources, ['build']);
});
// Compile Rheia coffee and stylus sources, minify requirejs files
var deployTarget = 'rheia-min';
var deployTemp = 'rheia-build';
// clean previous build output
gulp.task('deploy-clean', function() {
return del([deployTarget]);
});
// copy source into temporary folder
gulp.task('deploy-copySource', ['deploy-clean'], function() {
return gulp.src('rheia/**', {base:"./rheia/"})
.pipe(gulp.dest(deployTarget));
});
// compile coffee scripts
gulp.task('deploy-compileCoffee', ['deploy-copySource'], function() {
return gulp.src(deployTarget+'/**/*.coffee')
.pipe(coffee({bare: true})
.on('error', gutil.log))
.pipe(gulp.dest(deployTarget));
});
// compile stylus style sheet
gulp.task('deploy-compileStylus', ['deploy-copySource'], function() {
return gulp.src(deployTarget+'/style/rheia.styl')
.pipe(stylus({set:['compress']})
.on('error', gutil.log))
.pipe(gulp.dest(deployTarget+'/style'));
});
// minify with requireJS optimizer
gulp.task('deploy-minify', ['deploy-compileCoffee', 'deploy-compileStylus'], function(end) {
var config = {
appDir: deployTarget,
dir: deployTemp,
baseUrl: './src/',
mainConfigFile: deployTarget + '/src/Router.js',
optimizeCss: 'standard',
preserveLicenseComments: false,
locale: null,
optimize: 'uglify2',
useStrict: true,
modules: [{
name: 'Router'
}]
};
requirejs.optimize(config, function(err) {
end(err instanceof Error ? err : null);
});
});
// deploy task
gulp.task('deploy', ['deploy-minify'], function(end) {
// move into a timestamp folder and replace links to make page cacheable
var timestamp = new Date().getTime().toString();
var timestamped = join(deployTarget, timestamp);
var index = join(deployTarget, 'index.html');
fs.remove(deployTarget, function(err) {
if (err) {
return end(err);
}
fs.mkdirs(deployTarget, function(err) {
if (err) {
return end(err);
}
fs.rename(deployTemp, timestamped, function(err) {
if (err) {
return end(err);
}
fs.rename(join(timestamped, 'index.html'), index, function(err) {
if (err) {
return end(err);
}
// replaces links from main html file
fs.readFile(index, 'utf8', function(err, content) {
if (err) {
return end(err);
}
specs = [{
pattern: /<\s*script([^>]*)data-main\s*=\s*(["'])(.*(?=\2))\2([^>]*)src\s*=\s*(["'])(.*(?=\5))\5/gi,
replace: '<script$1data-main="' + timestamp + '/$3"$4src="' + timestamp + '/$6"'
},{
pattern: /<\s*script([^>]*)src\s*=\s*(["'])(.*(?=\2))\2([^>]*)data-main\s*=\s*(["'])(.*(?=\5))\5/gi,
replace: '<script$1src="' + timestamp + '/$3"$4data-main="' + timestamp + '/$6"'
},{
pattern: /<\s*link([^>]*)href\s*=\s*(["'])(.*(?=\2))\2/gi,
replace: '<link$1href="' + timestamp + '/$3"'
}];
specs.forEach(function(spec) {
content = content.replace(spec.pattern, spec.replace);
});
fs.writeFile(index, content, end);
});
});
});
});
});
});