1 | var fs = require('fs');
|
2 | var path = require('path');
|
3 | var karma = require('karma').server;
|
4 | var _ = require('./src/util.js');
|
5 | var gulp = require('gulp');
|
6 | var spawn = require('child_process').spawn;
|
7 | var shell = require('gulp-shell');
|
8 | var component = require('gulp-component');
|
9 | var istanbul = require('gulp-istanbul');
|
10 | var jshint = require('gulp-jshint');
|
11 | var webpack = require('gulp-webpack');
|
12 | var mocha = require('gulp-mocha');
|
13 | var uglify = require('gulp-uglify');
|
14 | var gutil = require('gulp-util');
|
15 | var through = require('through2');
|
16 | var before_mocha = require('./test/before_mocha.js');
|
17 | var pkg;
|
18 |
|
19 | try{
|
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 |
|
25 | gulp.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 |
|
37 | gulp.task('default', ['test'], function() {});
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | var 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 |
|
74 | reporters: ['progress', 'coverage'],
|
75 |
|
76 | preprocessors: {
|
77 |
|
78 |
|
79 |
|
80 | 'test/regular.js': ['coverage']
|
81 | },
|
82 |
|
83 |
|
84 | coverageReporter: {
|
85 | type: 'html'
|
86 | }
|
87 |
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | gulp.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 |
|
108 | gulp.task('build',["jshint", 'node'], function(){
|
109 |
|
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 |
|
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 |
|
136 | gulp.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 |
|
152 | gulp.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 |
|
168 | gulp.task('dev-test', function(){
|
169 | gulp.watch(['src/**/*.js', 'test/spec/**/*.js'], ['build','mocha'])
|
170 | })
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | gulp.task('jshint', function(){
|
176 |
|
177 | gulp.src(['src/**/*.js'])
|
178 | .pipe(jshint())
|
179 | .pipe(jshint.reporter('default'))
|
180 |
|
181 | })
|
182 |
|
183 | gulp.task('cover', function(cb){
|
184 | before_mocha.dirty();
|
185 | gulp.src(['src/**/*.js'])
|
186 | .pipe(istanbul())
|
187 | .on('end', function () {
|
188 | gulp.src(['test/spec/test-*.js'])
|
189 | .pipe(mocha())
|
190 | .pipe(istanbul.writeReports('./test/coverage'))
|
191 | .on('end', cb);
|
192 | });
|
193 | })
|
194 |
|
195 | gulp.task('test', ['jshint','mocha', 'karma'])
|
196 |
|
197 |
|
198 | gulp.task('travis', ['jshint' ,'build','mocha', 'karma']);
|
199 |
|
200 | gulp.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 |
|
216 | gulp.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 |
|
236 | function wrap(fn){
|
237 | return through.obj(fn);
|
238 | }
|
239 |
|
240 | function 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 |
|
247 | function mini(file, enc, cb){
|
248 | file.path = file.path.replace('.js', '.min.js');
|
249 | cb(null, file)
|
250 | } |
\ | No newline at end of file |