gulpfile.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. var gulp = require('gulp'),
  2. //var deleted = require('gulp-deleted');
  3. del = require('del'),
  4. //uglify = require('gulp-tm-uglify'),
  5. uglify = require('gulp-uglify-es').default,
  6. rename = require('gulp-rename'),
  7. changed = require('gulp-changed'),
  8. gulpif = require('gulp-if'),
  9. minimist = require('minimist'),
  10. ftp = require('gulp-ftp'),
  11. sftp = require('gulp-sftp-up4'),
  12. JSFtp = require('jsftp'),
  13. gutil = require('gulp-util'),
  14. fs = require("fs");
  15. concat = require('gulp-concat');
  16. //let uglify = require('gulp-uglify-es').default;
  17. var through2 = require('through2');
  18. var assetRev = require('gulp-tm-asset-rev');
  19. var apps = require('./gulpapps.js');
  20. var ftpconfig = require('./gulpconfig.js');
  21. var o_options = minimist(process.argv.slice(2), {//upload: local ftp or sftp
  22. string: ["ev", "upload", "location", "host", "user", "pass", "port", "remotePath", "dest", "src"]
  23. });
  24. function getEvOptions(ev){
  25. options.ev = ev;
  26. return (ftpconfig[ev]) || {
  27. 'location': '',
  28. 'host': '',
  29. 'user': '',
  30. 'pass': '',
  31. "port": null,
  32. "remotePath": "",
  33. "dest": "dest",
  34. "upload": ""
  35. }
  36. }
  37. function setOptions(op1, op2){
  38. if (!op2) op2 = {};
  39. options.upload = op1.upload || op2.upload || "";
  40. options.location = op1.location || op2.location || "";
  41. options.host = op1.host || op2.host || "";
  42. options.user = op1.user || op2.user || "";
  43. options.pass = op1.pass || op2.pass || "";
  44. options.port = op1.port || op2.port || "";
  45. options.remotePath = op1.remotePath || op2.remotePath || "";
  46. options.dest = op1.dest || op2.dest || "dest";
  47. }
  48. var options = {};
  49. setOptions(o_options, getEvOptions(o_options.ev));
  50. var appTasks = [];
  51. function createDefaultTask(path, isMin, thisOptions) {
  52. gulp.task(path, function (cb) {
  53. //var srcFile = 'source/' + path + '/**/*';
  54. var option = thisOptions || options;
  55. var src;
  56. var dest = option.dest+'/' + path + '/';
  57. let ev = option.ev
  58. var evList = Object.keys(ftpconfig).map((i)=>{ return (i==ev) ? "*"+i : i; });
  59. if (isMin){
  60. var src_min = ['source/' + path + '/**/*.js', '!**/*.spec.js', '!**/test/**'];
  61. var src_move = ['source/' + path + '/**/*', '!**/*.spec.js', '!**/test/**'];
  62. gutil.log("Move-Uglify", ":", gutil.colors.green(gutil.colors.blue(path), gutil.colors.white('->'), dest));
  63. return gulp.src(src_min)
  64. .pipe(changed(dest))
  65. .pipe(uglify())
  66. .pipe(rename({ extname: '.min.js' }))
  67. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  68. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  69. host: option.host,
  70. user: option.user || 'anonymous',
  71. pass: option.pass || '@anonymous',
  72. port: option.port || 21,
  73. remotePath: (option.remotePath || '/') + path
  74. })))
  75. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  76. host: option.host,
  77. user: option.user || 'anonymous',
  78. pass: option.pass || null,
  79. port: option.port || 22,
  80. remotePath: (option.remotePath || '/') + path
  81. })))
  82. .pipe(gulpif((option.ev == "dev" || option.ev == "pro") ,gulp.dest(dest)))
  83. .pipe(gulp.src(src_move))
  84. .pipe(changed(dest))
  85. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  86. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  87. host: option.host,
  88. user: option.user || 'anonymous',
  89. pass: option.pass || '@anonymous',
  90. port: option.port || 21,
  91. remotePath: (option.remotePath || '/') + path
  92. })))
  93. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  94. host: option.host,
  95. user: option.user || 'anonymous',
  96. pass: option.pass || null,
  97. port: option.port || 22,
  98. remotePath: (option.remotePath || '/') + path
  99. })))
  100. .pipe(gulp.dest(dest))
  101. .pipe(gutil.noop());
  102. }else{
  103. src = ['source/' + path + '/**/*', '!**/*.spec.js', '!**/test/**'];
  104. gutil.log("Move", ":", gutil.colors.green(gutil.colors.blue(path), gutil.colors.white('->'), dest));
  105. return gulp.src(src)
  106. .pipe(changed(dest))
  107. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  108. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  109. host: option.host,
  110. user: option.user || 'anonymous',
  111. pass: option.pass || '@anonymous',
  112. port: option.port || 21,
  113. remotePath: (option.remotePath || '/') + path
  114. })))
  115. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  116. host: option.host,
  117. user: option.user || 'anonymous',
  118. pass: option.pass || null,
  119. port: option.port || 22,
  120. remotePath: (option.remotePath || '/') + path
  121. })))
  122. .pipe(gulp.dest(dest))
  123. .pipe(gutil.noop());
  124. }
  125. });
  126. }
  127. function createXFormConcatTask(path, isMin, thisOptions) {
  128. gulp.task(path+" : concat", function(){
  129. var option = thisOptions || options;
  130. var src = [
  131. 'source/o2_core/o2/widget/AttachmentController.js',
  132. 'source/o2_core/o2/xScript/Macro.js',
  133. 'source/o2_core/o2/widget/Tab.js',
  134. 'source/o2_core/o2/widget/O2Identity.js',
  135. 'source/' + path + '/Form.js',
  136. 'source/' + path + '/$Module.js',
  137. 'source/' + path + '/$Input.js',
  138. 'source/' + path + '/Div.js',
  139. 'source/' + path + '/Combox.js',
  140. 'source/' + path + '/DatagridMobile.js',
  141. 'source/' + path + '/DatagridPC.js',
  142. 'source/' + path + '/Textfield.js',
  143. 'source/' + path + '/Personfield.js',
  144. 'source/' + path + '/*.js',
  145. 'source/x_component_process_Work/Processor.js',
  146. '!source/' + path + '/Office.js'
  147. ];
  148. var dest = option.dest+'/' + path + '/';
  149. return gulp.src(src)
  150. .pipe(concat('$all.js'))
  151. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  152. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  153. host: option.host,
  154. user: option.user || 'anonymous',
  155. pass: option.pass || '@anonymous',
  156. port: option.port || 21,
  157. remotePath: (option.remotePath || '/') + path
  158. })))
  159. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  160. host: option.host,
  161. user: option.user || 'anonymous',
  162. pass: option.pass || null,
  163. port: option.port || 22,
  164. remotePath: (option.remotePath || '/') + path
  165. })))
  166. .pipe(gulp.dest(dest))
  167. .pipe(uglify())
  168. .pipe(rename({ extname: '.min.js' }))
  169. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  170. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  171. host: option.host,
  172. user: option.user || 'anonymous',
  173. pass: option.pass || '@anonymous',
  174. port: option.port || 21,
  175. remotePath: (option.remotePath || '/') + path
  176. })))
  177. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  178. host: option.host,
  179. user: option.user || 'anonymous',
  180. pass: option.pass || null,
  181. port: option.port || 22,
  182. remotePath: (option.remotePath || '/') + path
  183. })))
  184. .pipe(gulp.dest(dest))
  185. });
  186. }
  187. function createO2ConcatTask(path, isMin, thisOptions) {
  188. gulp.task(path+" : concat", function(){
  189. var option = thisOptions || options;
  190. var src = [
  191. 'source/o2_lib/mootools/mootools-1.6.0_all.js',
  192. 'source/o2_lib/mootools/plugin/mBox.js',
  193. 'source/' + path + '/o2.js'
  194. ];
  195. var dest = option.dest+'/' + path + '/';
  196. return gulp.src(src)
  197. .pipe(concat('o2.js'))
  198. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  199. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  200. host: option.host,
  201. user: option.user || 'anonymous',
  202. pass: option.pass || '@anonymous',
  203. port: option.port || 21,
  204. remotePath: (option.remotePath || '/') + path
  205. })))
  206. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  207. host: option.host,
  208. user: option.user || 'anonymous',
  209. pass: option.pass || null,
  210. port: option.port || 22,
  211. remotePath: (option.remotePath || '/') + path
  212. })))
  213. .pipe(gulp.dest(dest))
  214. .pipe(uglify())
  215. .pipe(rename({ extname: '.min.js' }))
  216. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  217. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  218. host: option.host,
  219. user: option.user || 'anonymous',
  220. pass: option.pass || '@anonymous',
  221. port: option.port || 21,
  222. remotePath: (option.remotePath || '/') + path
  223. })))
  224. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  225. host: option.host,
  226. user: option.user || 'anonymous',
  227. pass: option.pass || null,
  228. port: option.port || 22,
  229. remotePath: (option.remotePath || '/') + path
  230. })))
  231. .pipe(gulp.dest(dest))
  232. });
  233. gulp.task(path+".xDesktop : concat", function(){
  234. var option = thisOptions || options;
  235. var src = [
  236. 'source/'+path+'/o2/widget/Common.js',
  237. 'source/'+path+'/o2/widget/Dialog.js',
  238. 'source/'+path+'/o2/widget/UUID.js',
  239. 'source/'+path+'/o2/xDesktop/Common.js',
  240. 'source/'+path+'/o2/xDesktop/Actions/RestActions.js',
  241. 'source/'+path+'/o2/xAction/RestActions.js',
  242. 'source/'+path+'/o2/xDesktop/Access.js',
  243. 'source/'+path+'/o2/xDesktop/Dialog.js',
  244. 'source/'+path+'/o2/xDesktop/Menu.js',
  245. 'source/'+path+'/o2/xDesktop/UserData.js',
  246. 'source/x_component_Template/MPopupForm.js',
  247. 'source/'+path+'/o2/xDesktop/Authentication.js',
  248. 'source/'+path+'/o2/xDesktop/Dialog.js',
  249. 'source/'+path+'/o2/xDesktop/Window.js',
  250. 'source/x_component_Common/Main.js'
  251. ];
  252. var dest = option.dest+'/' + path + '/o2/xDesktop/';
  253. return gulp.src(src)
  254. .pipe(concat('$all.js'))
  255. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/o2/xDesktop/')))
  256. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  257. host: option.host,
  258. user: option.user || 'anonymous',
  259. pass: option.pass || '@anonymous',
  260. port: option.port || 21,
  261. remotePath: (option.remotePath || '/') + path+"/o2/xDesktop/"
  262. })))
  263. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  264. host: option.host,
  265. user: option.user || 'anonymous',
  266. pass: option.pass || null,
  267. port: option.port || 22,
  268. remotePath: (option.remotePath || '/') + path+"/o2/xDesktop/"
  269. })))
  270. .pipe(gulp.dest(dest))
  271. .pipe(uglify())
  272. .pipe(rename({ extname: '.min.js' }))
  273. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/o2/xDesktop/')))
  274. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  275. host: option.host,
  276. user: option.user || 'anonymous',
  277. pass: option.pass || '@anonymous',
  278. port: option.port || 21,
  279. remotePath: (option.remotePath || '/') + path+"/o2/xDesktop/"
  280. })))
  281. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  282. host: option.host,
  283. user: option.user || 'anonymous',
  284. pass: option.pass || null,
  285. port: option.port || 22,
  286. remotePath: (option.remotePath || '/') + path+"/o2/xDesktop/"
  287. })))
  288. .pipe(gulp.dest(dest))
  289. });
  290. gulp.task(path+" : bundle", function(){
  291. var option = thisOptions || options;
  292. var src = [
  293. 'source/o2_lib/mootools/mootools-1.6.0_all.js',
  294. 'source/o2_lib/mootools/plugin/mBox.js',
  295. 'source/' + path + '/o2.js',
  296. 'source/x_desktop/js/base.js',
  297. "source/o2_core/o2/framework.js"
  298. ];
  299. var dest = option.dest+'/' + path + '/';
  300. return gulp.src(src)
  301. .pipe(concat('bundle.js'))
  302. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  303. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  304. host: option.host,
  305. user: option.user || 'anonymous',
  306. pass: option.pass || '@anonymous',
  307. port: option.port || 21,
  308. remotePath: (option.remotePath || '/') + path
  309. })))
  310. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  311. host: option.host,
  312. user: option.user || 'anonymous',
  313. pass: option.pass || null,
  314. port: option.port || 22,
  315. remotePath: (option.remotePath || '/') + path
  316. })))
  317. .pipe(gulp.dest(dest))
  318. .pipe(uglify())
  319. .pipe(rename({ extname: '.min.js' }))
  320. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  321. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  322. host: option.host,
  323. user: option.user || 'anonymous',
  324. pass: option.pass || '@anonymous',
  325. port: option.port || 21,
  326. remotePath: (option.remotePath || '/') + path
  327. })))
  328. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  329. host: option.host,
  330. user: option.user || 'anonymous',
  331. pass: option.pass || null,
  332. port: option.port || 22,
  333. remotePath: (option.remotePath || '/') + path
  334. })))
  335. .pipe(gulp.dest(dest))
  336. });
  337. }
  338. function createBaseWorkConcatTask(path, isMin, thisOptions) {
  339. gulp.task(path+".base_work : concat", function(){
  340. var option = thisOptions || options;
  341. var src = [
  342. 'source/' + path + '/js/base_work_begin.js',
  343. 'source/o2_core/o2/widget/Common.js',
  344. 'source/o2_core/o2/widget/Dialog.js',
  345. 'source/o2_core/o2/widget/UUID.js',
  346. 'source/o2_core/o2/widget/Menu.js',
  347. 'source/o2_core/o2/widget/Toolbar.js',
  348. 'source/o2_core/o2/xDesktop/Common.js',
  349. 'source/o2_core/o2/xDesktop/Actions/RestActions.js',
  350. 'source/o2_core/o2/xAction/RestActions.js',
  351. 'source/o2_core/o2/xDesktop/Access.js',
  352. 'source/o2_core/o2/xDesktop/Dialog.js',
  353. 'source/o2_core/o2/xDesktop/Menu.js',
  354. 'source/o2_core/o2/xDesktop/UserData.js',
  355. 'source/x_component_Template/MPopupForm.js',
  356. 'source/o2_core/o2/xDesktop/Authentication.js',
  357. 'source/o2_core/o2/xDesktop/Dialog.js',
  358. 'source/o2_core/o2/xDesktop/Window.js',
  359. 'source/x_component_Common/Main.js',
  360. 'source/x_component_process_Work/Main.js',
  361. 'source/x_component_Selector/package.js',
  362. 'source/x_component_Selector/Person.js',
  363. 'source/x_component_Selector/Identity.js',
  364. 'source/x_component_Selector/Unit.js',
  365. 'source/x_component_Selector/IdentityWidthDuty.js',
  366. 'source/x_component_Selector/IdentityWidthDutyCategoryByUnit.js',
  367. 'source/x_component_Selector/UnitWithType.js',
  368. 'source/o2_core/o2/xScript/Actions/UnitActions.js',
  369. 'source/o2_core/o2/xScript/Actions/ScriptActions.js',
  370. 'source/o2_core/o2/xScript/Actions/CMSScriptActions.js',
  371. 'source/o2_core/o2/xScript/Actions/PortalScriptActions.js',
  372. 'source/o2_core/o2/xScript/Environment.js',
  373. 'source/x_component_Template/MTooltips.js',
  374. 'source/x_component_Template/MSelector.js',
  375. 'source/o2_core/o2/xAction/services/x_organization_assemble_authentication.js',
  376. 'source/o2_core/o2/xAction/services/x_processplatform_assemble_surface.js',
  377. 'source/o2_core/o2/xAction/services/x_cms_assemble_control.js',
  378. 'source/o2_core/o2/xAction/services/x_organization_assemble_control.js',
  379. 'source/o2_core/o2/xAction/services/x_query_assemble_surface.js',
  380. 'source/o2_core/o2/xAction/services/x_organization_assemble_personal.js',
  381. 'source/' + path + '/js/base_work_end.js',
  382. 'source/' + path + '/js/base.js'
  383. ];
  384. var dest = option.dest+'/' + path + '/';
  385. return gulp.src(src)
  386. .pipe(concat('js/base_work.js'))
  387. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  388. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  389. host: option.host,
  390. user: option.user || 'anonymous',
  391. pass: option.pass || '@anonymous',
  392. port: option.port || 21,
  393. remotePath: (option.remotePath || '/') + path
  394. })))
  395. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  396. host: option.host,
  397. user: option.user || 'anonymous',
  398. pass: option.pass || null,
  399. port: option.port || 22,
  400. remotePath: (option.remotePath || '/') + path
  401. })))
  402. .pipe(gulp.dest(dest))
  403. .pipe(uglify())
  404. .pipe(rename({ extname: '.min.js' }))
  405. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  406. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  407. host: option.host,
  408. user: option.user || 'anonymous',
  409. pass: option.pass || '@anonymous',
  410. port: option.port || 21,
  411. remotePath: (option.remotePath || '/') + path
  412. })))
  413. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  414. host: option.host,
  415. user: option.user || 'anonymous',
  416. pass: option.pass || null,
  417. port: option.port || 22,
  418. remotePath: (option.remotePath || '/') + path
  419. })))
  420. .pipe(gulp.dest(dest))
  421. });
  422. }
  423. function getAppTask(path, isMin, thisOptions) {
  424. if (path==="x_component_process_Xform"){
  425. createDefaultTask(path, isMin, thisOptions);
  426. createXFormConcatTask(path, isMin, thisOptions);
  427. return gulp.series(path, path+" : concat");
  428. }else if (path==="o2_core"){
  429. createDefaultTask(path, isMin, thisOptions);
  430. createO2ConcatTask(path, isMin, thisOptions);
  431. return gulp.series(path, path+" : concat", path+".xDesktop : concat", path+" : bundle");
  432. }else if (path==="x_desktop") {
  433. createDefaultTask(path, isMin, thisOptions);
  434. createBaseWorkConcatTask(path, isMin, thisOptions);
  435. return gulp.series(path, path+".base_work : concat");
  436. }else{
  437. createDefaultTask(path, isMin, thisOptions);
  438. return gulp.series(path);
  439. }
  440. }
  441. //var taskObj = {};
  442. apps.map(function (app) {
  443. var taskName;
  444. var isMin = (app.tasks.indexOf("min")!==-1);
  445. taskName = app.folder;
  446. appTasks.push(taskName);
  447. gulp.task(taskName, getAppTask(app.folder, isMin));
  448. // //var isMin = (app.tasks.indexOf("min")!==-1);
  449. // taskName = app.folder+"_release";
  450. // //appTasks.push(taskName);
  451. // gulp.task(taskName, getAppTask(app.folder, isMin, release_options));
  452. });
  453. // Object.keys(taskObj).map(function(k){
  454. // exports[k] = parallel(taskObj[k]);
  455. // });
  456. //exports[app.folder] = parallel(minTask, moveTask);
  457. function getCleanTask(path) {
  458. return function (cb) {
  459. if (path){
  460. var dest = (path=="/") ? options.dest+"/" : options.dest+'/' + path + '/';
  461. gutil.log("Clean", ":", gutil.colors.red(dest));
  462. del.sync(dest, cb);
  463. cb();
  464. }else{
  465. cb();
  466. }
  467. }
  468. }
  469. function cleanRemoteFtp(f, cb) {
  470. var file = options.remotePath + f;
  471. var ftp = new JSFtp({
  472. host: options.host,
  473. user: options.user || 'anonymous',
  474. pass: options.pass || null,
  475. port: options.port || 21
  476. });
  477. ftp.raw('dele ' + file, function (err) {
  478. if (err) { cb(); return; }
  479. if (file.substring(file.length - 3).toLowerCase() == ".js") {
  480. file = file.replace('.js', ".min.js");
  481. ftp.raw('dele ' + file, function (err) {
  482. if (err) { cb(); return; }
  483. if (file.indexOf("/") != -1) {
  484. var p = file.substring(0, file.lastIndexOf("/"));
  485. ftp.raw('rmd ' + p, function (err) {
  486. if (err) { cb(); return; }
  487. ftp.raw.quit();
  488. cb();
  489. });
  490. }
  491. });
  492. } else {
  493. if (file.indexOf("/") != -1) {
  494. var pPath = file.substring(0, file.lastIndexOf("/"));
  495. ftp.raw('rmd ' + pPath, function (err) {
  496. if (err) { cb(); return; }
  497. ftp.raw.quit();
  498. cb();
  499. });
  500. }
  501. }
  502. });
  503. }
  504. function cleanRemoteLocal(f, cb) {
  505. var file = options.location + f;
  506. del(file, { force: true, dryRun: true }, function () {
  507. if (file.substring(file.length - 3).toLowerCase() == ".js") {
  508. var minfile = file.replace('.js', ".min.js");
  509. del(minfile, { force: true, dryRun: true }, function () {
  510. var p = file.substring(0, file.lastIndexOf("/"));
  511. fs.rmdir(p, function (err) {
  512. if (err) { }
  513. cb();
  514. })
  515. });
  516. } else {
  517. var p = file.substring(0, file.lastIndexOf("/"));
  518. fs.rmdir(p, function (err) {
  519. if (err) { }
  520. cb();
  521. })
  522. }
  523. });
  524. }
  525. function getCleanRemoteTask(path) {
  526. return function (cb) {
  527. if (options.upload) {
  528. var file = path.replace(/\\/g, "/");
  529. file = file.substring(file.indexOf("source/") + 7);
  530. if (options.upload == 'local' && options.location != '') cleanRemoteLocal(file, cb);
  531. if (options.upload == 'ftp' && options.host != '') cleanRemoteFtp(file, cb);
  532. } else {
  533. if (cb) cb();
  534. }
  535. }
  536. }
  537. function getWatchTask(path) {
  538. return (path) ? function (cb) {
  539. gutil.log("watch", ":", gutil.colors.green(path, "is watching ..."));
  540. gulp.watch(['source/' + path + '/**/*', "!./**/test/**"], { "events": ['addDir', 'add', 'change'] }, gulp.parallel([path]));
  541. } : function(cb){cb();};
  542. }
  543. gulp.task("clean", getCleanTask(options.src))
  544. gulp.task("watch", getWatchTask(options.src));
  545. gulp.task("index", function () {
  546. var src = ['source/favicon.ico', 'source/index.html'];
  547. var dest = options.dest;
  548. return gulp.src(src)
  549. .pipe(changed(dest))
  550. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + '/')))
  551. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  552. host: options.host,
  553. user: options.user || 'anonymous',
  554. pass: options.pass || '@anonymous',
  555. port: options.port || 21,
  556. remotePath: (options.remotePath || '/')
  557. })))
  558. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), ftp({
  559. host: options.host,
  560. user: options.user || 'anonymous',
  561. pass: options.pass || null,
  562. port: options.port || 22,
  563. remotePath: (options.remotePath || '/')
  564. })))
  565. .pipe(gulp.dest(dest))
  566. .pipe(gutil.noop());
  567. });
  568. gulp.task("cleanAll", getCleanTask('/'));
  569. gulp.task("o2:new-v:html", function () {
  570. var path = "x_desktop";
  571. var src = 'source/x_desktop/*.html';
  572. var dest = options.dest + '/x_desktop/';
  573. return gulp.src(src)
  574. .pipe(assetRev())
  575. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  576. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  577. host: options.host,
  578. user: options.user || 'anonymous',
  579. pass: options.pass || '@anonymous',
  580. port: options.port || 21,
  581. remotePath: (options.remotePath || '/') + path
  582. })))
  583. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  584. host: options.host,
  585. user: options.user || 'anonymous',
  586. pass: options.pass || null,
  587. port: options.port || 22,
  588. remotePath: (options.remotePath || '/') + path
  589. })))
  590. .pipe(gulp.dest(dest))
  591. .pipe(gutil.noop());
  592. });
  593. gulp.task("o2:new-v:o2", function () {
  594. var path = "o2_core";
  595. var src = 'source/o2_core/o2.js';
  596. var dest = options.dest +'/o2_core/';
  597. return gulp.src(src)
  598. .pipe(assetRev())
  599. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  600. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  601. host: options.host,
  602. user: options.user || 'anonymous',
  603. pass: options.pass || '@anonymous',
  604. port: options.port || 21,
  605. remotePath: (options.remotePath || '/') + path
  606. })))
  607. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  608. host: options.host,
  609. user: options.user || 'anonymous',
  610. pass: options.pass || null,
  611. port: options.port || 22,
  612. remotePath: (options.remotePath || '/') + path
  613. })))
  614. .pipe(gulp.dest(dest))
  615. .pipe(uglify())
  616. .pipe(rename({ extname: '.min.js' }))
  617. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  618. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  619. host: options.host,
  620. user: options.user || 'anonymous',
  621. pass: options.pass || '@anonymous',
  622. port: options.port || 21,
  623. remotePath: (options.remotePath || '/') + path
  624. })))
  625. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  626. host: options.host,
  627. user: options.user || 'anonymous',
  628. pass: options.pass || null,
  629. port: options.port || 22,
  630. remotePath: (options.remotePath || '/') + path
  631. })))
  632. .pipe(gulp.dest(dest))
  633. .pipe(gutil.noop());
  634. });
  635. gulp.task("o2:new-v", gulp.parallel("o2:new-v:o2", "o2:new-v:html"));
  636. gulp.task("git_clean", function (cb) {
  637. var dest = 'D:/O2/github/huqi1980/o2oa/o2web/source/';
  638. del(dest, { dryRun: true, force: true }, cb);
  639. });
  640. gulp.task("git_dest", function () {
  641. var dest = "D:/O2/github/huqi1980/o2oa/o2web/source";
  642. return gulp.src(["source/**/*", "!./**/test/**"])
  643. .pipe(changed(dest))
  644. .pipe(gulp.dest(dest))
  645. });
  646. gulp.task("git", gulp.series('git_clean', 'git_dest'));
  647. gulp.task("default", gulp.series(gulp.parallel(appTasks, 'index'), "o2:new-v"));
  648. function build(){
  649. options.ev = "p";
  650. options.upload = o_options.upload || "";
  651. options.location = o_options.location || uploadOptions.location;
  652. options.host = o_options.host || uploadOptions.host;
  653. options.user = o_options.user || uploadOptions.user;
  654. options.pass = o_options.pass || uploadOptions.pass;
  655. options.port = o_options.port || uploadOptions.port;
  656. options.remotePath = o_options.remotePath || uploadOptions.remotePath;
  657. options.dest = o_options.dest || uploadOptions.dest || "dest";
  658. };
  659. gulp.task("build", gulp.series("clean", gulp.parallel(appTasks, 'index'), "o2:new-v"))