-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
280 lines (213 loc) · 9.42 KB
/
Copy pathgulpfile.js
File metadata and controls
280 lines (213 loc) · 9.42 KB
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
var gulp = require('gulp');
var colors = require('colors')
var sourcemaps = require('gulp-sourcemaps');
var ts = require('gulp-typescript');
var KarmaServer = require('karma').Server;
var httpServer = require('http-server');
var useref = require('gulp-useref');
var argv = require('yargs').argv;
var zip = require('gulp-zip');
var replace = require('gulp-replace');
var bower = require('bower');
var tsd = require('gulp-tsd');
var del = require('del');
var appPaths = {
baseSource : 'src', //TypeScript source file root
get tsLibrariesSource () { return this.baseSource + '/lib' }, //TypeScript bower dependencies that need to be transpiled
runtimeFilesBase : 'app', //Directory where served files are stored (html, images, css, fonts, javascript (transpiled or third-party))
get output() { return this.runtimeFilesBase + '/transpiled' }, //Output dir for transpiled TypeScript
get sourcePath() { return [this.baseSource + '/**/*.ts']}, //GLOB for TypeScript sources
distributionPath : 'dist', //Directory for bundled verion of application
get htmlFilesPath() { return [this.runtimeFilesBase + '/**/*.html'] }, //GLOB for html files that will be processed during bundling
get resourcesFilesPath() { return [
this.runtimeFilesBase + '/**/*.png',
this.runtimeFilesBase + '/**/*.ico',
this.runtimeFilesBase + '/**/*.css'
]} //GLOB for resource files that will be processed during bundling
};
// Invokes bower install
gulp.task('bower-install', function (done) {
bower.commands.install([], {save: true}, {})
.on('end', function(installed){
done();
});
});
// Invokes tsd reinstall
gulp.task('tsd-install', function (done) {
tsd({
command: 'reinstall',
config: './tsd.json'
}, done);
});
// Copies the files listed under main of each bower package into ${appPaths.runtimeFilesBase}/bower_dependencies
// Check bower.json for overrides of main files
gulp.task('normalize-bower-components', ['bower-install'], function(done) {
var bower = require('main-bower-files');
var bowerNormalizer = require('gulp-bower-normalize');
return gulp.src(bower(), {base: './bower_components'})
.pipe(bowerNormalizer({bowerJson: './bower.json'}))
.pipe(gulp.dest('./' + appPaths.runtimeFilesBase + '/bower_dependencies/'))
});
// Copies the typescript files distributed by bower packages to the
gulp.task('copy-bower-ts-sources', ['normalize-bower-components'], function(done) {
return gulp.src(['./app/bower_dependencies/**/ts/*.ts'])
.pipe(gulp.dest('./' + appPaths.tsLibrariesSource));
});
// Fetch all bower dependencies, copies its main files into ${appPaths.runtimeFilesBase}/bower_dependencies and fetches all .d.ts files
gulp.task('fetch-all-dependencies', ['copy-bower-ts-sources', 'tsd-install']);
function doTranspilation(done) {
del.sync(appPaths.output);
var tsProject = ts.createProject('tsconfig.json', { typescript: require('typescript'), inlineSourceMap : false});
var tsResult = gulp.src(appPaths.sourcePath)
.pipe(sourcemaps.init())
.pipe(ts(tsProject));
return tsResult.js
// Inline source maps that include the original source. No need to serve TS files.
// Chrome: Often fails on Chrome
//.pipe(sourcemaps.write({includeContent: true, debug: true, sourceRoot : '/src/'}))
// Inline source maps that don't include the original source. Needs to serve TS files
// Chrome : Works most of time; Occasionally pauses on JS version instead
.pipe(sourcemaps.write({includeContent: false, debug: true, sourceRoot : '/src/'}))
// Separate source maps that don't include the original source. Needs to serve TS files
// Chrome : Doesn't work
//.pipe(sourcemaps.write('.', {includeContent: false, debug: true, sourceRoot : '/src/'}))
//.pipe(sourcemaps.write({includeContent: false, debug: true, sourceRoot : '/home/78581354572/labs/js/angular-typescript/src/'}))
.pipe(gulp.dest(appPaths.output));
}
// Transpile typescript
gulp.task('transpile', doTranspilation);
// Fetch all dependencies (bower, definetly typed) and then transpiles typescript
gulp.task('restore', ['fetch-all-dependencies'], doTranspilation);
// Watches changes on typescript files and triggers transpilation
//Note: Live reload only works if your HTML file has a <body> tag
gulp.task('watch', function() {
gulp.watch([appPaths.sourcePath, appPaths.tsLibrariesSource], ['transpile']).on('change', reportChange);
function reportChange(event){
console.log('File ' + event.path + ' was ' + event.type + ', running transpilation');
}
});
// Transpiles typescript and run unit tests using Karma
gulp.task('test', ['transpile'], function(done) {
var karmaOptions = {
configFile : __dirname + '/karma.conf.js',
singleRun : true
};
if (argv.browsers) {
karmaOptions.browsers = argv.browsers.split(',');
}
server = new KarmaServer(karmaOptions, done);
server.start();
});
// Generates code coverage report
gulp.task('coverage', ['test'], function (done) {
var remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
return gulp.src('test-results/PhantomJS*/coverage-final.json')
.pipe(remapIstanbul({
reports: {
'html': 'test-results/coverage-report'
},
basePath : './'
}));
});
//Source: http://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js
function findCurrentIPAddress() {
var os = require('os');
var ifaces = os.networkInterfaces();
var address;
Object.keys(ifaces).forEach(function (ifname) {
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
// skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
} else {
address = iface.address;
}
});
});
return address;
}
// Starts HTTP server with root dir being ${appPaths.runtimeFilesBase}
// gulp serve --dist will set root dir to ${appPaths.distributionPath}
// gulp serve --root will set root dir to application's directory
// gulp serve --external will accept external connections
gulp.task('serve', function(done) {
var rootDir = './' + (argv.dist ? appPaths.distributionPath : (argv.root ? '' : appPaths.runtimeFilesBase));
var hostAddress = '0.0.0.0';
var proxyAddress = undefined;
var ipAddress;
if (argv.external) {
ipAddress = findCurrentIPAddress();
if (ipAddress !== undefined) {
hostAddress = ipAddress;
} else {
console.log('Unable to obtain IP address');
}
}
if (typeof(argv.proxy) === 'number') {
proxyAddress = 'http://localhost:' + argv.proxy.toString();
}
var serverOptions = {
host: hostAddress,
port: 8080,
cors : true,
root : rootDir,
cache : -1,
proxy : proxyAddress,
logFn: requestLogger
}
function requestLogger(req, res, error) {
var date = (new Date).toUTCString();
if (error) {
log('[%s] "%s %s" Error (%s): "%s"', date, req.method.red, req.url.red, error.status.toString().red, error.message.red);
} else {
log('[%s] "%s %s" "%s"', date, req.method.cyan, req.url.cyan, req.headers['user-agent']);
}
};
var s = httpServer.createServer(serverOptions);
s.listen(serverOptions.port, serverOptions.host);
log('Starting up http-server, serving '.yellow
+ serverOptions.root.cyan
+ ' on: '.yellow
+ (serverOptions.host + ':' + serverOptions.port).cyan);
if (serverOptions.proxy !== undefined) {
log('Unknown requests being proxied to '.yellow + (serverOptions.proxy).cyan);
}
});
// Concats all transpiled javascript files into ${appPaths.distributionPath}/vendor.js (third-party libs) and
// ${appPaths.distributionPath}/app.js (transpiled application files)
gulp.task('bundle-resources', function(done) {
del.sync(appPaths.distributionPath);
var assets = useref.assets();
return gulp.src(appPaths.htmlFilesPath)
.pipe(assets)
.pipe(assets.restore())
.pipe(useref())
.pipe(gulp.dest(appPaths.distributionPath));
});
// Copy all resource files (images, css, fonts) from ${appPaths.runtimeFilesBase} to ${appPaths.distributionPath}
gulp.task('copy-resource-files', function (done) {
gulp.src(appPaths.resourcesFilesPath)
.pipe(gulp.dest(appPaths.distributionPath));
gulp.src(appPaths.runtimeFilesBase + '/bower_dependencies/bootstrap/fonts/*.*')
.pipe(gulp.dest(appPaths.distributionPath + '/fonts'));
done();
});
// Replaces the tag ${version} on ${appPaths.distributionPath}/index.html to the version configured on package.json
gulp.task('replace-version', ['bundle-resources'], function (done) {
var packageFile = require('./package.json');
return gulp.src(appPaths.distributionPath + '/index.html')
.pipe(replace('${version}', packageFile.version))
.pipe(gulp.dest(appPaths.distributionPath));
});
// Generates bundled version for distribuition on ${appPaths.distributionPath}
// See tasks 'bundle-resource' and 'replace-version'
gulp.task('bundle', ['copy-resource-files', 'replace-version'], function() {
});
// Generates a ZIP files with distribution bundled version
// See task 'bundle'
gulp.task('package', function (done) {
var packageFile = require('./package.json');
return gulp.src(appPaths.distributionPath + '/*')
.pipe(zip(packageFile.name + '-' + packageFile.version + '.zip'))
.pipe(gulp.dest(appPaths.distributionPath));
});