gulpfile.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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-up4'),
  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 o_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. function getEvOptions(ev){
  22. options.ev = ev;
  23. return (ftpconfig[ev]) || {
  24. 'location': '',
  25. 'host': '',
  26. 'user': '',
  27. 'pass': '',
  28. "port": null,
  29. "remotePath": "",
  30. "dest": "dest",
  31. "upload": ""
  32. }
  33. }
  34. function setOptions(op1, op2){
  35. if (!op2) op2 = {};
  36. options.upload = op1.upload || op2.upload || "";
  37. options.location = op1.location || op2.location || "";
  38. options.host = op1.host || op2.host || "";
  39. options.user = op1.user || op2.user || "";
  40. options.pass = op1.pass || op2.pass || "";
  41. options.port = op1.port || op2.port || "";
  42. options.remotePath = op1.remotePath || op2.remotePath || "";
  43. options.dest = op1.dest || op2.dest || "dest";
  44. }
  45. var options = {};
  46. setOptions(o_options, getEvOptions(o_options.ev));
  47. var appTasks = [];
  48. function getAppTask(path, isMin, thisOptions) {
  49. return function (cb) {
  50. //var srcFile = 'source/' + path + '/**/*';
  51. var option = thisOptions || options;
  52. var src;
  53. var dest = option.dest+'/' + path + '/';
  54. let ev = option.ev
  55. var evList = Object.keys(ftpconfig).map((i)=>{ return (i==ev) ? "*"+i : i; });
  56. if (isMin){
  57. var src_min = ['source/' + path + '/**/*.js', '!**/*.spec.js', '!**/test/**'];
  58. var src_move = ['source/' + path + '/**/*', '!**/*.spec.js', '!**/test/**'];
  59. gutil.log("Move-Uglify", ":", gutil.colors.green(gutil.colors.blue(path), gutil.colors.white('->'), dest));
  60. return gulp.src(src_min)
  61. .pipe(changed(dest))
  62. .pipe(uglify())
  63. .pipe(rename({ extname: '.min.js' }))
  64. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  65. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  66. host: option.host,
  67. user: option.user || 'anonymous',
  68. pass: option.pass || '@anonymous',
  69. port: option.port || 21,
  70. remotePath: (option.remotePath || '/') + path
  71. })))
  72. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  73. host: option.host,
  74. user: option.user || 'anonymous',
  75. pass: option.pass || null,
  76. port: option.port || 22,
  77. remotePath: (option.remotePath || '/') + path
  78. })))
  79. .pipe(gulpif((option.ev == "dev" || option.ev == "pro") ,gulp.dest(dest)))
  80. .pipe(gulp.src(src_move))
  81. .pipe(changed(dest))
  82. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  83. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  84. host: option.host,
  85. user: option.user || 'anonymous',
  86. pass: option.pass || '@anonymous',
  87. port: option.port || 21,
  88. remotePath: (option.remotePath || '/') + path
  89. })))
  90. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  91. host: option.host,
  92. user: option.user || 'anonymous',
  93. pass: option.pass || null,
  94. port: option.port || 22,
  95. remotePath: (option.remotePath || '/') + path
  96. })))
  97. .pipe(gulp.dest(dest))
  98. .pipe(gutil.noop());
  99. }else{
  100. src = ['source/' + path + '/**/*', '!**/*.spec.js', '!**/test/**'];
  101. gutil.log("Move", ":", gutil.colors.green(gutil.colors.blue(path), gutil.colors.white('->'), dest));
  102. return gulp.src(src)
  103. .pipe(changed(dest))
  104. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  105. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  106. host: option.host,
  107. user: option.user || 'anonymous',
  108. pass: option.pass || '@anonymous',
  109. port: option.port || 21,
  110. remotePath: (option.remotePath || '/') + path
  111. })))
  112. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  113. host: option.host,
  114. user: option.user || 'anonymous',
  115. pass: option.pass || null,
  116. port: option.port || 22,
  117. remotePath: (option.remotePath || '/') + path
  118. })))
  119. .pipe(gulp.dest(dest))
  120. .pipe(gutil.noop());
  121. }
  122. }
  123. }
  124. //var taskObj = {};
  125. apps.map(function (app) {
  126. var taskName;
  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"));
  331. function build(){
  332. options.ev = "p";
  333. options.upload = o_options.upload || "";
  334. options.location = o_options.location || uploadOptions.location;
  335. options.host = o_options.host || uploadOptions.host;
  336. options.user = o_options.user || uploadOptions.user;
  337. options.pass = o_options.pass || uploadOptions.pass;
  338. options.port = o_options.port || uploadOptions.port;
  339. options.remotePath = o_options.remotePath || uploadOptions.remotePath;
  340. options.dest = o_options.dest || uploadOptions.dest || "dest";
  341. };
  342. gulp.task("build", gulp.series("clean", gulp.parallel(appTasks, 'index'), "o2:new-v"))