-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
209 lines (182 loc) · 5.91 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
'use strict';
let browserify = require('browserify');
let gulp = require('gulp');
let source = require('vinyl-source-stream');
let tsify = require("tsify");
let exorcist = require('exorcist');
let ts = require('gulp-typescript');
let dts = require('dts-bundle');
let Promise = require('bluebird');
let gutil = require('gulp-util');
let del = require('del');
let path = require('path');
let karma = require('karma');
let runSequence = require('run-sequence');
let coveralls = require('gulp-coveralls');
let fs = require('fs');
let replace = require('gulp-replace');
let rename = require('gulp-rename');
let header = require('gulp-header');
let config = require('./bower.json');
/* Config */
let appVersion = config.version.replace('-alpha', '');
/* Tasks */
gulp.task('default', (done) => {
return runSequence(
'build',
'test',
done
);
});
gulp.task('ci', (done) => {
return runSequence(
'build',
'test-coverage',
'publish-test-coverage',
done
);
});
gulp.task('build', () => {
banner('Compiling TypeScript Files');
let projectPath = path.join('.', 'tsconfig.json');
let project = ts.createProject(projectPath);
let result = project.src().pipe(ts(project));
return asPromise(result.js.pipe(gulp.dest('.')))
.then(() => log('Done compiling TypeScript files!'))
.catch(() => error('Failed to compile TypeScript files!'));
});
gulp.task('test', (done) => {
banner('Running Karma Tests');
let server = new karma.Server({
configFile: path.join(__dirname, 'karma.conf.js'),
singleRun: true
}, done);
server.start();
});
gulp.task('test-watch', (done) => {
banner('Starting Karma Test Server');
let server = new karma.Server({
configFile: path.join(__dirname, 'karma.conf.js')
}, done);
server.start();
});
gulp.task('test-coverage', (done) => {
banner('Running Karma Test Coverage');
let server = new karma.Server({
configFile: path.join(__dirname, 'karma.conf.js'),
reporters: ['mocha', 'coverage'],
singleRun: true
}, done);
server.start();
});
gulp.task('publish-test-coverage', ['test-coverage'], () => {
gulp.src('./coverage/**/lcov.info')
.pipe(coveralls());
});
gulp.task('publish', () => {
var bundledFileName = 'decorum.js',
sourceMapFileName = './dist/' + bundledFileName + '.map',
globalNamespace = 'decorum';
let bundle = browserify({
entries: './src/main.ts',
debug: true,
standalone: globalNamespace
})
.plugin(tsify, { module: 'umd', target: 'es5', sourceMap: true })
.bundle()
.pipe(exorcist(sourceMapFileName))
.pipe(source(bundledFileName))
.pipe(gulp.dest('./dist'));
let banner = `
/*!
* ${config.name} - ${config.version}
*
* ${config.description}
*
* Copyright 2015 Danil Flores
*
* @version v${config.version}
* @link ${config.homepage}
* @license ${config.license}
*/
`;
return asPromise(bundle)
.then((x) => {
return gulp.src('./dist/' + bundledFileName)
.pipe(header(banner.trim() + '\n\n'))
.pipe(gulp.dest('./dist'));
})
.catch(err => error(err));
});
gulp.task('typedef', () => {
log('Generating base TypeScript declarations...');
asPromise(gulp.src('./src/**/*.ts')
.pipe(ts({
target: 'ES6',
declaration: true,
noExternalResolve: true,
listFiles: true,
})).dts
.pipe(gulp.dest('./build/temp/dts')))
.then(() => {
log('Bundling TypeScript declarations...');
dts.bundle({
name: 'decorum',
main: './build/temp/dts/main.d.ts',
out: '../decorum_temp.d.ts'
});
let typedefHeader = `
// Type definitions for Decorum JS v${appVersion}
// Project: https://github.com/dflor003/decorum
// Definitions by: Danil Flores <https://github.com/dflor003>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module 'decorum' {
export = decorum;
}
declare namespace decorum {
`;
gulp.src('./build/temp/decorum_temp.d.ts')
.pipe(replace(/\r\n/gi, '\n'))
.pipe(replace(/^\s*export\s*\{\s*default.*\n/gmi, ''))
.pipe(replace(/^\s*import.*\n/gmi, ''))
.pipe(replace(/^\s*declare module.*\n/gmi, ''))
.pipe(replace(/^}\s*\n/gmi, ''))
.pipe(replace(/export default/gmi, 'export'))
.pipe(replace(/^\s*export let .*\n/gmi, ''))
.pipe(replace(/^\s*export [a-z0-9_@$]+;\n/gmi, ''))
.pipe(replace(/(\s+)let ([a-z0-9_@$]+:)/gmi, '$1export let $2'))
.pipe(replace(/abstract class ([a-z0-9_@$]+\s*\{)/gmi, 'export abstract class $1'))
.pipe(replace(/([;}])\n([ ]+\/\*\*\n)/gmi, '$1\n\n$2'))
.pipe(replace(/^\/\/ Generated by.*\n/gmi, typedefHeader.trim() + '\n'))
.pipe(replace(/\n$/, '\n}\n'))
.pipe(replace(/\n/g, '\r\n'))
.pipe(rename('decorum.d.ts'))
.pipe(gulp.dest('./build/typings'));
})
.then(() => {
log('Deleting temp declaration files...');
del('./build/temp');
})
.catch((err) => error(err));
});
/* Helpers */
function log() {
gutil.log.apply(gutil.log, arguments);
}
function error() {
let args = Array.prototype.slice.call(arguments, 0).map(arg => gutil.colors.bold.red(arg));
log.apply(null, args);
}
function asPromise(stream) {
return new Promise((resolve, reject) => {
stream
.on('error', reject)
.on('end', resolve);
});
}
function banner(message) {
log('===========================================================');
log(message);
log('===========================================================');
log('');
}