forked from fabric8-ui/fabric8-planner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
248 lines (208 loc) · 6.75 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* Main build file for fabric8-planner.
* ---
* Each main task has parametric handlers to achieve different result.
* Check out the sections for task variants & accepted task arguments.
*/
// Require primitives
var del = require('del')
, path = require('path')
, argv = require('yargs').argv
, proc = require('child_process')
;
// Require gulp & its extension modules
var gulp = require('gulp')
, ngc = require('gulp-ngc')
, less = require('gulp-less')
, util = require('gulp-util')
, lesshint = require('gulp-lesshint')
, srcmaps = require('gulp-sourcemaps')
, replace = require('gulp-string-replace')
, tslint = require('gulp-tslint')
;
// Requirements with special treatments
var KarmaServer = require('karma').Server
, LessAutoprefix = require('less-plugin-autoprefix')
, autoprefix = new LessAutoprefix({ browsers: ['last 2 versions'] })
;
// Not sure if var or const
var appSrc = 'src';
var distPath = 'dist';
/*
* Utility functions
*/
// Global namespace to contain the reusable utility routines
let mach = {};
// Serialized typescript compile and post-compile steps
mach.transpileTS = function () {
return (gulp.series(function () {
return ngc('tsconfig.json');
}, function () {
// FIXME: why do we need that?
// Replace templateURL/styleURL with require statements in js.
return gulp.src(['dist/app/**/*.js'])
.pipe(replace(/templateUrl:\s/g, "template: require("))
.pipe(replace(/\.html',{0,1}/g, ".html'),"))
.pipe(replace(/styleUrls: \[/g, "styles: [require("))
.pipe(replace(/\.less']/g, ".css').toString()]"))
.pipe(gulp.dest(function (file) {
return file.base; // because of Angular 2's encapsulation, it's natural to save the css where the less-file was
}));
}))();
}
// Copy files to the distPath
mach.copyToDist = function (srcArr) {
return gulp.src(srcArr)
.pipe(gulp.dest(function (file) {
// Save directly to dist; @TODO: rethink the path evaluation strategy
return distPath + file.base.slice(__dirname.length + 'src/'.length);
}));
}
//TSLint
mach.tslint = function(src) {
return gulp.src(src)
.pipe(tslint({
formatter: 'verbose',
configuration: 'tslint.json'
}))
.pipe(tslint.report())
};
// Transpile given LESS source(s) to CSS, storing results to distPath.
mach.transpileLESS = function (src, debug) {
return gulp.src(src)
.pipe(lesshint({
configPath: './.lesshintrc' // Options
}))
.pipe(less({
plugins: [autoprefix]
}))
.pipe(lesshint.reporter()) // Leave empty to use the default, "stylish"
.pipe(lesshint.failOnError()) // Use this to fail the task on lint errors
.pipe(srcmaps.init())
.pipe(less())
.pipe(srcmaps.write())
.pipe(gulp.dest(function (file) {
return distPath + file.base.slice(__dirname.length + 'src/'.length);
}));
}
/*
* Task declarations
*/
// Build
gulp.task('build', function (done) {
// app (default)
mach.tslint(appSrc + '/app/**/*.ts'); // Report all the linter errors
mach.transpileTS(); // Transpile *.ts to *.js; _then_ post-process require statements to load templates
mach.transpileLESS(appSrc + '/**/*.less'); // Transpile and minify less, storing results in distPath.
mach.copyToDist(['src/**/*.html']); // Copy template html files to distPath
gulp.src(['LICENSE', 'README.adoc', 'package.json']).pipe(gulp.dest(distPath)); // Copy static assets to distPath
// image
// tarball
// validate
// watch
if (argv.watch) {
gulp.watch([
appSrc + '/app/**/*.ts', '!' + appSrc + '/app/**/*.spec.ts',
appSrc + '/planner.module.ts'
]).on('change', function (e) {
util.log(util.colors.cyan(e) + ' has been changed. Compiling TypeScript.');
mach.transpileTS();
});
gulp.watch(appSrc + '/app/**/*.less').on('change', function (e) {
util.log(util.colors.cyan(e) + ' has been changed. Compiling LESS.');
mach.transpileLESS(e);
});
gulp.watch(appSrc + '/app/**/*.html').on('change', function (e) {
util.log(util.colors.cyan(e) + ' has been changed. Compiling HTML.');
mach.copyToDist(e);
});
util.log('Now run');
util.log('');
util.log(util.colors.red(' npm link', path.resolve(distPath), ' --production'));
util.log('');
util.log('in the npm module you want to link this one to');
}
done();
});
// Clean
gulp.task('clean', function (done) {
// all (default): set flags to validate following conditional cleanups
if (
!argv.cache &&
!argv.config &&
!argv.dist &&
!argv.images &&
!argv.modules &&
!argv.temp) {
// if none of the known sub-task parameters for `clean` was provided
// i.e. only `gulp clean` was called, then set default --all flag ON
argv.all = true;
}
if (argv.all) {
// Exclusively set all subroutine parameters ON for `gulp clean --all`
argv.cache = argv.config = argv.dist = argv.images = argv.modules = argv.temp = true;
}
// cache
if (argv.cache) proc.exec('npm cache clean');
// config
// if (argv.config) { subroutine to clean config - not yet needed }
// dist
if (argv.dist) del([distPath]);
// images
if (argv.images) {
// Get ID of the images having 'fabric8-planner' in its name
proc.exec('sudo docker ps -aq --filter "name=fabric8-planner"', function (e, containerID) {
if (e) {
console.log(e);
return;
}
// @TODO: wrap this in a try-catch block to avoid unexpected behavior
proc.exec('sudo docker stop ' + containerID);
proc.exec('sudo docker rm ' + containerID);
// Container has been killed, safe to remove image(s) with 'fabric8-planner-*' as part of their ref
proc.exec('sudo docker images -aq --filter "reference=fabric8-planner-*"', function (e, imageID) {
if (e) {
console.log(e);
return;
}
// @TODO: wrap this in a try-catch block to avoid unexpected behavior
proc.exec('sudo docker rmi ' + imageID);
});
});
}
// modules
if (argv.modules) del(['node_modules']);
// temp
if (argv.temp) del(['tmp', 'coverage', 'typings', '.sass-cache']);
done();
});
// // Release
// gulp.task('release', function (done) {
// gulp.src(['.git/**/*']).pipe(gulp.dest('dist/.git'));
// proc.exec('$(npm bin)/semantic-release', function(error, stdout) {
// console.log("error: ", error);
// console.log(stdout);
// });
// done();
// });
// Test
gulp.task('tests', function (done) {
// unit
if (argv.unit) {
new KarmaServer({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, function (code) {
process.exit(code);
}).start();
}
// func
if (argv.func) {
// subroutine to run functional tests
}
// smok
if (argv.smok) {
// subroutine to run smoke tests
}
done();
});