gulpfile.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 + '/Button.js',
  145. 'source/' + path + '/ViewSelector.js',
  146. 'source/' + path + '/*.js',
  147. 'source/x_component_process_Work/Processor.js',
  148. '!source/' + path + '/Office.js'
  149. ];
  150. var dest = option.dest+'/' + path + '/';
  151. return gulp.src(src)
  152. .pipe(concat('$all.js'))
  153. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  154. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  155. host: option.host,
  156. user: option.user || 'anonymous',
  157. pass: option.pass || '@anonymous',
  158. port: option.port || 21,
  159. remotePath: (option.remotePath || '/') + path
  160. })))
  161. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  162. host: option.host,
  163. user: option.user || 'anonymous',
  164. pass: option.pass || null,
  165. port: option.port || 22,
  166. remotePath: (option.remotePath || '/') + path
  167. })))
  168. .pipe(gulp.dest(dest))
  169. .pipe(uglify())
  170. .pipe(rename({ extname: '.min.js' }))
  171. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  172. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  173. host: option.host,
  174. user: option.user || 'anonymous',
  175. pass: option.pass || '@anonymous',
  176. port: option.port || 21,
  177. remotePath: (option.remotePath || '/') + path
  178. })))
  179. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  180. host: option.host,
  181. user: option.user || 'anonymous',
  182. pass: option.pass || null,
  183. port: option.port || 22,
  184. remotePath: (option.remotePath || '/') + path
  185. })))
  186. .pipe(gulp.dest(dest))
  187. });
  188. }
  189. function createO2ConcatTask(path, isMin, thisOptions) {
  190. gulp.task(path+" : concat", function(){
  191. var option = thisOptions || options;
  192. var src = [
  193. 'source/o2_lib/mootools/mootools-1.6.0_all.js',
  194. 'source/o2_lib/mootools/plugin/mBox.js',
  195. 'source/' + path + '/o2.js'
  196. ];
  197. var dest = option.dest+'/' + path + '/';
  198. return gulp.src(src)
  199. .pipe(concat('o2.js'))
  200. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  201. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  202. host: option.host,
  203. user: option.user || 'anonymous',
  204. pass: option.pass || '@anonymous',
  205. port: option.port || 21,
  206. remotePath: (option.remotePath || '/') + path
  207. })))
  208. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  209. host: option.host,
  210. user: option.user || 'anonymous',
  211. pass: option.pass || null,
  212. port: option.port || 22,
  213. remotePath: (option.remotePath || '/') + path
  214. })))
  215. .pipe(gulp.dest(dest))
  216. .pipe(uglify())
  217. .pipe(rename({ extname: '.min.js' }))
  218. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  219. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  220. host: option.host,
  221. user: option.user || 'anonymous',
  222. pass: option.pass || '@anonymous',
  223. port: option.port || 21,
  224. remotePath: (option.remotePath || '/') + path
  225. })))
  226. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  227. host: option.host,
  228. user: option.user || 'anonymous',
  229. pass: option.pass || null,
  230. port: option.port || 22,
  231. remotePath: (option.remotePath || '/') + path
  232. })))
  233. .pipe(gulp.dest(dest))
  234. });
  235. gulp.task(path+".xDesktop : concat", function(){
  236. var option = thisOptions || options;
  237. var src = [
  238. 'source/'+path+'/o2/widget/Common.js',
  239. 'source/'+path+'/o2/widget/Dialog.js',
  240. 'source/'+path+'/o2/widget/UUID.js',
  241. 'source/'+path+'/o2/xDesktop/Common.js',
  242. 'source/'+path+'/o2/xDesktop/Actions/RestActions.js',
  243. 'source/'+path+'/o2/xAction/RestActions.js',
  244. 'source/'+path+'/o2/xDesktop/Access.js',
  245. 'source/'+path+'/o2/xDesktop/Dialog.js',
  246. 'source/'+path+'/o2/xDesktop/Menu.js',
  247. 'source/'+path+'/o2/xDesktop/UserData.js',
  248. 'source/x_component_Template/MPopupForm.js',
  249. 'source/'+path+'/o2/xDesktop/Authentication.js',
  250. 'source/'+path+'/o2/xDesktop/Dialog.js',
  251. 'source/'+path+'/o2/xDesktop/Window.js',
  252. 'source/x_component_Common/Main.js'
  253. ];
  254. var dest = option.dest+'/' + path + '/o2/xDesktop/';
  255. return gulp.src(src)
  256. .pipe(concat('$all.js'))
  257. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/o2/xDesktop/')))
  258. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  259. host: option.host,
  260. user: option.user || 'anonymous',
  261. pass: option.pass || '@anonymous',
  262. port: option.port || 21,
  263. remotePath: (option.remotePath || '/') + path+"/o2/xDesktop/"
  264. })))
  265. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  266. host: option.host,
  267. user: option.user || 'anonymous',
  268. pass: option.pass || null,
  269. port: option.port || 22,
  270. remotePath: (option.remotePath || '/') + path+"/o2/xDesktop/"
  271. })))
  272. .pipe(gulp.dest(dest))
  273. .pipe(uglify())
  274. .pipe(rename({ extname: '.min.js' }))
  275. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/o2/xDesktop/')))
  276. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  277. host: option.host,
  278. user: option.user || 'anonymous',
  279. pass: option.pass || '@anonymous',
  280. port: option.port || 21,
  281. remotePath: (option.remotePath || '/') + path+"/o2/xDesktop/"
  282. })))
  283. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  284. host: option.host,
  285. user: option.user || 'anonymous',
  286. pass: option.pass || null,
  287. port: option.port || 22,
  288. remotePath: (option.remotePath || '/') + path+"/o2/xDesktop/"
  289. })))
  290. .pipe(gulp.dest(dest))
  291. });
  292. gulp.task(path+" : bundle", function(){
  293. var option = thisOptions || options;
  294. var src = [
  295. 'source/o2_lib/mootools/mootools-1.6.0_all.js',
  296. 'source/o2_lib/mootools/plugin/mBox.js',
  297. 'source/' + path + '/o2.js',
  298. 'source/x_desktop/js/base.js',
  299. "source/o2_core/o2/framework.js"
  300. ];
  301. var dest = option.dest+'/' + path + '/';
  302. return gulp.src(src)
  303. .pipe(concat('bundle.js'))
  304. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  305. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  306. host: option.host,
  307. user: option.user || 'anonymous',
  308. pass: option.pass || '@anonymous',
  309. port: option.port || 21,
  310. remotePath: (option.remotePath || '/') + path
  311. })))
  312. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  313. host: option.host,
  314. user: option.user || 'anonymous',
  315. pass: option.pass || null,
  316. port: option.port || 22,
  317. remotePath: (option.remotePath || '/') + path
  318. })))
  319. .pipe(gulp.dest(dest))
  320. .pipe(uglify())
  321. .pipe(rename({ extname: '.min.js' }))
  322. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  323. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  324. host: option.host,
  325. user: option.user || 'anonymous',
  326. pass: option.pass || '@anonymous',
  327. port: option.port || 21,
  328. remotePath: (option.remotePath || '/') + path
  329. })))
  330. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  331. host: option.host,
  332. user: option.user || 'anonymous',
  333. pass: option.pass || null,
  334. port: option.port || 22,
  335. remotePath: (option.remotePath || '/') + path
  336. })))
  337. .pipe(gulp.dest(dest))
  338. });
  339. }
  340. function createBaseWorkConcatTask(path, isMin, thisOptions) {
  341. gulp.task(path+".base_work : concat", function(){
  342. var option = thisOptions || options;
  343. var src = [
  344. 'source/' + path + '/js/base_work_begin.js',
  345. 'source/o2_core/o2/widget/Common.js',
  346. 'source/o2_core/o2/widget/Dialog.js',
  347. 'source/o2_core/o2/widget/UUID.js',
  348. 'source/o2_core/o2/widget/Menu.js',
  349. 'source/o2_core/o2/widget/Toolbar.js',
  350. 'source/o2_core/o2/xDesktop/Common.js',
  351. 'source/o2_core/o2/xDesktop/Actions/RestActions.js',
  352. 'source/o2_core/o2/xAction/RestActions.js',
  353. 'source/o2_core/o2/xDesktop/Access.js',
  354. 'source/o2_core/o2/xDesktop/Dialog.js',
  355. 'source/o2_core/o2/xDesktop/Menu.js',
  356. 'source/o2_core/o2/xDesktop/UserData.js',
  357. 'source/x_component_Template/MPopupForm.js',
  358. 'source/o2_core/o2/xDesktop/Authentication.js',
  359. 'source/o2_core/o2/xDesktop/Dialog.js',
  360. 'source/o2_core/o2/xDesktop/Window.js',
  361. 'source/x_component_Common/Main.js',
  362. 'source/x_component_process_Work/Main.js',
  363. 'source/x_component_Selector/package.js',
  364. 'source/x_component_Selector/Person.js',
  365. 'source/x_component_Selector/Identity.js',
  366. 'source/x_component_Selector/Unit.js',
  367. 'source/x_component_Selector/IdentityWidthDuty.js',
  368. 'source/x_component_Selector/IdentityWidthDutyCategoryByUnit.js',
  369. 'source/x_component_Selector/UnitWithType.js',
  370. 'source/o2_core/o2/xScript/Actions/UnitActions.js',
  371. 'source/o2_core/o2/xScript/Actions/ScriptActions.js',
  372. 'source/o2_core/o2/xScript/Actions/CMSScriptActions.js',
  373. 'source/o2_core/o2/xScript/Actions/PortalScriptActions.js',
  374. 'source/o2_core/o2/xScript/Environment.js',
  375. 'source/x_component_Template/MTooltips.js',
  376. 'source/x_component_Template/MSelector.js',
  377. 'source/o2_core/o2/xAction/services/x_organization_assemble_authentication.js',
  378. 'source/o2_core/o2/xAction/services/x_processplatform_assemble_surface.js',
  379. 'source/o2_core/o2/xAction/services/x_cms_assemble_control.js',
  380. 'source/o2_core/o2/xAction/services/x_organization_assemble_control.js',
  381. 'source/o2_core/o2/xAction/services/x_query_assemble_surface.js',
  382. 'source/o2_core/o2/xAction/services/x_organization_assemble_personal.js',
  383. 'source/' + path + '/js/base_work_end.js',
  384. 'source/' + path + '/js/base.js'
  385. ];
  386. var dest = option.dest+'/' + path + '/';
  387. return gulp.src(src)
  388. .pipe(concat('js/base_work.js'))
  389. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  390. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  391. host: option.host,
  392. user: option.user || 'anonymous',
  393. pass: option.pass || '@anonymous',
  394. port: option.port || 21,
  395. remotePath: (option.remotePath || '/') + path
  396. })))
  397. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  398. host: option.host,
  399. user: option.user || 'anonymous',
  400. pass: option.pass || null,
  401. port: option.port || 22,
  402. remotePath: (option.remotePath || '/') + path
  403. })))
  404. .pipe(gulp.dest(dest))
  405. .pipe(uglify())
  406. .pipe(rename({ extname: '.min.js' }))
  407. .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
  408. .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
  409. host: option.host,
  410. user: option.user || 'anonymous',
  411. pass: option.pass || '@anonymous',
  412. port: option.port || 21,
  413. remotePath: (option.remotePath || '/') + path
  414. })))
  415. .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
  416. host: option.host,
  417. user: option.user || 'anonymous',
  418. pass: option.pass || null,
  419. port: option.port || 22,
  420. remotePath: (option.remotePath || '/') + path
  421. })))
  422. .pipe(gulp.dest(dest))
  423. });
  424. }
  425. function getAppTask(path, isMin, thisOptions) {
  426. if (path==="x_component_process_Xform"){
  427. createDefaultTask(path, isMin, thisOptions);
  428. createXFormConcatTask(path, isMin, thisOptions);
  429. return gulp.series(path, path+" : concat");
  430. }else if (path==="o2_core"){
  431. createDefaultTask(path, isMin, thisOptions);
  432. createO2ConcatTask(path, isMin, thisOptions);
  433. return gulp.series(path, path+" : concat", path+".xDesktop : concat", path+" : bundle");
  434. }else if (path==="x_desktop") {
  435. createDefaultTask(path, isMin, thisOptions);
  436. createBaseWorkConcatTask(path, isMin, thisOptions);
  437. return gulp.series(path, path+".base_work : concat");
  438. }else{
  439. createDefaultTask(path, isMin, thisOptions);
  440. return gulp.series(path);
  441. }
  442. }
  443. //var taskObj = {};
  444. apps.map(function (app) {
  445. var taskName;
  446. var isMin = (app.tasks.indexOf("min")!==-1);
  447. taskName = app.folder;
  448. appTasks.push(taskName);
  449. gulp.task(taskName, getAppTask(app.folder, isMin));
  450. // //var isMin = (app.tasks.indexOf("min")!==-1);
  451. // taskName = app.folder+"_release";
  452. // //appTasks.push(taskName);
  453. // gulp.task(taskName, getAppTask(app.folder, isMin, release_options));
  454. });
  455. // Object.keys(taskObj).map(function(k){
  456. // exports[k] = parallel(taskObj[k]);
  457. // });
  458. //exports[app.folder] = parallel(minTask, moveTask);
  459. function getCleanTask(path) {
  460. return function (cb) {
  461. if (path){
  462. var dest = (path=="/") ? options.dest+"/" : options.dest+'/' + path + '/';
  463. gutil.log("Clean", ":", gutil.colors.red(dest));
  464. del.sync(dest, cb);
  465. cb();
  466. }else{
  467. cb();
  468. }
  469. }
  470. }
  471. function cleanRemoteFtp(f, cb) {
  472. var file = options.remotePath + f;
  473. var ftp = new JSFtp({
  474. host: options.host,
  475. user: options.user || 'anonymous',
  476. pass: options.pass || null,
  477. port: options.port || 21
  478. });
  479. ftp.raw('dele ' + file, function (err) {
  480. if (err) { cb(); return; }
  481. if (file.substring(file.length - 3).toLowerCase() == ".js") {
  482. file = file.replace('.js', ".min.js");
  483. ftp.raw('dele ' + file, function (err) {
  484. if (err) { cb(); return; }
  485. if (file.indexOf("/") != -1) {
  486. var p = file.substring(0, file.lastIndexOf("/"));
  487. ftp.raw('rmd ' + p, function (err) {
  488. if (err) { cb(); return; }
  489. ftp.raw.quit();
  490. cb();
  491. });
  492. }
  493. });
  494. } else {
  495. if (file.indexOf("/") != -1) {
  496. var pPath = file.substring(0, file.lastIndexOf("/"));
  497. ftp.raw('rmd ' + pPath, function (err) {
  498. if (err) { cb(); return; }
  499. ftp.raw.quit();
  500. cb();
  501. });
  502. }
  503. }
  504. });
  505. }
  506. function cleanRemoteLocal(f, cb) {
  507. var file = options.location + f;
  508. del(file, { force: true, dryRun: true }, function () {
  509. if (file.substring(file.length - 3).toLowerCase() == ".js") {
  510. var minfile = file.replace('.js', ".min.js");
  511. del(minfile, { force: true, dryRun: true }, function () {
  512. var p = file.substring(0, file.lastIndexOf("/"));
  513. fs.rmdir(p, function (err) {
  514. if (err) { }
  515. cb();
  516. })
  517. });
  518. } else {
  519. var p = file.substring(0, file.lastIndexOf("/"));
  520. fs.rmdir(p, function (err) {
  521. if (err) { }
  522. cb();
  523. })
  524. }
  525. });
  526. }
  527. function getCleanRemoteTask(path) {
  528. return function (cb) {
  529. if (options.upload) {
  530. var file = path.replace(/\\/g, "/");
  531. file = file.substring(file.indexOf("source/") + 7);
  532. if (options.upload == 'local' && options.location != '') cleanRemoteLocal(file, cb);
  533. if (options.upload == 'ftp' && options.host != '') cleanRemoteFtp(file, cb);
  534. } else {
  535. if (cb) cb();
  536. }
  537. }
  538. }
  539. function getWatchTask(path) {
  540. return (path) ? function (cb) {
  541. gutil.log("watch", ":", gutil.colors.green(path, "is watching ..."));
  542. gulp.watch(['source/' + path + '/**/*', "!./**/test/**"], { "events": ['addDir', 'add', 'change'] }, gulp.parallel([path]));
  543. } : function(cb){cb();};
  544. }
  545. gulp.task("clean", getCleanTask(options.src))
  546. gulp.task("watch", getWatchTask(options.src));
  547. gulp.task("index", function () {
  548. var src = ['source/favicon.ico', 'source/index.html'];
  549. var dest = options.dest;
  550. return gulp.src(src)
  551. .pipe(changed(dest))
  552. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + '/')))
  553. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  554. host: options.host,
  555. user: options.user || 'anonymous',
  556. pass: options.pass || '@anonymous',
  557. port: options.port || 21,
  558. remotePath: (options.remotePath || '/')
  559. })))
  560. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), ftp({
  561. host: options.host,
  562. user: options.user || 'anonymous',
  563. pass: options.pass || null,
  564. port: options.port || 22,
  565. remotePath: (options.remotePath || '/')
  566. })))
  567. .pipe(gulp.dest(dest))
  568. .pipe(gutil.noop());
  569. });
  570. gulp.task("cleanAll", getCleanTask('/'));
  571. gulp.task("o2:new-v:html", function () {
  572. var path = "x_desktop";
  573. var src = '/source/x_desktop/*.html';
  574. var dest = options.dest + '/x_desktop/';
  575. return gulp.src(src)
  576. .pipe(assetRev())
  577. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  578. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  579. host: options.host,
  580. user: options.user || 'anonymous',
  581. pass: options.pass || '@anonymous',
  582. port: options.port || 21,
  583. remotePath: (options.remotePath || '/') + path
  584. })))
  585. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  586. host: options.host,
  587. user: options.user || 'anonymous',
  588. pass: options.pass || null,
  589. port: options.port || 22,
  590. remotePath: (options.remotePath || '/') + path
  591. })))
  592. .pipe(gulp.dest(dest))
  593. .pipe(gutil.noop());
  594. });
  595. gulp.task("o2:new-v:o2", function () {
  596. var path = "o2_core";
  597. var src = options.dest +'/o2_core/o2.js';
  598. var dest = options.dest +'/o2_core/';
  599. return gulp.src(src)
  600. .pipe(assetRev())
  601. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  602. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  603. host: options.host,
  604. user: options.user || 'anonymous',
  605. pass: options.pass || '@anonymous',
  606. port: options.port || 21,
  607. remotePath: (options.remotePath || '/') + path
  608. })))
  609. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  610. host: options.host,
  611. user: options.user || 'anonymous',
  612. pass: options.pass || null,
  613. port: options.port || 22,
  614. remotePath: (options.remotePath || '/') + path
  615. })))
  616. .pipe(gulp.dest(dest))
  617. .pipe(uglify())
  618. .pipe(rename({ extname: '.min.js' }))
  619. .pipe(gulpif((options.upload == 'local' && options.location != ''), gulp.dest(options.location + path + '/')))
  620. .pipe(gulpif((options.upload == 'ftp' && options.host != ''), ftp({
  621. host: options.host,
  622. user: options.user || 'anonymous',
  623. pass: options.pass || '@anonymous',
  624. port: options.port || 21,
  625. remotePath: (options.remotePath || '/') + path
  626. })))
  627. .pipe(gulpif((options.upload == 'sftp' && options.host != ''), sftp({
  628. host: options.host,
  629. user: options.user || 'anonymous',
  630. pass: options.pass || null,
  631. port: options.port || 22,
  632. remotePath: (options.remotePath || '/') + path
  633. })))
  634. .pipe(gulp.dest(dest))
  635. .pipe(gutil.noop());
  636. });
  637. gulp.task("o2:new-v", gulp.parallel("o2:new-v:o2", "o2:new-v:html"));
  638. gulp.task("git_clean", function (cb) {
  639. var dest = 'D:/O2/github/huqi1980/o2oa/o2web/source/';
  640. del(dest, { dryRun: true, force: true }, cb);
  641. });
  642. gulp.task("git_dest", function () {
  643. var dest = "D:/O2/github/huqi1980/o2oa/o2web/source";
  644. return gulp.src(["source/**/*", "!./**/test/**"])
  645. .pipe(changed(dest))
  646. .pipe(gulp.dest(dest))
  647. });
  648. gulp.task("git", gulp.series('git_clean', 'git_dest'));
  649. gulp.task("default", gulp.series(gulp.parallel(appTasks, 'index'), "o2:new-v"));
  650. function build(){
  651. options.ev = "p";
  652. options.upload = o_options.upload || "";
  653. options.location = o_options.location || uploadOptions.location;
  654. options.host = o_options.host || uploadOptions.host;
  655. options.user = o_options.user || uploadOptions.user;
  656. options.pass = o_options.pass || uploadOptions.pass;
  657. options.port = o_options.port || uploadOptions.port;
  658. options.remotePath = o_options.remotePath || uploadOptions.remotePath;
  659. options.dest = o_options.dest || uploadOptions.dest || "dest";
  660. };
  661. gulp.task("build", gulp.series("clean", gulp.parallel(appTasks, 'index'), "o2:new-v"))