UNPKG

6.05 kBJavaScriptView Raw
1var fs = require('fs');
2var path = require('path');
3var karma = require('karma').server;
4var _ = require('./src/util.js');
5var gulp = require('gulp');
6var spawn = require('child_process').spawn;
7var shell = require('gulp-shell');
8var component = require('gulp-component');
9var istanbul = require('gulp-istanbul');
10var jshint = require('gulp-jshint');
11var webpack = require('gulp-webpack');
12var mocha = require('gulp-mocha');
13var uglify = require('gulp-uglify');
14var gutil = require('gulp-util');
15var through = require('through2');
16var before_mocha = require('./test/before_mocha.js');
17var pkg;
18
19try{
20 pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
21 pkg_bower = JSON.parse(fs.readFileSync('./bower.json', 'utf8'))
22 pkg_component = JSON.parse(fs.readFileSync('./component.json', 'utf8'))
23}catch(e){}
24
25gulp.task('node', function() {
26 return gulp.src('src/node.js')
27 .pipe(webpack({
28 output: {
29 filename: 'regular-parser.js',
30 libraryTarget: "umd"
31 }
32 }
33 ))
34 .pipe(gulp.dest('dist/'));
35});
36
37gulp.task('default', ['test'], function() {});
38
39
40//one could also externalize common config into a separate file,
41//ex.: var karmaCommonConf = require('./karma-common-conf.js');
42var karmaCommonConf = {
43 browsers: ['Chrome', 'Firefox', 'IE', 'IE9', 'IE8', 'IE7', 'PhantomJS'],
44 frameworks: ['mocha'],
45 files: [
46 'test/regular.js',
47 'test/karma.js',
48 'test/runner/vendor/expect.js',
49 'test/runner/vendor/jquery.js',
50 'test/runner/vendor/nes.js',
51 'test/runner/vendor/util.js',
52 'test/spec/test-*.js',
53 'test/spec/browser-*.js'
54 ],
55 client: {
56 mocha: {ui: 'bdd'}
57 },
58 customLaunchers: {
59 IE9: {
60 base: 'IE',
61 'x-ua-compatible': 'IE=EmulateIE9'
62 },
63 IE8: {
64 base: 'IE',
65 'x-ua-compatible': 'IE=EmulateIE8'
66 },
67 IE7: {
68 base: 'IE',
69 'x-ua-compatible': 'IE=EmulateIE8'
70 }
71 },
72
73 // coverage reporter generates the coverage
74 reporters: ['progress', 'coverage'],
75
76 preprocessors: {
77 // source files, that you wanna generate coverage for
78 // do not include tests or libraries
79 // (these files will be instrumented by Istanbul)
80 'test/regular.js': ['coverage']
81 },
82
83 // optionally, configure the reporter
84 coverageReporter: {
85 type: 'html'
86 }
87
88};
89
90/**
91 * Run test once and exit
92 */
93gulp.task('karma', function (done) {
94 var config = _.extend({}, karmaCommonConf);
95 if(process.argv[3] === '--phantomjs'){
96 config.browsers=["PhantomJS"]
97 config.coverageReporter = {type : 'text-summary'}
98
99 karma.start(_.extend(config, {singleRun: true}), done);
100 }else{
101 karma.start(_.extend(config, {singleRun: true}), done);
102 }
103
104});
105
106
107// build after jshint
108gulp.task('build',["jshint", 'node'], function(){
109 // form minify
110 gulp.src('./component.json')
111 .pipe(component.scripts({
112 standalone: 'Regular',
113 name: 'regular'
114 }))
115 .pipe(wrap(signatrue))
116 .pipe(gulp.dest('dist'))
117 .pipe(wrap(mini))
118 .pipe(uglify())
119 .pipe(gulp.dest('dist'))
120 .on('error', function(err){
121 console.log(err)
122 })
123
124 // for test
125 gulp.src('./component.json')
126 .pipe(component.scripts({
127 name: 'regular'
128 }))
129 .pipe(wrap(signatrue))
130 .pipe(gulp.dest('test'))
131
132})
133
134
135// gulp v -0.0.1
136gulp.task('v', function(fn){
137 var version = process.argv[3].replace('-',"");
138 pkg.version = version
139 pkg_component.version = version
140 pkg_bower.version = version
141 try{
142 fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2), 'utf8');
143 fs.writeFileSync('./component.json', JSON.stringify(pkg_component, null, 2), 'utf8');
144 fs.writeFileSync('./bower.json', JSON.stringify(pkg_bower, null, 2), 'utf8');
145 }catch(e){
146 console.error('update version faild' + e.message)
147 }
148})
149
150
151// watch file then build
152gulp.task('dev', function(){
153 gulp.watch(['component.json', 'src/**/*.js'], ['build'])
154 var puer = spawn('puer', ["--no-reload"], {})
155 puer.stdout.on('data', function (data) {
156 console.log(""+ data);
157 });
158 puer.stderr.on('data', function (data) {
159 console.log('stderr: ' + data);
160 });
161
162 puer.on('close', function (code) {
163 console.log('puer test compelete!');
164 });
165
166})
167
168gulp.task('dev-test', function(){
169 gulp.watch(['src/**/*.js', 'test/spec/**/*.js'], ['build','mocha'])
170})
171
172
173
174//
175gulp.task('jshint', function(){
176 // jshint
177 gulp.src(['src/**/*.js'])
178 .pipe(jshint())
179 .pipe(jshint.reporter('default'))
180
181})
182
183gulp.task('cover', function(cb){
184 before_mocha.dirty();
185 gulp.src(['src/**/*.js'])
186 .pipe(istanbul()) // Covering files
187 .on('end', function () {
188 gulp.src(['test/spec/test-*.js'])
189 .pipe(mocha())
190 .pipe(istanbul.writeReports('./test/coverage')) // Creating the reports after tests runned
191 .on('end', cb);
192 });
193})
194
195gulp.task('test', ['jshint','mocha', 'karma'])
196
197// for travis
198gulp.task('travis', ['jshint' ,'build','mocha', 'karma']);
199
200gulp.task('mocha', function() {
201
202 before_mocha.dirty();
203
204 return gulp.src(['test/spec/test-*.js'])
205 .pipe(mocha({reporter: 'spec' }) )
206 .on('error', function(){
207 gutil.log.apply(this, arguments);
208 console.log('\u0007');
209 })
210 .on('end', function(){
211 before_mocha.clean();
212 });
213});
214
215
216gulp.task('casper', function(){
217 var casperjs = spawn('casperjs', ['test','--concise', 'spec'], {
218 cwd: path.resolve('test/fixtures')
219 })
220
221 casperjs.stdout.on('data', function (data) {
222 console.log(""+ data);
223 });
224 casperjs.stderr.on('data', function (data) {
225 console.log('stderr: ' + data);
226 });
227
228 casperjs.on('close', function (code) {
229 console.log('casperjs test compelete!');
230 });
231
232})
233
234
235
236function wrap(fn){
237 return through.obj(fn);
238}
239
240function signatrue(file, enc, cb){
241 var sign = '/**\n'+ '@author\t'+ pkg.author.name + '\n'+ '@version\t'+ pkg.version +
242 '\n'+ '@homepage\t'+ pkg.homepage + '\n*/\n';
243 file.contents = Buffer.concat([new Buffer(sign), file.contents]);
244 cb(null, file);
245}
246
247function mini(file, enc, cb){
248 file.path = file.path.replace('.js', '.min.js');
249 cb(null, file)
250}
\No newline at end of file