gulpfile.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. var gulp = require('gulp'),
  2. //var deleted = require('gulp-deleted');
  3. del = require('del'),
  4. uglify = require('gulp-tm-uglify'),
  5. rename = require('gulp-rename'),
  6. changed = require('gulp-changed'),
  7. gulpif = require('gulp-if'),
  8. minimist = require('minimist'),
  9. ftp = require('gulp-ftp'),
  10. sftp = require('gulp-sftp'),
  11. JSFtp = require('jsftp'),
  12. gutil = require('gulp-util'),
  13. fs = require("fs");
  14. var through2 = require('through2');
  15. var assetRev = require('gulp-tm-asset-rev');
  16. var apps = require('./gulpapps.js');
  17. var ftpconfig = require('./gulpconfig.js');
  18. var options = minimist(process.argv.slice(2), {//upload: local ftp or sftp
  19. string: ["ev", "upload", "location", "host", "user", "pass", "port", "remotePath", "dest", "src"]
  20. });
  21. var uploadOptions = ftpconfig.dev;
  22. if (options.ev && options.ev=="dev"){
  23. uploadOptions = ftpconfig.dev;
  24. }else if (options.ev && options.ev=="release"){
  25. uploadOptions = ftpconfig.release;
  26. }else if (options.ev && options.ev=="wrdp"){
  27. uploadOptions = ftpconfig.wrdp;
  28. }else{
  29. options.ev = "dev";
  30. uploadOptions = ftpconfig.dev;
  31. }
  32. options.upload = options.upload || "";
  33. options.location = options.location || uploadOptions.location;
  34. options.host = options.host || uploadOptions.host;
  35. options.user = options.user || uploadOptions.user;
  36. options.pass = options.pass || uploadOptions.pass;
  37. options.port = options.port || uploadOptions.port;
  38. options.remotePath = options.remotePath || uploadOptions.remotePath;
  39. options.dest = options.dest || uploadOptions.dest || "dest";
  40. var release_options = {};
  41. release_options.ev = "release";
  42. release_options.upload = release_options.upload || "";
  43. release_options.location = release_options.location || ftpconfig.release.location;
  44. release_options.host = release_options.host || ftpconfig.release.host;
  45. release_options.user = release_options.user || ftpconfig.release.user;
  46. release_options.pass = release_options.pass || ftpconfig.release.pass;
  47. release_options.port = release_options.port || ftpconfig.release.port;
  48. release_options.remotePath = release_options.remotePath || ftpconfig.release.remotePath;
  49. release_options.dest = release_options.dest || ftpconfig.release.dest || "dest";
  50. var appTasks = [];
  51. function getAppTask(path, isMin, thisOptions) {
  52. return function (cb) {
  53. //var srcFile = 'source/' + path + '/**/*';
  54. var option = thisOptions || options;
  55. var src;
  56. var dest = option.dest+'/' + path + '/';
  57. if (isMin){
  58. var src_min = ['source/' + path + '/**/*.js', '!**/*.spec.js', '!**/test/**'];
  59. var src_move = ['source/' + path + '/**/*', '!**/*.spec.js', '!**/test/**'];
  60. gutil.log("Move-Uglify", ":", gutil.colors.green(gutil.colors.blue(path), gutil.colors.white('->'), dest));
  61. return gulp.src(src_min)
  62. .pipe(changed(dest))
  63. .pipe(uglify())
  64. .pipe(rename({ extname: '.min.js' }))
  65. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  66. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  67. host: option.host,
  68. user: option.user || 'anonymous',
  69. pass: option.pass || '@anonymous',
  70. port: option.port || 21,
  71. remotePath: (option.remotePath || '/') + path
  72. })))
  73. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  74. host: option.host,
  75. user: option.user || 'anonymous',
  76. pass: option.pass || null,
  77. port: option.port || 22,
  78. remotePath: (option.remotePath || '/') + path
  79. })))
  80. .pipe(gulpif((option.ev == "dev") ,gulp.dest(dest)))
  81. .pipe(gulp.src(src_move))
  82. .pipe(changed(dest))
  83. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  84. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  85. host: option.host,
  86. user: option.user || 'anonymous',
  87. pass: option.pass || '@anonymous',
  88. port: option.port || 21,
  89. remotePath: (option.remotePath || '/') + path
  90. })))
  91. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  92. host: option.host,
  93. user: option.user || 'anonymous',
  94. pass: option.pass || null,
  95. port: option.port || 22,
  96. remotePath: (option.remotePath || '/') + path
  97. })))
  98. .pipe(gulp.dest(dest))
  99. .pipe(gutil.noop());
  100. }else{
  101. src = ['source/' + path + '/**/*', '!**/*.spec.js', '!**/test/**'];
  102. gutil.log("Move", ":", gutil.colors.green(gutil.colors.blue(path), gutil.colors.white('->'), dest));
  103. return gulp.src(src)
  104. .pipe(changed(dest))
  105. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  106. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  107. host: option.host,
  108. user: option.user || 'anonymous',
  109. pass: option.pass || '@anonymous',
  110. port: option.port || 21,
  111. remotePath: (option.remotePath || '/') + path
  112. })))
  113. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  114. host: option.host,
  115. user: option.user || 'anonymous',
  116. pass: option.pass || null,
  117. port: option.port || 22,
  118. remotePath: (option.remotePath || '/') + path
  119. })))
  120. .pipe(gulp.dest(dest))
  121. .pipe(gutil.noop());
  122. }
  123. }
  124. }
  125. //var taskObj = {};
  126. apps.map(function (app) {
  127. var isMin = (app.tasks.indexOf("min")!==-1);
  128. taskName = app.folder;
  129. appTasks.push(taskName);
  130. gulp.task(taskName, getAppTask(app.folder, isMin));
  131. //var isMin = (app.tasks.indexOf("min")!==-1);
  132. taskName = app.folder+"_release";
  133. //appTasks.push(taskName);
  134. gulp.task(taskName, getAppTask(app.folder, isMin, release_options));
  135. });
  136. // Object.keys(taskObj).map(function(k){
  137. // exports[k] = parallel(taskObj[k]);
  138. // });
  139. //exports[app.folder] = parallel(minTask, moveTask);
  140. function getCleanTask(path) {
  141. return function (cb) {
  142. if (path){
  143. var dest = (path=="/") ? options.dest+"/" : options.dest+'/' + path + '/';
  144. gutil.log("Clean", ":", gutil.colors.red(dest));
  145. del.sync(dest, cb);
  146. cb();
  147. }else{
  148. cb();
  149. }
  150. }
  151. }
  152. function cleanRemoteFtp(f, cb) {
  153. var file = options.remotePath + f;
  154. var ftp = new JSFtp({
  155. host: options.host,
  156. user: options.user || 'anonymous',
  157. pass: options.pass || null,
  158. port: options.port || 21
  159. });
  160. ftp.raw('dele ' + file, function (err) {
  161. if (err) { cb(); return; }
  162. if (file.substring(file.length - 3).toLowerCase() == ".js") {
  163. file = file.replace('.js', ".min.js");
  164. ftp.raw('dele ' + file, function (err) {
  165. if (err) { cb(); return; }
  166. if (file.indexOf("/") != -1) {
  167. var p = file.substring(0, file.lastIndexOf("/"));
  168. ftp.raw('rmd ' + p, function (err) {
  169. if (err) { cb(); return; }
  170. ftp.raw.quit();
  171. cb();
  172. });
  173. }
  174. });
  175. } else {
  176. if (file.indexOf("/") != -1) {
  177. var pPath = file.substring(0, file.lastIndexOf("/"));
  178. ftp.raw('rmd ' + pPath, function (err) {
  179. if (err) { cb(); return; }
  180. ftp.raw.quit();
  181. cb();
  182. });
  183. }
  184. }
  185. });
  186. }
  187. function cleanRemoteLocal(f, cb) {
  188. var file = options.location + f;
  189. del(file, { force: true, dryRun: true }, function () {
  190. if (file.substring(file.length - 3).toLowerCase() == ".js") {
  191. var minfile = file.replace('.js', ".min.js");
  192. del(minfile, { force: true, dryRun: true }, function () {
  193. var p = file.substring(0, file.lastIndexOf("/"));
  194. fs.rmdir(p, function (err) {
  195. if (err) { }
  196. cb();
  197. })
  198. });
  199. } else {
  200. var p = file.substring(0, file.lastIndexOf("/"));
  201. fs.rmdir(p, function (err) {
  202. if (err) { }
  203. cb();
  204. })
  205. }
  206. });
  207. }
  208. function getCleanRemoteTask(path) {
  209. return function (cb) {
  210. if (options.upload) {
  211. var file = path.replace(/\\/g, "/");
  212. file = file.substring(file.indexOf("source/") + 7);
  213. if (options.upload == 'local' && options.location != '') cleanRemoteLocal(file, cb);
  214. if (options.upload == 'ftp' && options.host != '') cleanRemoteFtp(file, cb);
  215. } else {
  216. if (cb) cb();
  217. }
  218. }
  219. }
  220. function getWatchTask(path) {
  221. return (path) ? function (cb) {
  222. gutil.log("watch", ":", gutil.colors.green(path, "is watching ..."));
  223. gulp.watch(['source/' + path + '/**/*', "!./**/test/**"], { "events": ['addDir', 'add', 'change'] }, gulp.parallel([path]));
  224. } : function(cb){cb();};
  225. }
  226. gulp.task("clean", getCleanTask(options.src))
  227. gulp.task("watch", getWatchTask(options.src));
  228. gulp.task("index", function () {
  229. var src = ['source/favicon.ico', 'source/index.html'];
  230. var dest = options.dest;
  231. return gulp.src(src)
  232. .pipe(changed(dest))
  233. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + '/')))
  234. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  235. host: options.host,
  236. user: options.user || 'anonymous',
  237. pass: options.pass || '@anonymous',
  238. port: options.port || 21,
  239. remotePath: (options.remotePath || '/')
  240. })))
  241. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), ftp({
  242. host: options.host,
  243. user: options.user || 'anonymous',
  244. pass: options.pass || null,
  245. port: options.port || 22,
  246. remotePath: (options.remotePath || '/')
  247. })))
  248. .pipe(gulp.dest(dest))
  249. .pipe(gutil.noop());
  250. });
  251. gulp.task("cleanAll", getCleanTask('/'));
  252. gulp.task("o2:new-v:html", function () {
  253. var path = "x_desktop";
  254. var src = 'source/x_desktop/*.html';
  255. var dest = options.dest + '/x_desktop/';
  256. return gulp.src(src)
  257. .pipe(assetRev())
  258. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  259. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  260. host: options.host,
  261. user: options.user || 'anonymous',
  262. pass: options.pass || '@anonymous',
  263. port: options.port || 21,
  264. remotePath: (options.remotePath || '/') + path
  265. })))
  266. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  267. host: options.host,
  268. user: options.user || 'anonymous',
  269. pass: options.pass || null,
  270. port: options.port || 22,
  271. remotePath: (options.remotePath || '/') + path
  272. })))
  273. .pipe(gulp.dest(dest))
  274. .pipe(gutil.noop());
  275. });
  276. gulp.task("o2:new-v:o2", function () {
  277. var path = "o2_core";
  278. var src = 'source/o2_core/o2.js';
  279. var dest = options.dest +'/o2_core/';
  280. return gulp.src(src)
  281. .pipe(assetRev())
  282. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  283. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  284. host: options.host,
  285. user: options.user || 'anonymous',
  286. pass: options.pass || '@anonymous',
  287. port: options.port || 21,
  288. remotePath: (options.remotePath || '/') + path
  289. })))
  290. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  291. host: options.host,
  292. user: options.user || 'anonymous',
  293. pass: options.pass || null,
  294. port: options.port || 22,
  295. remotePath: (options.remotePath || '/') + path
  296. })))
  297. .pipe(gulp.dest(dest))
  298. .pipe(uglify())
  299. .pipe(rename({ extname: '.min.js' }))
  300. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  301. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  302. host: options.host,
  303. user: options.user || 'anonymous',
  304. pass: options.pass || '@anonymous',
  305. port: options.port || 21,
  306. remotePath: (options.remotePath || '/') + path
  307. })))
  308. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  309. host: options.host,
  310. user: options.user || 'anonymous',
  311. pass: options.pass || null,
  312. port: options.port || 22,
  313. remotePath: (options.remotePath || '/') + path
  314. })))
  315. .pipe(gulp.dest(dest))
  316. .pipe(gutil.noop());
  317. });
  318. gulp.task("o2:new-v", gulp.parallel("o2:new-v:o2", "o2:new-v:html"));
  319. gulp.task("git_clean", function (cb) {
  320. var dest = 'D:/O2/github/huqi1980/o2oa/o2web/source/';
  321. del(dest, { dryRun: true, force: true }, cb);
  322. });
  323. gulp.task("git_dest", function () {
  324. var dest = "D:/O2/github/huqi1980/o2oa/o2web/source";
  325. return gulp.src(["source/**/*", "!./**/test/**"])
  326. .pipe(changed(dest))
  327. .pipe(gulp.dest(dest))
  328. });
  329. gulp.task("git", gulp.series('git_clean', 'git_dest'));
  330. gulp.task("default", gulp.series(gulp.parallel(appTasks, 'index'), "o2:new-v"));