Skip to content

Commit 53260e6

Browse files
author
benholloway
committed
javascript task now also compiles test sources (to facilitate watch) and the test task can now become a seperate file
1 parent 0f4eb6a commit 53260e6

File tree

3 files changed

+70
-66
lines changed

3 files changed

+70
-66
lines changed

tasks/javascript.js

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

3-
var path = require('path'),
4-
fs = require('fs');
3+
var path = require('path'),
4+
fs = require('fs');
55

6-
var gulp = require('gulp'),
7-
jshint = require('gulp-jshint'),
8-
rimraf = require('gulp-rimraf'),
9-
runSequence = require('run-sequence'),
10-
combined = require('combined-stream'),
11-
semiflat = require('gulp-semiflat'),
12-
to5ify = require('6to5ify'),
13-
stringify = require('stringify'),
14-
wordwrap = require('wordwrap'),
15-
ngAnnotate = require('browserify-ngannotate');
6+
var gulp = require('gulp'),
7+
jshint = require('gulp-jshint'),
8+
rimraf = require('gulp-rimraf'),
9+
runSequence = require('run-sequence'),
10+
combined = require('combined-stream'),
11+
semiflat = require('gulp-semiflat'),
12+
to5ify = require('6to5ify'),
13+
stringify = require('stringify'),
14+
wordwrap = require('wordwrap'),
15+
ngAnnotate = require('browserify-ngannotate');
1616

1717
var karma = require('../lib/test/karma'),
1818
browserify = require('../lib/build/browserify'),
@@ -22,10 +22,10 @@ var karma = require('../lib/test/karma'),
2222
jshintReporter = require('../lib/util/jshint-reporter');
2323

2424
var cliArgs;
25-
var transforms;
2625

