1 | var gulp = require('gulp');
|
2 | var clean = require('gulp-clean');
|
3 | var less = require('gulp-less');
|
4 | var replace = require('gulp-replace-path');
|
5 | var gulpCopy = require('gulp-copy');
|
6 | var fileinclude = require('gulp-file-include');
|
7 | var autoprefixer = require('gulp-autoprefixer');
|
8 | var pngquant = require('imagemin-pngquant');
|
9 | var webpack = require('gulp-webpack');
|
10 | var webpackConfig = require('./webpack.config');
|
11 | var filter = require('gulp-filter');
|
12 | var path = require('path');
|
13 | var minifyCss = require('gulp-minify-css');
|
14 | var imagemin = require('gulp-imagemin');
|
15 | var gulpSequence = require('gulp-sequence');
|
16 | var md5 = require("gulp-md5-url-timestamp");
|
17 | var fs= require('fs');
|
18 | var svn = require('gulp-svn');
|
19 | var ftp = require( 'vinyl-ftp');
|
20 | var plumber = require('gulp-plumber');
|
21 |
|
22 | var buildJsonPath = process.cwd()+"/build.json";
|
23 | var os = require("os");
|
24 | var tempDir = (process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + 'Library/Preferences' : '/var/local'))+"/"+process.env.appName
|
25 |
|
26 | var global = {
|
27 | src:".",
|
28 | dist:tempDir+"/dist",
|
29 | cache:tempDir+'/_cache'
|
30 | };
|
31 | var utils = {
|
32 | extend:function(){
|
33 | var result = {};
|
34 | if(arguments.length == 1){
|
35 | var a = arguments[0];
|
36 | result = copy(result,a);
|
37 | }
|
38 | if(arguments.length>1){
|
39 | for(var j = 1,len=arguments.length;j<len;j++){
|
40 | result = copy(result,arguments[j]);
|
41 | }
|
42 | }
|
43 | return result;
|
44 | function copy(o1,o2){
|
45 | for(var key in o2){
|
46 | o1[key] = o2[key];
|
47 | }
|
48 | return o1;
|
49 | }
|
50 | },
|
51 | parseJson:function(src){
|
52 | var jsonPath = path.resolve(src+"/build.json");
|
53 | try{
|
54 | var file = fs.readFileSync(jsonPath)
|
55 | file = JSON.parse(String(file.contents));
|
56 | return file;
|
57 | }catch(err){
|
58 | return -1;
|
59 | }
|
60 |
|
61 | },
|
62 | isRelativePath:function(path){
|
63 | var result = true;
|
64 | if(path.search(/[:;#]|\)|\(|\{|\}/)>-1){
|
65 | result = false;
|
66 | }
|
67 | return result;
|
68 | },
|
69 | replacePath:function($0,$1,__absolutePath__,relativePath){
|
70 | if(path.isAbsolute($1)||!utils.isRelativePath($1)){
|
71 | return $0;
|
72 | }
|
73 | var releaseUri = config.cdn_path;
|
74 | var imageDir =path.dirname(__absolutePath__);
|
75 |
|
76 | imageDir = path.resolve(imageDir,$1);
|
77 |
|
78 |
|
79 | var relativeDir = path.relative(relativePath, imageDir);
|
80 | var finalUri = releaseUri+'/'+relativeDir;
|
81 | finalUri=finalUri.replace(/\\/g,'/');
|
82 | finalUri=$0.replace($1,finalUri);
|
83 | return finalUri;
|
84 | }
|
85 | };
|
86 |
|
87 |
|
88 | function isBuildFileExist(){
|
89 | try{
|
90 | fs.accessSync(buildJsonPath);
|
91 | return JSON.parse(fs.readFileSync(buildJsonPath).toString());
|
92 | }catch(e){
|
93 | return false;
|
94 | }
|
95 |
|
96 | }
|
97 |
|
98 | function modList(){
|
99 | function ModAtom(id,dist,cdn,ftp){
|
100 | return {
|
101 | id:id,
|
102 | dist:dist,
|
103 | cdn_path:cdn||'',
|
104 | ftp:ftp||{}
|
105 | };
|
106 | }
|
107 | function Mod(src){
|
108 | var mod = this.mod = [];
|
109 | this.scanDirAndAddConfig(src);
|
110 | }
|
111 | Mod.prototype.push = function(id,dist,cdn,ftp){
|
112 | this.mod.push(ModAtom(id,dist,cdn,ftp));
|
113 | };
|
114 | Mod.prototype.getMod = function(id){
|
115 | if(id == '') return undefined;
|
116 | var mod = this.mod;
|
117 | for(var i=0,len=mod.length;i<len;i++){
|
118 | if(mod[i].id == id) return mod[i];
|
119 | }
|
120 | return undefined;
|
121 | };
|
122 | Mod.prototype.scanDirAndAddConfig = function(baseSrc){
|
123 | var sysPath = path.resolve(baseSrc);
|
124 | var dirs = fs.readdirSync(sysPath);
|
125 | for(var i =0,len=dirs.length;i<len;i++){
|
126 | var json = utils.parseJson(path.resolve(baseSrc+"/"+dirs[i]));
|
127 | if(json != -1){
|
128 | this.push(dirs[i],json.dist||null,json.cdn_path,json.ftp);
|
129 | }else{
|
130 | this.push(dirs[i]);
|
131 | }
|
132 | }
|
133 | };
|
134 | return new Mod(path.resolve(global.src));
|
135 | }
|
136 | function makeConfig(modid){
|
137 | var config = {};
|
138 | var mods = modList();
|
139 | var mod = mods.getMod(modid);
|
140 | var file = isBuildFileExist();
|
141 | if(!file){
|
142 | console.log("配置文件未找到");
|
143 | process.exit(0);
|
144 | }
|
145 |
|
146 | if(modid&&!mod){
|
147 | console.log("模块不存在");
|
148 | process.exit();
|
149 | }
|
150 | if(modid&&mods){
|
151 | config.src = global.src+"/"+ mod.id||"";
|
152 | config.cache = global.cache+"/"+ mod.id;
|
153 | config.dist = file.dist||global.dist;
|
154 | config.modName = mod.id;
|
155 | config.ftp = mod.ftp||file.ftp;
|
156 | config.cdn_path = mod.cdn_path||file.cdn_path;
|
157 | }else{
|
158 | config.src = global.src;
|
159 | config.cache = global.cache;
|
160 | config.dist = file.dist||global.dist;
|
161 | config.modName = "";
|
162 | config.ftp = file.ftp;
|
163 | config.cdn_path = file.cdn_path ;
|
164 | }
|
165 | if(!config.ftp&&!config.isPro){
|
166 | config.cdn_path="";
|
167 | }
|
168 |
|
169 | config.isPro = !!process.env.pro;
|
170 | config.live = !!process.env.live;
|
171 | config.autoFtp = !!process.env.ftp;
|
172 | config.publish = process.env.publish;
|
173 | if(typeof config.publish != "undefined"){
|
174 |
|
175 | config.isPro = true;
|
176 | }
|
177 | if(config.live){
|
178 |
|
179 | config.isPro = false;
|
180 | }
|
181 | return config;
|
182 | }
|
183 |
|
184 | function main(){
|
185 | config=makeConfig(process.env.mod||"");
|
186 | }
|
187 | main();
|
188 |
|
189 |
|
190 |
|
191 | gulp.task('clean',function(){
|
192 | var steam = gulp.src(config.dist)
|
193 | .pipe(clean());
|
194 | return steam;
|
195 | });
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | gulp.task('copyLessCache',function(){
|
201 | var steam = gulp.src([config.src+'/**/*.{less,css}'],{base:config.src})
|
202 | .pipe(gulp.dest(config.cache));
|
203 | return steam;
|
204 | });
|
205 |
|
206 | gulp.task('replaceLessCache',function(){
|
207 | var stream = gulp.src([config.cache+'/**/*.{less,css}'])
|
208 |
|
209 | .pipe(replace(/url\(\"{0,1}(.*)\"{0,1}\)/g, function (match, __absolutePath__) {
|
210 | function replaceMethod($0,$1,$2,$3){
|
211 |
|
212 | var finalUri = utils.replacePath($0,$1,__absolutePath__,global.cache)
|
213 |
|
214 | return finalUri;
|
215 | }
|
216 | return replaceMethod;
|
217 | }))
|
218 | .pipe(gulp.dest(config.cache));
|
219 | return stream;
|
220 | });
|
221 |
|
222 | gulp.task('compileLess',function(){
|
223 | var stream = gulp.src([config.cache+'/**/*.{less,css}'],{base:global.cache+"/"})
|
224 | .pipe(plumber())
|
225 | .pipe(less())
|
226 | .pipe(minifyCss())
|
227 | .pipe(autoprefixer({
|
228 | browsers: ['last 2 versions', 'Android >= 4.0'],
|
229 | cascade: true,
|
230 | remove: true
|
231 | }));
|
232 | stream.pipe(gulp.dest(config.dist+"/"))
|
233 | return stream;
|
234 | });
|
235 |
|
236 | gulp.task('delLessCache',function(){
|
237 | var stream = gulp.src(config.cache)
|
238 | .pipe(clean());
|
239 | return stream;
|
240 | });
|
241 |
|
242 | gulp.task("less",function(cb){
|
243 | gulpSequence("copyLessCache","replaceLessCache","compileLess","delLessCache",cb);
|
244 | });
|
245 |
|
246 |
|
247 |
|
248 |
|
249 | gulp.task('html',function(){
|
250 | var stream = gulp.src(config.src+'/**/*.{htm,html}',{base:global.src})
|
251 | .pipe(fileinclude({
|
252 | prefix: '@@',
|
253 | basepath: '@file'
|
254 | }))
|
255 | .pipe(replace(/link.*href=\"([\S^\"]*)\"|src=\"([\S^\"]*)\"|url\(\"{0,1}(.*)\"{0,1}\)/g, function (match, __absolutePath__) {
|
256 | function replaceMethod($0,$1,$2,$3){
|
257 | $1 = $1||$2||$3;
|
258 | $1 = $1||"#";
|
259 | var finalUri = utils.replacePath($0,$1,__absolutePath__,global.src);
|
260 | return finalUri;
|
261 | };
|
262 | return replaceMethod;
|
263 | }))
|
264 | .pipe(gulp.dest(config.dist+''));
|
265 | return stream;
|
266 | });
|
267 |
|
268 |
|
269 |
|
270 | gulp.task('img',function(){
|
271 | var stream = gulp.src(config.src+'/**/*.{jpg,png,gif}',{base:global.src})
|
272 | if(config.isPro){
|
273 | stream = stream.pipe(imagemin({
|
274 | use: [pngquant()]
|
275 | }))
|
276 | }
|
277 | stream.pipe(gulp.dest(config.dist+""))
|
278 | return stream;
|
279 | });
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | gulp.task("js", function() {
|
285 | var packConfig = webpackConfig.makeConfig(config.isPro,config.src);
|
286 | var stream = gulp.src(config.src+'/**/*.js',{base:global.src})
|
287 | .pipe(webpack(packConfig))
|
288 | .pipe(gulp.dest(config.dist+'/'+config.modName))
|
289 | return stream;
|
290 | });
|
291 |
|
292 |
|
293 |
|
294 | gulp.task("md5",function(){
|
295 | var jsF = filter('**/*.js', {restore: true});
|
296 | var cssF = filter('**/*.css', {restore: true});
|
297 | var imgF = filter('**/*.{jpg,png,gif}', {restore: true});
|
298 |
|
299 | var stream = gulp.src(config.dist+'/**/*.{js,css,jpg,png}')
|
300 | .pipe(imgF)
|
301 | .pipe(md5(10,[config.dist+'/**/*.css',config.dist+'/**/*.html'],{dirLevel:1}))
|
302 | .pipe(imgF.restore)
|
303 | .pipe(cssF)
|
304 | .pipe(md5(10,config.dist+'/**/*.html',{dirLevel:1}))
|
305 | .pipe(cssF.restore)
|
306 | .pipe(jsF)
|
307 | .pipe(md5(10,config.dist+'/**/*.html',{dirLevel:1}))
|
308 | .pipe(jsF.restore)
|
309 | return stream;
|
310 | });
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | gulp.task('add', function(){
|
319 | return svn.add('./', {args: '--force'}, function(err){
|
320 | if(err) throw err;
|
321 | });
|
322 | });
|
323 |
|
324 |
|
325 | gulp.task('commit',['add'],function(){
|
326 | return svn.commit(config.publish, function(err){
|
327 | if(err) throw err;
|
328 | });
|
329 | });
|
330 |
|
331 |
|
332 | gulp.task('ftp',function(){
|
333 | var ftpConfig = config.ftp.test;
|
334 | if(config.isPro){
|
335 | ftpConfig = config.ftp.product;
|
336 | }
|
337 |
|
338 | var instanceFtp = ftp.create(ftpConfig);
|
339 | var resource = filter('**/*.{css,jpg,jpeg,js,swf,png,gif}', {restore: true});
|
340 | var html = filter('**/*.{html,htm}', {restore: true});
|
341 |
|
342 | var stream =gulp.src( config.dist+"/**/*", { base: config.dist, buffer: false } )
|
343 | .pipe(html)
|
344 | .pipe(instanceFtp.newerOrDifferentSize(ftpConfig.htmlPath))
|
345 | .pipe(instanceFtp.dest(ftpConfig.htmlPath))
|
346 | .pipe(html.restore)
|
347 | .pipe(resource)
|
348 | .pipe(instanceFtp.newerOrDifferentSize(ftpConfig.resourcePath))
|
349 | .pipe( instanceFtp.dest(ftpConfig.resourcePath))
|
350 | .pipe(resource.restore)
|
351 | return stream;
|
352 | });
|
353 |
|
354 |
|
355 |
|
356 |
|
357 | var browserSync = require("browser-sync");
|
358 |
|
359 |
|
360 |
|
361 | function browser(){
|
362 | browserSync({
|
363 |
|
364 | port: 9000,
|
365 | open: true,
|
366 |
|
367 | startPath: "",
|
368 |
|
369 | server: {
|
370 | directory: true,
|
371 |
|
372 |
|
373 |
|
374 | middleware: function(req,res,next){
|
375 | next();
|
376 | },
|
377 | baseDir: config.dist
|
378 | },
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 | reloadDelay: 1,
|
385 |
|
386 | injectChanges: false
|
387 | });
|
388 |
|
389 |
|
390 | gulp.watch(config.src+'/**/*',function(){
|
391 | gulpSequence("deploy")(function(){
|
392 | browserSync.reload();
|
393 | })
|
394 | })
|
395 |
|
396 | }
|
397 |
|
398 |
|
399 | function watchFtp(){
|
400 | gulp.watch(config.src+'/**/*',function(){
|
401 | gulpSequence("deploy","ftp")(function(err){
|
402 | if(err)console.error(err);
|
403 | })
|
404 | })
|
405 | }
|
406 |
|
407 |
|
408 | if(config.isPro){
|
409 | gulp.task('deploy',gulpSequence(["clean"],['html', 'less'], 'js','img','md5'));
|
410 | }else{
|
411 | gulp.task('deploy',['html', 'less', 'js','img']);
|
412 | }
|
413 |
|
414 |
|
415 |
|
416 |
|
417 | try{
|
418 | fs.accessSync(".svn");
|
419 | gulp.task("publish",gulpSequence('deploy',"commit","ftp"))
|
420 | }catch(e){
|
421 |
|
422 | gulp.task("publish",gulpSequence('deploy',"ftp"))
|
423 | }
|
424 |
|
425 |
|
426 |
|
427 | if(config.live){
|
428 | if(config.autoFtp){
|
429 | console.log("开启实时ftp同步");
|
430 | gulp.task('default',["deploy"],watchFtp);
|
431 | }else{
|
432 | gulp.task('default',["deploy"],browser);
|
433 | }
|
434 | }else{
|
435 | gulp.task('default',["deploy"]);
|
436 | }
|
437 |
|
438 |
|
439 | if(typeof config.publish != "undefined"){
|
440 | console.log("默认发布到测试环境,-p参数表示发布到线上");
|
441 | gulp.task("default",["publish"]);
|
442 | }
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
\ | No newline at end of file |