gulpfile.js 14 KB

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