gulpfile.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 assetRev = require('gulp-tm-asset-rev');
  15. var apps = [
  16. {"folder": "o2_lib", "tasks": ["move", "clean"]},
  17. {"folder": "o2_core", "tasks": ["move", "min", "clean", "watch"]},
  18. {"folder": "x_component_ANN", "tasks": ["move", "min", "clean", "watch"]},
  19. {"folder": "x_component_AppCenter", "tasks": ["move", "min", "clean", "watch"]},
  20. {"folder": "x_component_AppMarket", "tasks": ["move", "min", "clean", "watch"]},
  21. {"folder": "x_component_Attendance", "tasks": ["move", "min", "clean", "watch"]},
  22. {"folder": "x_component_BAM", "tasks": ["move", "min", "clean", "watch"]},
  23. {"folder": "x_component_Calendar", "tasks": ["move", "min", "clean", "watch"]},
  24. {"folder": "x_component_Chat", "tasks": ["move", "min", "clean", "watch"]},
  25. {"folder": "x_component_cms_Column", "tasks": ["move", "min", "clean", "watch"]},
  26. {"folder": "x_component_cms_ColumnManager", "tasks": ["move", "min", "clean", "watch"]},
  27. {"folder": "x_component_cms_DictionaryDesigner", "tasks": ["move", "min", "clean", "watch"]},
  28. {"folder": "x_component_cms_Document", "tasks": ["move", "min", "clean", "watch"]},
  29. {"folder": "x_component_cms_FormDesigner", "tasks": ["move", "min", "clean", "watch"]},
  30. {"folder": "x_component_cms_Index", "tasks": ["move", "min", "clean", "watch"]},
  31. {"folder": "x_component_cms_Module", "tasks": ["move", "min", "clean", "watch"]},
  32. {"folder": "x_component_cms_QueryViewDesigner", "tasks": ["move", "min", "clean", "watch"]},
  33. {"folder": "x_component_cms_ScriptDesigner", "tasks": ["move", "min", "clean", "watch"]},
  34. {"folder": "x_component_cms_ViewDesigner", "tasks": ["move", "min", "clean", "watch"]},
  35. {"folder": "x_component_cms_Xform", "tasks": ["move", "min", "clean", "watch"]},
  36. {"folder": "x_component_Collect", "tasks": ["move", "min", "clean", "watch"]},
  37. {"folder": "x_component_Common", "tasks": ["move", "min", "clean", "watch"]},
  38. {"folder": "x_component_Console", "tasks": ["move", "min", "clean", "watch"]},
  39. {"folder": "x_component_ControlPanel", "tasks": ["move", "min", "clean", "watch"]},
  40. {"folder": "x_component_CRM", "tasks": ["move", "min", "clean", "watch"]},
  41. {"folder": "x_component_Deployment", "tasks": ["move", "min", "clean", "watch"]},
  42. {"folder": "x_component_DesignCenter", "tasks": ["move", "min", "clean", "watch"]},
  43. {"folder": "x_component_Empty", "tasks": ["move", "min", "clean", "watch"]},
  44. {"folder": "x_component_Execution", "tasks": ["move", "min", "clean", "watch"]},
  45. {"folder": "x_component_ExeManager", "tasks": ["move", "min", "clean", "watch"]},
  46. {"folder": "x_component_FaceSet", "tasks": ["move", "min", "clean", "watch"]},
  47. {"folder": "x_component_File", "tasks": ["move", "min", "clean", "watch"]},
  48. {"folder": "x_component_Forum", "tasks": ["move", "min", "clean", "watch"]},
  49. {"folder": "x_component_ForumCategory", "tasks": ["move", "min", "clean", "watch"]},
  50. {"folder": "x_component_ForumDocument", "tasks": ["move", "min", "clean", "watch"]},
  51. {"folder": "x_component_ForumPerson", "tasks": ["move", "min", "clean", "watch"]},
  52. {"folder": "x_component_ForumSearch", "tasks": ["move", "min", "clean", "watch"]},
  53. {"folder": "x_component_ForumSection", "tasks": ["move", "min", "clean", "watch"]},
  54. {"folder": "x_component_Homepage", "tasks": ["move", "min", "clean", "watch"]},
  55. {"folder": "x_component_HotArticle", "tasks": ["move", "min", "clean", "watch"]},
  56. {"folder": "x_component_IM", "tasks": ["move", "min", "clean", "watch"]},
  57. {"folder": "x_component_LogViewer", "tasks": ["move", "min", "clean", "watch"]},
  58. {"folder": "x_component_Meeting", "tasks": ["move", "min", "clean", "watch"]},
  59. {"folder": "x_component_Message", "tasks": ["move", "min", "clean", "watch"]},
  60. {"folder": "x_component_Minder", "tasks": ["move", "min", "clean", "watch"]},
  61. {"folder": "x_component_MinderEditor", "tasks": ["move", "min", "clean", "watch"]},
  62. {"folder": "x_component_Note", "tasks": ["move", "min", "clean", "watch"]},
  63. {"folder": "x_component_OKR", "tasks": ["move", "min", "clean", "watch"]},
  64. {"folder": "x_component_OnlineMeeting", "tasks": ["move", "min", "clean", "watch"]},
  65. {"folder": "x_component_OnlineMeetingRoom", "tasks": ["move", "min", "clean", "watch"]},
  66. {"folder": "x_component_Org", "tasks": ["move", "min", "clean", "watch"]},
  67. {"folder": "x_component_portal_PageDesigner", "tasks": ["move", "min", "clean", "watch"]},
  68. {"folder": "x_component_portal_Portal", "tasks": ["move", "min", "clean", "watch"]},
  69. {"folder": "x_component_portal_PortalExplorer", "tasks": ["move", "min", "clean", "watch"]},
  70. {"folder": "x_component_portal_PortalManager", "tasks": ["move", "min", "clean", "watch"]},
  71. {"folder": "x_component_portal_ScriptDesigner", "tasks": ["move", "min", "clean", "watch"]},
  72. {"folder": "x_component_portal_WidgetDesigner", "tasks": ["move", "min", "clean", "watch"]},
  73. {"folder": "x_component_process_Application", "tasks": ["move", "min", "clean", "watch"]},
  74. {"folder": "x_component_process_ApplicationExplorer", "tasks": ["move", "min", "clean", "watch"]},
  75. {"folder": "x_component_process_DictionaryDesigner", "tasks": ["move", "min", "clean", "watch"]},
  76. {"folder": "x_component_process_FormDesigner", "tasks": ["move", "min", "clean", "watch"]},
  77. {"folder": "x_component_process_ProcessDesigner", "tasks": ["move", "min", "clean", "watch"]},
  78. {"folder": "x_component_process_ProcessManager", "tasks": ["move", "min", "clean", "watch"]},
  79. {"folder": "x_component_process_ScriptDesigner", "tasks": ["move", "min", "clean", "watch"]},
  80. {"folder": "x_component_process_StatDesigner", "tasks": ["move", "min", "clean", "watch"]},
  81. {"folder": "x_component_process_TaskCenter", "tasks": ["move", "min", "clean", "watch"]},
  82. {"folder": "x_component_process_ViewDesigner", "tasks": ["move", "min", "clean", "watch"]},
  83. {"folder": "x_component_process_Work", "tasks": ["move", "min", "clean", "watch"]},
  84. {"folder": "x_component_process_Xform", "tasks": ["move", "min", "clean", "watch"]},
  85. {"folder": "x_component_Profile", "tasks": ["move", "min", "clean", "watch"]},
  86. {"folder": "x_component_query_Query", "tasks": ["move", "min", "clean", "watch"]},
  87. {"folder": "x_component_query_QueryExplorer", "tasks": ["move", "min", "clean", "watch"]},
  88. {"folder": "x_component_query_QueryManager", "tasks": ["move", "min", "clean", "watch"]},
  89. {"folder": "x_component_query_StatDesigner", "tasks": ["move", "min", "clean", "watch"]},
  90. {"folder": "x_component_query_ViewDesigner", "tasks": ["move", "min", "clean", "watch"]},
  91. {"folder": "x_component_query_TableDesigner", "tasks": ["move", "min", "clean", "watch"]},
  92. {"folder": "x_component_query_StatementDesigner", "tasks": ["move", "min", "clean", "watch"]},
  93. {"folder": "x_component_Report", "tasks": ["move", "min", "clean", "watch"]},
  94. {"folder": "x_component_ReportDocument", "tasks": ["move", "min", "clean", "watch"]},
  95. {"folder": "x_component_ReportMinder", "tasks": ["move", "min", "clean", "watch"]},
  96. {"folder": "x_component_ScriptEditor", "tasks": ["move", "min", "clean", "watch"]},
  97. {"folder": "x_component_Search", "tasks": ["move", "min", "clean", "watch"]},
  98. {"folder": "x_component_SelecterTest", "tasks": ["move", "min", "clean", "watch"]},
  99. {"folder": "x_component_Selector", "tasks": ["move", "min", "clean", "watch"]},
  100. {"folder": "x_component_service_AgentDesigner", "tasks": ["move", "min", "clean", "watch"]},
  101. {"folder": "x_component_service_InvokeDesigner", "tasks": ["move", "min", "clean", "watch"]},
  102. {"folder": "x_component_service_ServiceManager", "tasks": ["move", "min", "clean", "watch"]},
  103. {"folder": "x_component_Setting", "tasks": ["move", "min", "clean", "watch"]},
  104. {"folder": "x_component_SmartOfficeRoom", "tasks": ["move", "min", "clean", "watch"]},
  105. {"folder": "x_component_Snake", "tasks": ["move", "min", "clean", "watch"]},
  106. {"folder": "x_component_Strategy", "tasks": ["move", "min", "clean", "watch"]},
  107. {"folder": "x_component_Template", "tasks": ["move", "min", "clean", "watch"]},
  108. {"folder": "x_component_Weixin", "tasks": ["move", "min", "clean", "watch"]},
  109. {"folder": "x_desktop", "tasks": ["move", "min", "clean", "watch"]}
  110. ];
  111. var sourcePrefix = '../o2web/';
  112. var destDir = 'servers/webServer/';
  113. var uploadOptions = {
  114. 'location': 'E:/o2server/servers/webServer/',
  115. 'host': '',
  116. 'user': '',
  117. 'pass': '',
  118. "remotePath": "/"
  119. };
  120. var options = minimist(process.argv.slice(2), {//upload: local ftp or sftp
  121. string: ["upload", "location", "host", "user", "pass", "port", "remotePath"]
  122. });
  123. options.upload = options.upload || "";
  124. options.location = options.location || uploadOptions.location;
  125. options.host = options.host || uploadOptions.host;
  126. options.user = options.user || uploadOptions.user;
  127. options.pass = options.pass || uploadOptions.pass;
  128. options.port = options.port || 0;
  129. options.remotePath = options.remotePath || uploadOptions.remotePath;
  130. var minTasks = [];
  131. var moveTasks = [];
  132. var watchTasks = [];
  133. var cleanTasks = [];
  134. function getMinTask(path){
  135. return function(){
  136. var src = sourcePrefix+'source/'+path+'/**/*.js';
  137. var dest = destDir+path+'/';
  138. return gulp.src(src)
  139. .pipe(changed(dest))
  140. .pipe(uglify())
  141. .pipe(rename({ extname: '.min.js' }))
  142. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+path+'/')))
  143. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  144. host: options.host,
  145. user: options.user || 'anonymous',
  146. pass: options.pass || '@anonymous',
  147. port: options.port || 21,
  148. remotePath: (options.remotePath || '/')+path
  149. })))
  150. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), ftp({
  151. host: options.host,
  152. user: options.user || 'anonymous',
  153. pass: options.pass || null,
  154. port: options.port || 22,
  155. remotePath: (options.remotePath || '/')+path
  156. })))
  157. .pipe(gulp.dest(dest))
  158. .pipe(gutil.noop());
  159. }
  160. }
  161. function getMoveTask(path){
  162. return function(){
  163. var src = sourcePrefix+'source/'+path+'/**/*';
  164. var dest = destDir+path+'/';
  165. return gulp.src(src)
  166. .pipe(changed(dest))
  167. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+path+'/')))
  168. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  169. host: options.host,
  170. user: options.user || 'anonymous',
  171. pass: options.pass || '@anonymous',
  172. port: options.port || 21,
  173. remotePath: (options.remotePath || '/')+path
  174. })))
  175. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), ftp({
  176. host: options.host,
  177. user: options.user || 'anonymous',
  178. pass: options.pass || null,
  179. port: options.port || 22,
  180. remotePath: (options.remotePath || '/')+path
  181. })))
  182. .pipe(gulp.dest(dest))
  183. .pipe(gutil.noop());
  184. }
  185. }
  186. function getCleanTask(path){
  187. return function(cb){
  188. var dest = destDir+path+'/';
  189. del(dest, cb);
  190. }
  191. }
  192. function cleanRemoteFtp(f, cb){
  193. var file = options.remotePath+f;
  194. var ftp = new JSFtp({
  195. host: options.host,
  196. user: options.user || 'anonymous',
  197. pass: options.pass || null,
  198. port: options.port || 21
  199. });
  200. ftp.raw('dele '+file, function(err) {
  201. if (err){ cb(); return; }
  202. if (file.substring(file.length-3).toLowerCase()==".js"){
  203. file = file.replace('.js', ".min.js");
  204. ftp.raw('dele '+file, function(err) {
  205. if (err){ cb(); return; }
  206. if (file.indexOf("/")!=-1){
  207. var p = file.substring(0, file.lastIndexOf("/"));
  208. ftp.raw('rmd '+p, function(err) {
  209. if (err){ cb(); return; }
  210. ftp.raw.quit();
  211. cb();
  212. });
  213. }
  214. });
  215. }else{
  216. if (file.indexOf("/")!=-1){
  217. var pPath = file.substring(0, file.lastIndexOf("/"));
  218. ftp.raw('rmd '+pPath, function(err) {
  219. if (err){ cb(); return; }
  220. ftp.raw.quit();
  221. cb();
  222. });
  223. }
  224. }
  225. });
  226. }
  227. function cleanRemoteLocal(f, cb){
  228. var file = options.location+f;
  229. del(file, {force: true, dryRun: true}, function(){
  230. if (file.substring(file.length-3).toLowerCase()==".js"){
  231. var minfile = file.replace('.js', ".min.js");
  232. del(minfile, {force: true, dryRun: true}, function(){
  233. var p = file.substring(0, file.lastIndexOf("/"));
  234. fs.rmdir(p,function(err){
  235. if(err){}
  236. cb();
  237. })
  238. });
  239. }else{
  240. var p = file.substring(0, file.lastIndexOf("/"));
  241. fs.rmdir(p,function(err){
  242. if(err){}
  243. cb();
  244. })
  245. }
  246. });
  247. }
  248. function getCleanRemoteTask(path){
  249. return function(cb){
  250. if (options.upload){
  251. var file = path.replace(/\\/g, "/");
  252. file = file.substring(file.indexOf("source/")+7);
  253. if (options.upload=='local'&&options.location!='') cleanRemoteLocal(file, cb);
  254. if (options.upload=='ftp'&&options.host!='') cleanRemoteFtp(file, cb);
  255. }else{
  256. if (cb) cb();
  257. }
  258. }
  259. }
  260. function getWatchTask(path, min){
  261. return function(cb){
  262. var moveTask = "move:"+path;
  263. var minTask = "min:"+path;
  264. var cleanTask = "clean:"+path;
  265. if (min) gulp.watch('source/'+path+'/**/*.js', {"events": ['add','change']}, gulp.parallel(minTask));
  266. gulp.watch('source/'+path+'/**/*', {"events": ['addDir', 'add','change']}, gulp.parallel(moveTask));
  267. // gulp.watch('source/'+path+'/**/*', {"events": ['unlinkDir']}, function(file){
  268. // console.log("into unlinkDir watch ......."+file);
  269. // });
  270. watcher = gulp.watch('source/'+path+'/**/*', {delay:500});
  271. watcher.on('unlink', function(file, stats){
  272. console.log("into unlink watch ......."+file);
  273. gulp.task("cleanRemote", getCleanRemoteTask(file))
  274. gulp.series(gulp.parallel(cleanTask, "cleanRemote"), gulp.parallel(minTask, moveTask))();
  275. });
  276. // watcher.on('unlinkDir', function(file, stats){
  277. // console.log("into unlinkDir watch ......."+file);
  278. // // gulp.task("cleanRemoteDir", getCleanRemoteTask(file))
  279. // // gulp.series(gulp.parallel(cleanTask, "cleanRemoteDir"), gulp.parallel(minTask, moveTask))();
  280. // });
  281. }
  282. }
  283. apps.map(function(app){
  284. var taskName = "";
  285. if (app.tasks.indexOf("min")!==-1){
  286. taskName = "min:"+app.folder;
  287. minTasks.push(taskName);
  288. gulp.task(taskName, getMinTask(app.folder));
  289. }
  290. if (app.tasks.indexOf("move")!==-1){
  291. taskName = "move:"+app.folder;
  292. moveTasks.push(taskName);
  293. gulp.task(taskName, getMoveTask(app.folder));
  294. }
  295. if (app.tasks.indexOf("clean")!==-1){
  296. taskName = "clean:"+app.folder;
  297. cleanTasks.push(taskName);
  298. gulp.task(taskName, getCleanTask(app.folder));
  299. }
  300. if (app.tasks.indexOf("watch")!==-1){
  301. taskName = "watch:"+app.folder;
  302. watchTasks.push(taskName);
  303. gulp.task(taskName, getWatchTask(app.folder, (app.tasks.indexOf("min")!==-1)));
  304. }
  305. if (app.tasks.indexOf("min")!==-1 && app.tasks.indexOf("move")!==-1){
  306. gulp.task(app.folder, gulp.parallel("min:"+app.folder, "move:"+app.folder));
  307. }else if (app.tasks.indexOf("min")==-1 && app.tasks.indexOf("move")!==-1){
  308. gulp.task(app.folder, gulp.parallel("move:"+app.folder));
  309. }else if (app.tasks.indexOf("min")!==-1 && app.tasks.indexOf("move")==-1){
  310. gulp.task(app.folder, gulp.parallel("min:"+app.folder));
  311. }
  312. })
  313. gulp.task("index", function(){
  314. var src = [sourcePrefix+'source/favicon.ico', sourcePrefix+'source/index.html'];
  315. return gulp.src(src)
  316. .pipe(changed(destDir))
  317. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+'/')))
  318. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  319. host: options.host,
  320. user: options.user || 'anonymous',
  321. pass: options.pass || '@anonymous',
  322. port: options.port || 21,
  323. remotePath: (options.remotePath || '/')
  324. })))
  325. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), ftp({
  326. host: options.host,
  327. user: options.user || 'anonymous',
  328. pass: options.pass || null,
  329. port: options.port || 22,
  330. remotePath: (options.remotePath || '/')
  331. })))
  332. .pipe(gulp.dest(destDir))
  333. .pipe(gutil.noop());
  334. });
  335. //gulp.task("default", gulp.parallel(minTasks, moveTasks, "index"));
  336. gulp.task("clean", gulp.series(cleanTasks));
  337. gulp.task("sync", gulp.series(
  338. gulp.series(cleanTasks),
  339. gulp.parallel(minTasks, moveTasks, "index")
  340. ));
  341. gulp.task("watch", gulp.parallel(watchTasks));
  342. gulp.task("o2:new-v:html", function () {
  343. var path = "x_desktop";
  344. var src = sourcePrefix+'source/'+path+'/*.html';
  345. var dest = destDir+'x_desktop/';
  346. return gulp.src(src)
  347. .pipe(assetRev())
  348. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+path+'/')))
  349. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  350. host: options.host,
  351. user: options.user || 'anonymous',
  352. pass: options.pass || '@anonymous',
  353. port: options.port || 21,
  354. remotePath: (options.remotePath || '/')+path
  355. })))
  356. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), sftp({
  357. host: options.host,
  358. user: options.user || 'anonymous',
  359. pass: options.pass || null,
  360. port: options.port || 22,
  361. remotePath: (options.remotePath || '/')+path
  362. })))
  363. .pipe(gulp.dest(dest))
  364. .pipe(gutil.noop());
  365. });
  366. gulp.task("o2:new-v:o2", function () {
  367. var path = "o2_core";
  368. var src = sourcePrefix+'source/'+path+'/o2.js';
  369. var dest = destDir+'o2_core/';
  370. return gulp.src(src)
  371. .pipe(assetRev())
  372. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+path+'/')))
  373. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  374. host: options.host,
  375. user: options.user || 'anonymous',
  376. pass: options.pass || '@anonymous',
  377. port: options.port || 21,
  378. remotePath: (options.remotePath || '/')+path
  379. })))
  380. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), sftp({
  381. host: options.host,
  382. user: options.user || 'anonymous',
  383. pass: options.pass || null,
  384. port: options.port || 22,
  385. remotePath: (options.remotePath || '/')+path
  386. })))
  387. .pipe(gulp.dest(dest))
  388. .pipe(uglify())
  389. .pipe(rename({ extname: '.min.js' }))
  390. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+path+'/')))
  391. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  392. host: options.host,
  393. user: options.user || 'anonymous',
  394. pass: options.pass || '@anonymous',
  395. port: options.port || 21,
  396. remotePath: (options.remotePath || '/')+path
  397. })))
  398. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), sftp({
  399. host: options.host,
  400. user: options.user || 'anonymous',
  401. pass: options.pass || null,
  402. port: options.port || 22,
  403. remotePath: (options.remotePath || '/')+path
  404. })))
  405. .pipe(gulp.dest(dest))
  406. .pipe(gutil.noop());
  407. });
  408. gulp.task("o2:new-v", gulp.parallel("o2:new-v:o2", "o2:new-v:html"));
  409. gulp.task("default", gulp.series("clean", gulp.parallel(minTasks, moveTasks, 'index'), "o2:new-v"));