gulpfile.js 14 KB

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