-
Notifications
You must be signed in to change notification settings - Fork 43
/
gulpfile.js
136 lines (123 loc) · 4.55 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
'use strict';
// require all needed modules
var gulp = require('gulp');
var ts = require('gulp-typescript');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var del = require('del');
var merge = require('merge2');
var sourcemaps = require('gulp-sourcemaps');
var path = require('path');
var watch = require('gulp-watch');
var karma = require('karma').Server;
var tslint = require('gulp-tslint');
module.exports = function (config) {
// default project config
var tsProject = ts.createProject({
module: 'commonjs',
target: 'es5',
noImplicitAny: false,
sourceMap: false,
declaration: true,
noExternalResolve: false
});
// Clean out build directories
gulp.task('clean', function () {
del.sync([
path.join(__dirname, 'build', config.appName),
path.join(__dirname, 'appTypings', config.appName),
path.join(__dirname, 'compiled', config.appName)
], {
force: true
});
});
// compile TypeScript
gulp.task('ts', ['lint', 'clean'], function () {
var tsResult = gulp
.src(path.join(config.path, '**/*.ts'))
.pipe(sourcemaps.init()) // with Sourcemaps
.pipe(ts(tsProject));
return merge([
tsResult.dts // write automatically generated typing files
.pipe(sourcemaps.write())
.pipe(gulp.dest(path.join(__dirname, 'appTypings', config.appName))),
tsResult.js // write compiled JavaScript files
.pipe(sourcemaps.write())
.pipe(gulp.dest(path.join(__dirname, 'build', config.appName)))
]);
});
// prepare files for browser with Browserify
gulp.task('js', ['ts'], function () {
return browserify({
entries: path.join(__dirname, 'build', config.appName, 'main.js'),
debug: true
})
// ignore required module (for libraries which you include yourself - config in package.json)
.transform('browserify-shim')
.bundle()
.on('error', function (e) {
console.log(e.toString());
this.emit('end');
})
// save it as "{{applicationName}}Bundle.js"
.pipe(source(path.join(config.appName + 'Bundle.js')))
// in the application's "compiled" folder
.pipe(gulp.dest(path.join(__dirname, 'compiled', config.appName)));
});
// run unit tests
gulp.task('test', ['js'], function (done) {
// files needed for testing
var files = [
// include needed libraries
path.join(__dirname, 'node_modules/angular/angular.js'),
path.join(__dirname, 'node_modules/angular-mocks/angular-mocks.js')
];
// unless we're testing the common vertical, include it as an extra
if (config.appName !== 'common') {
files = files.concat([path.join(__dirname, 'compiled/common/commonBundle.js')])
}
// application, the actual tests
files = files.concat([
path.join(__dirname, 'compiled', config.appName, '**/*.js'), // include the application
path.join(__dirname, 'build', config.appName, 'tests/**/*.spec.js') // test files
]);
// start Karma Test Runner
new karma({
basePath: __dirname,
browsers: ['PhantomJS'],
frameworks: ['jasmine', 'browserify'],
files: files,
// process tests that were transpiled for the CommonJS module system
preprocessors: {
'build/**/tests/**/*.spec.js': ['browserify']
},
// mock away Angular as required
browserify: {
debug: true,
transform: ['browserify-shim']
},
singleRun: true,
autoWatch: false
}, done).start();
});
// lint TypeScript code
gulp.task('lint', function () {
return gulp
.src(path.join(config.path, '**/*.ts'))
.pipe(tslint())
.pipe(tslint.report('verbose'));
});
// watch for file changes
gulp.task('watch', ['test'], function () {
var watchPath = [
path.join(config.path, '**/*.ts')
];
if (config.appName !== 'common') {
watchPath = watchPath.concat(path.join(__dirname, 'compiled/common/**/*.js'));
}
return watch(watchPath, function () {
gulp.start('test');
});
});
gulp.task('default', ['test']);
};