gulpfile.js 15 KB

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