gulpfile.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. var gulp = require('gulp'),
  2. //var deleted = require('gulp-deleted');
  3. del = require('del'),
  4. uglify = require('gulp-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 apps = [
  15. {"folder": "o2_lib", "tasks": ["move", "clean"]},
  16. {"folder": "o2_core", "tasks": ["move", "min", "clean", "watch"]},
  17. {"folder": "x_desktop", "tasks": ["move", "min", "clean", "watch"]},
  18. {"folder": "x_component_Common", "tasks": ["move", "min", "clean", "watch"]},
  19. {"folder": "x_component_Template", "tasks": ["move", "min", "clean", "watch"]},
  20. {"folder": "x_component_AppCenter", "tasks": ["move", "min", "clean", "watch"]},
  21. {"folder": "x_component_AppMarket", "tasks": ["move", "min", "clean", "watch"]},
  22. {"folder": "x_component_Attendance", "tasks": ["move", "min", "clean", "watch"]},
  23. {"folder": "x_component_BAM", "tasks": ["move", "min", "clean", "watch"]},
  24. {"folder": "x_component_Calendar", "tasks": ["move", "min", "clean", "watch"]},
  25. {"folder": "x_component_Chat", "tasks": ["move", "min", "clean", "watch"]},
  26. {"folder": "x_component_cms_Column", "tasks": ["move", "min", "clean", "watch"]},
  27. {"folder": "x_component_cms_ColumnManager", "tasks": ["move", "min", "clean", "watch"]},
  28. {"folder": "x_component_cms_DictionaryDesigner", "tasks": ["move", "min", "clean", "watch"]},
  29. {"folder": "x_component_cms_Document", "tasks": ["move", "min", "clean", "watch"]},
  30. {"folder": "x_component_cms_FormDesigner", "tasks": ["move", "min", "clean", "watch"]},
  31. {"folder": "x_component_cms_Index", "tasks": ["move", "min", "clean", "watch"]},
  32. {"folder": "x_component_cms_Module", "tasks": ["move", "min", "clean", "watch"]},
  33. {"folder": "x_component_cms_QueryViewDesigner", "tasks": ["move", "min", "clean", "watch"]},
  34. {"folder": "x_component_cms_ScriptDesigner", "tasks": ["move", "min", "clean", "watch"]},
  35. {"folder": "x_component_cms_ViewDesigner", "tasks": ["move", "min", "clean", "watch"]},
  36. {"folder": "x_component_cms_Xform", "tasks": ["move", "min", "clean", "watch"]},
  37. {"folder": "x_component_Collect", "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_HotArticle", "tasks": ["move", "min", "clean", "watch"]},
  55. {"folder": "x_component_IM", "tasks": ["move", "min", "clean", "watch"]},
  56. {"folder": "x_component_LogViewer", "tasks": ["move", "min", "clean", "watch"]},
  57. {"folder": "x_component_Meeting", "tasks": ["move", "min", "clean", "watch"]},
  58. {"folder": "x_component_Message", "tasks": ["move", "min", "clean", "watch"]},
  59. {"folder": "x_component_Minder", "tasks": ["move", "min", "clean", "watch"]},
  60. {"folder": "x_component_MinderEditor", "tasks": ["move", "min", "clean", "watch"]},
  61. {"folder": "x_component_Note", "tasks": ["move", "min", "clean", "watch"]},
  62. {"folder": "x_component_OKR", "tasks": ["move", "min", "clean", "watch"]},
  63. {"folder": "x_component_OnlineMeeting", "tasks": ["move", "min", "clean", "watch"]},
  64. {"folder": "x_component_OnlineMeetingRoom", "tasks": ["move", "min", "clean", "watch"]},
  65. {"folder": "x_component_Org", "tasks": ["move", "min", "clean", "watch"]},
  66. {"folder": "x_component_portal_PageDesigner", "tasks": ["move", "min", "clean", "watch"]},
  67. {"folder": "x_component_portal_Portal", "tasks": ["move", "min", "clean", "watch"]},
  68. {"folder": "x_component_portal_PortalExplorer", "tasks": ["move", "min", "clean", "watch"]},
  69. {"folder": "x_component_portal_PortalManager", "tasks": ["move", "min", "clean", "watch"]},
  70. {"folder": "x_component_portal_ScriptDesigner", "tasks": ["move", "min", "clean", "watch"]},
  71. {"folder": "x_component_portal_WidgetDesigner", "tasks": ["move", "min", "clean", "watch"]},
  72. {"folder": "x_component_process_Application", "tasks": ["move", "min", "clean", "watch"]},
  73. {"folder": "x_component_process_ApplicationExplorer", "tasks": ["move", "min", "clean", "watch"]},
  74. {"folder": "x_component_process_DictionaryDesigner", "tasks": ["move", "min", "clean", "watch"]},
  75. {"folder": "x_component_process_FormDesigner", "tasks": ["move", "min", "clean", "watch"]},
  76. {"folder": "x_component_process_ProcessDesigner", "tasks": ["move", "min", "clean", "watch"]},
  77. {"folder": "x_component_process_ProcessManager", "tasks": ["move", "min", "clean", "watch"]},
  78. {"folder": "x_component_process_ScriptDesigner", "tasks": ["move", "min", "clean", "watch"]},
  79. {"folder": "x_component_process_StatDesigner", "tasks": ["move", "min", "clean", "watch"]},
  80. {"folder": "x_component_process_TaskCenter", "tasks": ["move", "min", "clean", "watch"]},
  81. {"folder": "x_component_process_ViewDesigner", "tasks": ["move", "min", "clean", "watch"]},
  82. {"folder": "x_component_process_Work", "tasks": ["move", "min", "clean", "watch"]},
  83. {"folder": "x_component_process_Xform", "tasks": ["move", "min", "clean", "watch"]},
  84. {"folder": "x_component_Profile", "tasks": ["move", "min", "clean", "watch"]},
  85. {"folder": "x_component_query_Query", "tasks": ["move", "min", "clean", "watch"]},
  86. {"folder": "x_component_query_QueryExplorer", "tasks": ["move", "min", "clean", "watch"]},
  87. {"folder": "x_component_query_QueryManager", "tasks": ["move", "min", "clean", "watch"]},
  88. {"folder": "x_component_query_StatDesigner", "tasks": ["move", "min", "clean", "watch"]},
  89. {"folder": "x_component_query_ViewDesigner", "tasks": ["move", "min", "clean", "watch"]},
  90. {"folder": "x_component_Report", "tasks": ["move", "min", "clean", "watch"]},
  91. {"folder": "x_component_ReportDocument", "tasks": ["move", "min", "clean", "watch"]},
  92. {"folder": "x_component_ReportMinder", "tasks": ["move", "min", "clean", "watch"]},
  93. {"folder": "x_component_ScriptEditor", "tasks": ["move", "min", "clean", "watch"]},
  94. {"folder": "x_component_Search", "tasks": ["move", "min", "clean", "watch"]},
  95. {"folder": "x_component_SelecterTest", "tasks": ["move", "min", "clean", "watch"]},
  96. {"folder": "x_component_Selector", "tasks": ["move", "min", "clean", "watch"]},
  97. {"folder": "x_component_service_AgentDesigner", "tasks": ["move", "min", "clean", "watch"]},
  98. {"folder": "x_component_service_InvokeDesigner", "tasks": ["move", "min", "clean", "watch"]},
  99. {"folder": "x_component_service_ServiceManager", "tasks": ["move", "min", "clean", "watch"]},
  100. {"folder": "x_component_Setting", "tasks": ["move", "min", "clean", "watch"]},
  101. {"folder": "x_component_Strategy", "tasks": ["move", "min", "clean", "watch"]},
  102. {"folder": "x_component_Weixin", "tasks": ["move", "min", "clean", "watch"]},
  103. ];
  104. var uploadOptions = {
  105. 'location': 'E:/o2server/servers/webServer/',
  106. 'host': 'dev.o2oa.net',
  107. 'user': 'xadmin',
  108. 'pass': 'xadmin',
  109. "remotePath": "/"
  110. }
  111. var options = minimist(process.argv.slice(2), {//upload: local ftp or sftp
  112. string: ["upload", "location", "host", "user", "pass", "port", "remotePath"]
  113. });
  114. options.upload = options.upload || "";
  115. options.location = options.location || uploadOptions.location;
  116. options.host = options.host || uploadOptions.host;
  117. options.user = options.user || uploadOptions.user;
  118. options.pass = options.pass || uploadOptions.pass;
  119. options.port = options.port || 0;
  120. options.remotePath = options.remotePath || uploadOptions.remotePath;
  121. var minTasks = []; //压缩修改过的js文件,更名为 *.min.js,并移动到编译目录(dest)
  122. var moveTasks = []; //将修改过的文件移动到编译目录(dest)
  123. var watchTasks = []; //监控任务
  124. var cleanTasks = [];
  125. function getMinTask(path){
  126. return function(){
  127. var src = 'source/'+path+'/**/*.js';
  128. var dest = 'dest/'+path+'/';
  129. return gulp.src(src)
  130. .pipe(changed(dest))
  131. .pipe(uglify())
  132. .pipe(rename({ extname: '.min.js' }))
  133. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+path+'/')))
  134. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  135. host: options.host,
  136. user: options.user || 'anonymous',
  137. pass: options.pass || '@anonymous',
  138. port: options.port || 21,
  139. remotePath: (options.remotePath || '/')+path
  140. })))
  141. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), ftp({
  142. host: options.host,
  143. user: options.user || 'anonymous',
  144. pass: options.pass || null,
  145. port: options.port || 22,
  146. remotePath: (options.remotePath || '/')+path
  147. })))
  148. .pipe(gulp.dest(dest))
  149. .pipe(gutil.noop());
  150. }
  151. }
  152. function getMoveTask(path){
  153. return function(){
  154. var src = 'source/'+path+'/**/*';
  155. var dest = 'dest/'+path+'/';
  156. return gulp.src(src)
  157. .pipe(changed(dest))
  158. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+path+'/')))
  159. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  160. host: options.host,
  161. user: options.user || 'anonymous',
  162. pass: options.pass || '@anonymous',
  163. port: options.port || 21,
  164. remotePath: (options.remotePath || '/')+path
  165. })))
  166. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), ftp({
  167. host: options.host,
  168. user: options.user || 'anonymous',
  169. pass: options.pass || null,
  170. port: options.port || 22,
  171. remotePath: (options.remotePath || '/')+path
  172. })))
  173. .pipe(gulp.dest(dest))
  174. .pipe(gutil.noop());
  175. }
  176. }
  177. function getCleanTask(path){
  178. return function(cb){
  179. var dest = 'dest/'+path+'/';
  180. del(dest, cb);
  181. }
  182. }
  183. function cleanRemoteFtp(f, cb){
  184. var file = options.remotePath+f;
  185. var ftp = new JSFtp({
  186. host: options.host,
  187. user: options.user || 'anonymous',
  188. pass: options.pass || null,
  189. port: options.port || 21,
  190. });
  191. ftp.raw('dele '+file, function(err) {
  192. if (err){ cb(); return; }
  193. if (file.substring(file.length-3).toLowerCase()==".js"){
  194. file = file.replace('.js', ".min.js");
  195. ftp.raw('dele '+file, function(err) {
  196. if (err){ cb(); return; }
  197. if (file.indexOf("/")!=-1){
  198. var p = file.substring(0, file.lastIndexOf("/"));
  199. ftp.raw('rmd '+p, function(err) {
  200. if (err){ cb(); return; }
  201. ftp.raw.quit();
  202. cb();
  203. });
  204. }
  205. });
  206. }else{
  207. if (file.indexOf("/")!=-1){
  208. var pPath = file.substring(0, file.lastIndexOf("/"));
  209. ftp.raw('rmd '+pPath, function(err) {
  210. if (err){ cb(); return; }
  211. ftp.raw.quit();
  212. cb();
  213. });
  214. }
  215. }
  216. });
  217. }
  218. function cleanRemoteLocal(f, cb){
  219. var file = options.location+f;
  220. del(file, {force: true, dryRun: true}, function(){
  221. if (file.substring(file.length-3).toLowerCase()==".js"){
  222. var minfile = file.replace('.js', ".min.js");
  223. del(minfile, {force: true, dryRun: true}, function(){
  224. var p = file.substring(0, file.lastIndexOf("/"));
  225. fs.rmdir(p,function(err){
  226. if(err){}
  227. cb();
  228. })
  229. });
  230. }else{
  231. var p = file.substring(0, file.lastIndexOf("/"));
  232. fs.rmdir(p,function(err){
  233. if(err){}
  234. cb();
  235. })
  236. }
  237. });
  238. }
  239. function getCleanRemoteTask(path){
  240. return function(cb){
  241. if (options.upload){
  242. var file = path.replace(/\\/g, "/");
  243. file = file.substring(file.indexOf("source/")+7);
  244. if (options.upload=='local'&&options.location!='') cleanRemoteLocal(file, cb);
  245. if (options.upload=='ftp'&&options.host!='') cleanRemoteFtp(file, cb);
  246. }else{
  247. if (cb) cb();
  248. }
  249. }
  250. }
  251. function getWatchTask(path, min){
  252. return function(cb){
  253. var moveTask = "move:"+path;
  254. var minTask = "min:"+path;
  255. var cleanTask = "clean:"+path;
  256. if (min) gulp.watch('source/'+path+'/**/*.js', {"events": ['add','change']}, gulp.parallel(minTask));
  257. gulp.watch('source/'+path+'/**/*', {"events": ['addDir', 'add','change']}, gulp.parallel(moveTask));
  258. // gulp.watch('source/'+path+'/**/*', {"events": ['unlinkDir']}, function(file){
  259. // console.log("into unlinkDir watch ......."+file);
  260. // });
  261. watcher = gulp.watch('source/'+path+'/**/*', {delay:500});
  262. watcher.on('unlink', function(file, stats){
  263. console.log("into unlink watch ......."+file);
  264. gulp.task("cleanRemote", getCleanRemoteTask(file))
  265. gulp.series(gulp.parallel(cleanTask, "cleanRemote"), gulp.parallel(minTask, moveTask))();
  266. });
  267. // watcher.on('unlinkDir', function(file, stats){
  268. // console.log("into unlinkDir watch ......."+file);
  269. // // gulp.task("cleanRemoteDir", getCleanRemoteTask(file))
  270. // // gulp.series(gulp.parallel(cleanTask, "cleanRemoteDir"), gulp.parallel(minTask, moveTask))();
  271. // });
  272. }
  273. }
  274. apps.map(function(app){
  275. var taskName = "";
  276. if (app.tasks.indexOf("min")!==-1){
  277. taskName = "min:"+app.folder;
  278. minTasks.push(taskName);
  279. gulp.task(taskName, getMinTask(app.folder));
  280. }
  281. if (app.tasks.indexOf("move")!==-1){
  282. taskName = "move:"+app.folder;
  283. moveTasks.push(taskName);
  284. gulp.task(taskName, getMoveTask(app.folder));
  285. }
  286. if (app.tasks.indexOf("clean")!==-1){
  287. taskName = "clean:"+app.folder;
  288. cleanTasks.push(taskName);
  289. gulp.task(taskName, getCleanTask(app.folder));
  290. }
  291. if (app.tasks.indexOf("watch")!==-1){
  292. taskName = "watch:"+app.folder;
  293. watchTasks.push(taskName);
  294. gulp.task(taskName, getWatchTask(app.folder, (app.tasks.indexOf("min")!==-1)));
  295. }
  296. })
  297. gulp.task("default", gulp.parallel(minTasks, moveTasks));
  298. gulp.task("clean", gulp.series(cleanTasks));
  299. gulp.task("sync", gulp.series(
  300. gulp.series(cleanTasks),
  301. gulp.parallel(minTasks, moveTasks)
  302. ));
  303. gulp.task("watch", gulp.parallel(watchTasks));
  304. // gulp.task("watch", function(){
  305. // watcher = gulp.watch('source/x_desktop/**/*');
  306. // var log = console.log.bind(console);
  307. // watcher
  308. // .on('add', function(path) { log('File', path, 'has been added'); })
  309. // .on('addDir', function(path) { log('Directory', path, 'has been added'); })
  310. // .on('change', function(path) { log('File', path, 'has been changed'); })
  311. // .on('unlink', function(path) { log('File', path, 'has been removed'); })
  312. // .on('unlinkDir', function(path) { log('Directory', path, 'has been removed'); })
  313. // .on('error', function(error) { log('Error happened', error); })
  314. // .on('ready', function() { log('Initial scan complete. Ready for changes.'); })
  315. // //.on('raw', function(event, path, details) { log('Raw event info:', event, path, details); })
  316. // });
  317. gulp.task("upload", function(cb){
  318. var dest = 'dest/**/*';
  319. console.log(options.upload);
  320. console.log(options.user);
  321. console.log(options.pass);
  322. console.log(options.host);
  323. console.log(options.remotePath);
  324. gulp.src(dest)
  325. .pipe(gulpif((options.upload=='local'&&options.location!=''), gulp.dest(options.location+'/')))
  326. .pipe(gulpif((options.upload=='ftp'&&options.host!=''), ftp({
  327. host: options.host,
  328. user: options.user || 'anonymous',
  329. pass: options.pass || '@anonymous',
  330. port: options.port || 21,
  331. remotePath: (options.remotePath || '/')
  332. })))
  333. .pipe(gulpif((options.upload=='sftp'&&options.host!=''), ftp({
  334. host: options.host,
  335. user: options.user || 'anonymous',
  336. pass: options.pass || null,
  337. port: options.port || 22,
  338. remotePath: (options.remotePath || '/')
  339. })));
  340. cb();
  341. });