gulpfile.js 15 KB

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