gulpfile.js 15 KB

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