forked from ng-bootstrap/ng-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
285 lines (231 loc) · 9.67 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
var gulp = require('gulp');
var gutil = require('gulp-util');
var ddescribeIit = require('gulp-ddescribe-iit');
var shell = require('gulp-shell');
var ghPages = require('gulp-gh-pages');
var gulpFile = require('gulp-file');
var del = require('del');
var clangFormat = require('clang-format');
var gulpFormat = require('gulp-clang-format');
var runSequence = require('run-sequence');
var tslint = require('gulp-tslint');
var webpack = require('webpack');
var typescript = require('typescript');
var exec = require('child_process').exec;
var path = require('path');
var os = require('os');
var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
var PATHS = {
src: 'src/**/*.ts',
srcIndex: 'src/index.ts',
specs: 'src/**/*.spec.ts',
testHelpers: 'src/test/**/*.ts',
demo: 'demo/**/*.ts',
demoDist: 'demo/dist/**/*',
typings: 'typings/index.d.ts',
jasmineTypings: 'typings/globals/jasmine/index.d.ts',
demoApiDocs: 'demo/src',
coverageJson: 'coverage/json/coverage-final.json'
};
function platformPath(path) {
return /^win/.test(os.platform()) ? `${path}.cmd` : path;
}
function webpackCallBack(taskName, gulpDone) {
return function(err, stats) {
if (err) throw new gutil.PluginError(taskName, err);
gutil.log(`[${taskName}]`, stats.toString());
gulpDone();
}
}
// Transpiling & Building
gulp.task('clean:build', function() { return del('dist/'); });
gulp.task('ngc', function(cb) {
var executable = path.join(__dirname, platformPath('/node_modules/.bin/ngc'));
exec(`${executable} -p ./tsconfig-es2015.json`, (e) => {
if (e) console.log(e);
del('./dist/waste');
cb();
}).stdout.on('data', function(data) { console.log(data); });
});
gulp.task('umd', function(cb) {
function ngExternal(ns) {
var ng2Ns = `@angular/${ns}`;
return {root: ['ng', ns], commonjs: ng2Ns, commonjs2: ng2Ns, amd: ng2Ns};
}
function rxjsExternal(context, request, cb) {
if (/^rxjs\/add\/observable\//.test(request)) {
return cb(null, {root: ['Rx', 'Observable'], commonjs: request, commonjs2: request, amd: request});
} else if (/^rxjs\/add\/operator\//.test(request)) {
return cb(null, {root: ['Rx', 'Observable', 'prototype'], commonjs: request, commonjs2: request, amd: request});
} else if (/^rxjs\//.test(request)) {
return cb(null, {root: ['Rx'], commonjs: request, commonjs2: request, amd: request});
}
cb();
}
webpack(
{
entry: './temp/index.js',
output: {filename: 'dist/bundles/ng-bootstrap.js', library: 'ngb', libraryTarget: 'umd'},
devtool: 'source-map',
externals: [
{
'@angular/core': ngExternal('core'),
'@angular/common': ngExternal('common'),
'@angular/forms': ngExternal('forms')
},
rxjsExternal
]
},
webpackCallBack('webpack', cb));
});
gulp.task('npm', function() {
var pkgJson = require('./package.json');
var targetPkgJson = {};
var fieldsToCopy = ['version', 'description', 'keywords', 'author', 'repository', 'license', 'bugs', 'homepage'];
targetPkgJson['name'] = '@ng-bootstrap/ng-bootstrap';
fieldsToCopy.forEach(function(field) { targetPkgJson[field] = pkgJson[field]; });
targetPkgJson['main'] = 'bundles/ng-bootstrap.js';
targetPkgJson['module'] = 'index.js';
targetPkgJson['typings'] = 'index.d.ts';
targetPkgJson.peerDependencies = {};
Object.keys(pkgJson.dependencies).forEach(function(dependency) {
targetPkgJson.peerDependencies[dependency] = `^${pkgJson.dependencies[dependency]}`;
});
return gulp.src('README.md')
.pipe(gulpFile('package.json', JSON.stringify(targetPkgJson, null, 2)))
.pipe(gulp.dest('dist'));
});
gulp.task('changelog', function() {
var conventionalChangelog = require('gulp-conventional-changelog');
return gulp.src('CHANGELOG.md', {})
.pipe(conventionalChangelog({preset: 'angular', releaseCount: 1}, {
// Override release version to avoid `v` prefix for git comparison
// See https://github.com/conventional-changelog/conventional-changelog-core/issues/10
currentTag: require('./package.json').version
}))
.pipe(gulp.dest('./'));
});
// Testing
function startKarmaServer(isTddMode, isSaucelabs, done) {
var karmaServer = require('karma').Server;
var travis = process.env.TRAVIS;
var config = {configFile: `${__dirname}/karma.conf.js`, singleRun: !isTddMode, autoWatch: isTddMode};
if (travis) {
config['reporters'] = ['dots'];
config['browsers'] = ['Firefox'];
}
if (isSaucelabs) {
config['reporters'] = ['dots', 'saucelabs'];
config['browsers'] =
['SL_CHROME', 'SL_FIREFOX', 'SL_IE10', 'SL_IE11', 'SL_EDGE13', 'SL_EDGE14', 'SL_SAFARI9', 'SL_SAFARI10'];
if (process.env.TRAVIS) {
var buildId = `TRAVIS #${process.env.TRAVIS_BUILD_NUMBER} (${process.env.TRAVIS_BUILD_ID})`;
config['sauceLabs'] = {build: buildId, tunnelIdentifier: process.env.TRAVIS_JOB_NUMBER};
process.env.SAUCE_ACCESS_KEY = process.env.SAUCE_ACCESS_KEY.split('').reverse().join('');
}
}
new karmaServer(config, done).start();
}
gulp.task('clean:tests', function() { return del(['temp/', 'coverage/']); });
gulp.task('build:tests', ['clean:tests'], (cb) => {
exec(path.join(__dirname, platformPath('/node_modules/.bin/tsc')), (e) => {
if (e) console.log(e);
cb();
}).stdout.on('data', function(data) { console.log(data); });
});
gulp.task(
'ddescribe-iit', function() { return gulp.src(PATHS.specs).pipe(ddescribeIit({allowDisabledTests: false})); });
gulp.task('test', ['build:tests'], function() {
startKarmaServer(false, false, () => {
return gulp.src(PATHS.coverageJson).pipe(remapIstanbul({reports: {'html': 'coverage/html'}}));
});
});
gulp.task('remap-coverage', function() {
return gulp.src(PATHS.coverageJson).pipe(remapIstanbul({reports: {'html': 'coverage/html'}}));
});
gulp.task('tdd', ['clean:tests'], (cb) => {
var executable = path.join(__dirname, platformPath('/node_modules/.bin/tsc'));
var startedKarma = false;
exec(`${executable} -w`, (e) => {
cb(e && e.signal !== 'SIGINT' ? e : undefined);
}).stdout.on('data', function(data) {
console.log(data);
// starting karma in tdd as soon as 'tsc -w' finishes first compilation
if (!startedKarma) {
startedKarma = true;
startKarmaServer(true, false, function(err) { process.exit(err ? 1 : 0); });
}
});
});
gulp.task('saucelabs', ['build:tests'], function(done) {
startKarmaServer(false, true, function(err) {
done(err);
process.exit(err ? 1 : 0);
});
});
// Formatting
gulp.task('lint', function() {
return gulp.src([PATHS.src, PATHS.demo, '!demo/src/api-docs.ts'])
.pipe(tslint({configuration: require('./tslint.json'), formatter: 'prose'}))
.pipe(tslint.report({summarizeFailureOutput: true}));
});
gulp.task('check-format', function() {
return doCheckFormat().on(
'warning', function(e) { console.log("NOTE: this will be promoted to an ERROR in the continuous build"); });
});
gulp.task('enforce-format', function() {
return doCheckFormat().on('warning', function(e) {
console.log("ERROR: You forgot to run clang-format on your change.");
console.log("See https://github.com/ng-bootstrap/ng-bootstrap/blob/master/DEVELOPER.md#clang-format");
process.exit(1);
});
});
function doCheckFormat() {
return gulp
.src([
'gulpfile.js', 'karma-test-shim.js', 'misc/api-doc.js', 'misc/api-doc.spec.js', 'misc/demo-gen.js', PATHS.src
])
.pipe(gulpFormat.checkFormat('file', clangFormat));
}
// Demo
gulp.task('generate-docs', function() {
var getApiDocs = require('./misc/get-doc');
var docs = `const API_DOCS = ${JSON.stringify(getApiDocs(), null, 2)};\n\nexport default API_DOCS;`;
return gulpFile('api-docs.ts', docs, {src: true}).pipe(gulp.dest(PATHS.demoApiDocs));
});
gulp.task('generate-plunks', function() {
var getPlunker = require('./misc/plunk-gen');
var demoGenUtils = require('./misc/demo-gen-utils');
var plunks = [];
demoGenUtils.getDemoComponentNames().forEach(function(componentName) {
plunks = plunks.concat(demoGenUtils.getDemoNames(componentName).reduce(function(soFar, demoName) {
soFar.push({name: `${componentName}/demos/${demoName}/plnkr.html`, source: getPlunker(componentName, demoName)});
return soFar;
}, []));
});
return gulpFile(plunks, {src: true}).pipe(gulp.dest('demo/src/public/app/components'));
});
gulp.task('clean:demo', function() { return del('demo/dist'); });
gulp.task('clean:demo-cache', function() { return del('.publish/'); });
gulp.task(
'demo-server', ['generate-docs', 'generate-plunks'],
shell.task(['webpack-dev-server --port 9090 --config webpack.demo.js --inline --progress']));
gulp.task(
'build:demo', ['clean:demo', 'generate-docs', 'generate-plunks'],
shell.task(['webpack --config webpack.demo.js --progress --profile --bail'], {env: {MODE: 'build'}}));
gulp.task(
'demo-server:aot', ['generate-docs', 'generate-plunks'],
shell.task(
['webpack-dev-server --port 9090 --config webpack.demo.js --inline --progress'], {env: {MODE: 'build'}}));
gulp.task('demo-push', function() {
return gulp.src(PATHS.demoDist)
.pipe(ghPages({remoteUrl: "https://github.com/ng-bootstrap/ng-bootstrap.github.io.git", branch: "master"}));
});
// Public Tasks
gulp.task('clean', ['clean:build', 'clean:tests', 'clean:demo', 'clean:demo-cache']);
gulp.task('build', function(done) {
runSequence('lint', 'enforce-format', 'ddescribe-iit', 'test', 'clean:build', 'ngc', 'umd', 'npm', done);
});
gulp.task(
'deploy-demo', function(done) { runSequence('clean:demo', 'build:demo', 'demo-push', 'clean:demo-cache', done); });
gulp.task('default', function(done) { runSequence('lint', 'enforce-format', 'ddescribe-iit', 'test', done); });