2726
yargs.getInstance('javascript')
28-
.usage(wordwrap(2, 80)('The "javascript" task performs a one time build of the javascript composition root(s).'))
27+
.usage(wordwrap(2, 80)('The "javascript" task performs a one time build of the javascript composition root(s) ' +
28+
'and also bundles of all .spec.js files in the project.'))
2929
.example('angularity javascript', 'Run this task')
3030
.example('angularity javascript -u', 'Run this task but do not minify javascript')
3131
.options('help', {
@@ -52,39 +52,12 @@ yargs.getInstance('javascript')
5252
//Proper solution would be to have yargs.getInstance modified to
5353
//mixin options and checks from dependent/ prequisite yargs instances
5454

55-
yargs.getInstance('test')
56-
.usage(wordwrap(2, 80)('The "test" task performs a one time build and '+
57-
'karma test of all .spec.js files in the project.'))
58-
.example('angularity test', 'Run this task')
59-
.options('help', {
60-
describe: 'This help message',
61-
alias : [ 'h', '?' ],
62-
boolean : true
63-
})
64-
.options(jshintReporter.yargsOption.key, jshintReporter.yargsOption.value)
65-
.options(karma.yargsOption.key, karma.yargsOption.value)
66-
.strict()
67-
.check(yargs.subCommandCheck)
68-
.check(jshintReporter.yargsCheck)
69-
.check(karma.yargsCheck)
70-
.wrap(80);
71-
7255
gulp.task('javascript', function (done) {
7356
console.log(hr('-', 80, 'javascript'));
74-
init();
75-
runSequence(
76-
['javascript:cleanbuild', 'javascript:lint'],
77-
['javascript:build'],
78-
done
79-
);
80-
});
81-
82-
gulp.task('test', function (done) {
83-
console.log(hr('-', 80, 'test'));
84-
init(true);
57+
cliArgs = cliArgs || yargs.resolveArgv();
8558
runSequence(
86-
['javascript:cleanunit', 'javascript:lint'],
87-
'javascript:unit',
59+
['javascript:cleanbuild', 'javascript:cleanunit', 'javascript:lint'],
60+
['javascript:build', 'javascript:unit'],
8861
done
8962
);
9063
});
@@ -96,8 +69,9 @@ gulp.task('javascript:cleanbuild', function () {
9669
});
9770

9871
// clean javascript from the test directory
72+
// don't remove the karma conf or Webstorm ide will have problems
9973
gulp.task('javascript:cleanunit', function () {
100-
return gulp.src(streams.TEST + '/**/*.js*', {read: false})
74+
return gulp.src([streams.TEST + '/**/*.js*', '!**/karma.conf.js'], {read: false}) // keep configuration
10175
.pipe(rimraf());
10276
});
10377

@@ -114,9 +88,7 @@ gulp.task('javascript:lint', function () {
11488
// karma unit tests in local library only
11589
gulp.task('javascript:unit', function () {
11690
var reporters = [].concat(cliArgs[karma.yargsOption.key])
117-
.filter(function isString(value) {
118-
return (typeof value === 'string');
119-
});
91+
.filter(Boolean);
12092
return combined.create()
12193
.append(
12294
streams
@@ -129,35 +101,34 @@ gulp.task('javascript:unit', function () {
129101
streams
130102
.jsSpec()
131103
.pipe(browserify
132-
.compile(80, transforms.concat(browserify.jasmineTransform('@')))
104+
.compile(80, getTransforms().concat(browserify.jasmineTransform('@')))
133105
.all('index.js', false, '/base'))
134106
.pipe(gulp.dest(streams.TEST))
135107
)
136108
.pipe(semiflat(process.cwd()))
137109
.pipe(karma.createConfig(reporters))
138110
.pipe(gulp.dest(streams.TEST))
139-
.pipe(karma.run(reporters, 80));
140111
});
141112

142113
// give a single optimised javascript file in the build directory with source map for each
143114
gulp.task('javascript:build', function () {
144115
return streams.jsApp({read: false})
145116
.pipe(browserify
146-
.compile(80, transforms)
117+
.compile(80, getTransforms(!cliArgs.unminified))
147118
.each(!cliArgs.unminified))
148119
.pipe(gulp.dest(streams.BUILD));
149120
});
150121

151122
/**
152-
* Initialisation must be deferred until a task actually starts
123+
* Retrieve the transform list
124+
* @param {boolean} isMinify Indicates whether minification will be used
153125
*/
154-
function init(unminified) {
155-
cliArgs = cliArgs || yargs.resolveArgv();
156-
transforms = [
157-
to5ify.configure({ ignoreRegex: /(?!)/ }), // convert any es6 to es5 (degenerate regex)
158-
stringify({ minify: false }), // allow import of html to a string
159-
!cliArgs.unminified && !unminified && ngAnnotate, { sourcemap: true } // @ngInject for angular injection points
160-
];
126+
function getTransforms(isMinify) {
127+
return [
128+
to5ify.configure({ ignoreRegex: /(?!)/ }), // convert any es6 to es5 (degenerate regex)
129+
stringify({ minify: false }), // allow import of html to a string
130+
isMinify && ngAnnotate, { sourcemap: true } // @ngInject for angular injection points
131+
].filter(Boolean);
161132
// TODO @bholloway fix stringify({ minify: true }) throwing error on badly formed html so that we can minify
162133
// TODO @bholloway fix sourcemaps in ngAnnotate so that it may be included even when not minifying
163-
}
134+
}

tasks/test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
var path = require('path'),
4+
fs = require('fs');
5+
6+
var gulp = require('gulp'),
7+
jshint = require('gulp-jshint'),
8+
stringify = require('stringify'),
9+
wordwrap = require('wordwrap');
10+
11+
var karma = require('../lib/test/karma'),
12+
yargs = require('../lib/util/yargs'),
13+
hr = require('../lib/util/hr'),
14+
streams = require('../lib/config/streams'),
15+
jshintReporter = require('../lib/util/jshint-reporter');
16+
17+
yargs.getInstance('test')
18+
.usage(wordwrap(2, 80)('The "test" task performs a one time build and karma test of all .spec.js files in the ' +
19+
'project.'))
20+
.example('angularity test', 'Run this task')
21+
.options('help', {
22+
describe: 'This help message',
23+
alias : [ 'h', '?' ],
24+
boolean : true
25+
})
26+
.options(karma.yargsOption.key, karma.yargsOption.value)
27+
.options(jshintReporter.yargsOption.key, jshintReporter.yargsOption.value)
28+
.strict()
29+
.check(yargs.subCommandCheck)
30+
.check(karma.yargsCheck)
31+
.check(jshintReporter.yargsCheck)
32+
.wrap(80);
33+
34+
gulp.task('test', ['javascript'], function () {
35+
console.log(hr('-', 80, 'test'));
36+
gulp.src(streams.TEST + '/karma.conf.js')
37+
.pipe(karma.run());
38+
});

tasks/watch.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ gulp.task('watch', ['server'], function () {
7373
});
7474

7575
// watch statements
76-
watch(getGlobAppNodeBower('**/*.js', '**/*.html', '!' + streams.APP + '/**/*.html', '!*.*', '!**/*.spec.*'), {
76+
watch(getGlobAppNodeBower('**/*.js', '**/*.html', '!' + streams.APP + '/**/*.html', '!*.*'), {
7777
name : 'JS|HTML',
7878
emitOnGlob: false
7979
}, queue.getHandler('javascript', 'html', 'reload')); // html will be needed in case previous injection failed
@@ -87,9 +87,4 @@ gulp.task('watch', ['server'], function () {
8787
name : 'INJECT',
8888
emitOnGlob: false
8989
}, queue.getHandler('html', 'reload'));
90-
91-
watch(streams.getLocalLibGlob()(['**/*.spec.js', '!*.spec.js']), {
92-
name : 'TEST',
93-
emitOnGlob: false
94-
}, queue.getHandler('test'));
9590
});

0 commit comments

Comments
 (0)