PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/Metis/src/assets/lib/elFinder/Jakefile.js

https://gitlab.com/kaouech/theme
JavaScript | 249 lines | 203 code | 22 blank | 24 comment | 21 complexity | 50158f34983d56187a97caf507736f26 MD5 | raw file
  1. /*
  2. * This is build file for elFinder 2.x
  3. * Build tool: https://github.com/mde/jake
  4. * JS compressor: https://github.com/mishoo/UglifyJS/
  5. * CSS optimizer: https://github.com/afelix/csso
  6. */
  7. // if Jake fails to detect need libraries try running before: export NODE_PATH=`npm root`
  8. var sys = require('sys'),
  9. fs = require('fs'),
  10. path = require('path'),
  11. util = require('util'),
  12. ugp = require('uglify-js').parser,
  13. ugu = require('uglify-js').uglify,
  14. csso = require('csso');
  15. var dirmode = 0755,
  16. src = __dirname,
  17. files = {
  18. 'elfinder.full.js':
  19. [
  20. path.join(src, 'js', 'elFinder.js'),
  21. path.join(src, 'js', 'elFinder.version.js'),
  22. path.join(src, 'js', 'jquery.elfinder.js'),
  23. path.join(src, 'js', 'elFinder.options.js'),
  24. path.join(src, 'js', 'elFinder.history.js'),
  25. path.join(src, 'js', 'elFinder.command.js'),
  26. path.join(src, 'js', 'elFinder.resources.js'),
  27. path.join(src, 'js', 'jquery.dialogelfinder.js'),
  28. path.join(src, 'js', 'i18n', 'elfinder.en.js')
  29. ]
  30. .concat(grep(path.join(src, 'js', 'ui'), '\\.js$'))
  31. .concat(grep(path.join(src, 'js', 'commands'), '\\.js$')),
  32. 'elfinder.full.css': grep(path.join(src, 'css'), '\\.css$', 'theme'),
  33. 'images': grep(path.join(src, 'img'), '\\.png|\\.gif'),
  34. 'i18n': grep(path.join(src, 'js', 'i18n'), '\\.js', 'elfinder.en.js'),
  35. 'php':
  36. [
  37. path.join(src, 'php', 'elFinder.class.php'),
  38. path.join(src, 'php', 'elFinderConnector.class.php'),
  39. path.join(src, 'php', 'elFinderVolumeDriver.class.php'),
  40. path.join(src, 'php', 'elFinderVolumeLocalFileSystem.class.php'),
  41. path.join(src, 'php', 'elFinderVolumeMySQL.class.php'),
  42. path.join(src, 'php', 'mime.types'),
  43. path.join(src, 'php', 'MySQLStorage.sql')
  44. ],
  45. 'misc':
  46. [
  47. path.join(src, 'js', 'proxy', 'elFinderSupportVer1.js'),
  48. path.join(src, 'Changelog'),
  49. path.join(src, 'README.md')
  50. ]
  51. };
  52. // custom functions
  53. function grep(prefix, mask, exculde) {
  54. var m = new RegExp(mask);
  55. var e = new RegExp(exculde);
  56. var o = new Array();
  57. var input = new Array();
  58. try {
  59. input = fs.readdirSync(prefix);
  60. } catch (err) { }
  61. for (i in input) {
  62. if ((typeof exculde !== 'undefined') && (input[i].match(e))) {
  63. //console.log('skip ' + input[i]);
  64. continue;
  65. }
  66. if (input[i].match(m)) {
  67. o.push(path.join(prefix, input[i]));
  68. }
  69. }
  70. return o.sort();
  71. }
  72. function copyFile(from, to, overwrite) {
  73. if (!overwrite && path.existsSync(to)) {
  74. return false;
  75. }
  76. console.log('\t' + from);
  77. var srcs = fs.createReadStream(from);
  78. var dsts = fs.createWriteStream(to);
  79. return util.pump(srcs, dsts);
  80. }
  81. function getComment() {
  82. var ver = fs.readFileSync(path.join(src, 'js', 'elFinder.version.js')).toString();
  83. ver = ver.match(/= '(.+)';/);
  84. var d = new Date();
  85. var bd = d.getFullYear() + '-' +
  86. (d.getMonth() >= 9 ? '' : '0') + (d.getMonth() + 1) + '-' +
  87. (d.getDate() >= 10 ? '' : '0') + d.getDate();
  88. var comment =
  89. '/*!\n' +
  90. ' * elFinder - file manager for web\n' +
  91. ' * Version ' + ver[1] + ' (' + bd + ')\n' +
  92. ' * http://elfinder.org\n' +
  93. ' * \n' +
  94. ' * Copyright 2009-2012, Studio 42\n' +
  95. ' * Licensed under a 3 clauses BSD license\n' +
  96. ' */\n';
  97. return comment;
  98. }
  99. // tasks
  100. desc('Help')
  101. task('default', function(){
  102. console.log(
  103. "This is elFinder build script, run `jake --tasks` for more info, for a default build run:\n" +
  104. " jake -C ./build elfinder"
  105. );
  106. });
  107. desc('pre build task')
  108. task('prebuild', function(){
  109. console.log('build dir: ' + path.resolve());
  110. console.log('src dir: ' + src);
  111. var dir = ['css', 'js', 'img', path.join('js', 'i18n'), path.join('js', 'proxy'), 'php', 'files'];
  112. for (d in dir) {
  113. var bd = dir[d];
  114. if (!path.existsSync(bd)) {
  115. console.log('mkdir ' + bd);
  116. fs.mkdirSync(bd, dirmode);
  117. }
  118. }
  119. //jake.Task['elfinder'].invoke();
  120. });
  121. desc('build elFinder')
  122. task({'elfinder': ['prebuild', 'css/elfinder.min.css', 'js/elfinder.min.js', 'misc']}, function(){
  123. console.log('elFinder build done');
  124. });
  125. // CSS
  126. desc('concat elfinder.full.css')
  127. file({'css/elfinder.full.css': files['elfinder.full.css']}, function(){
  128. console.log('concat ' + this.name)
  129. var data = '';
  130. for (f in this.prereqs) {
  131. file = this.prereqs[f];
  132. console.log('\t' + file);
  133. data += '\n/* File: ' + file + ' */\n';
  134. data += fs.readFileSync(file);
  135. }
  136. fs.writeFileSync(this.name, getComment() + data);
  137. });
  138. desc('optimize elfinder.min.css');
  139. file({'css/elfinder.min.css': ['css/elfinder.full.css']}, function () {
  140. console.log('optimize elfinder.min.css');
  141. var css_optimized = csso.justDoIt(fs.readFileSync('css/elfinder.full.css').toString())
  142. fs.writeFileSync(this.name, getComment() + css_optimized);
  143. });
  144. // JS
  145. desc('concat elfinder.full.js')
  146. file({'js/elfinder.full.js': files['elfinder.full.js']}, function(){
  147. console.log('concat elfinder.full.js');
  148. var strict = new RegExp('"use strict"\;?\n?');
  149. var elf = files['elfinder.full.js'];
  150. var data = '';
  151. for (f in elf) {
  152. file = elf[f];
  153. console.log('\t' + file);
  154. data += '\n\n/*\n * File: ' + file + '\n */\n\n';
  155. data += fs.readFileSync(file);
  156. data = data.replace(strict, '');
  157. }
  158. data = '(function($) {\n' + data + '\n})(jQuery);'; // add closure
  159. fs.writeFileSync(this.name, getComment() + data);
  160. });
  161. desc('uglify elfinder.min.js');
  162. file({'js/elfinder.min.js': ['js/elfinder.full.js']}, function () {
  163. console.log('uglify elfinder.min.js');
  164. var ast = ugp.parse(fs.readFileSync('js/elfinder.full.js').toString()); // parse code and get the initial AST
  165. ast = ugu.ast_mangle(ast); // get a new AST with mangled names
  166. ast = ugu.ast_squeeze(ast); // get an AST with compression optimizations
  167. var result = ugu.split_lines(ugu.gen_code(ast), 1024 * 8); // insert new line every 8 kb
  168. fs.writeFileSync(this.name, getComment() + result);
  169. });
  170. // IMG + I18N + PHP
  171. desc('copy misc files')
  172. task('misc', function(){
  173. console.log('copy misc files');
  174. var cf = files['images']
  175. .concat(files['i18n'])
  176. .concat(path.join(src, 'css', 'theme.css'))
  177. .concat(files['php'])
  178. .concat(files['misc']);
  179. for (i in cf)
  180. {
  181. var dst = cf[i].replace(src, '').substr(1);
  182. copyFile(cf[i], dst);
  183. }
  184. // elfinder.html
  185. var hs = path.join(src, 'build', 'elfinder.html');
  186. var hd = path.join('elfinder.html');
  187. copyFile(hs, hd);
  188. // connector
  189. var cs = path.join(src, 'php', 'connector.minimal.php');
  190. var cd = path.join('php', 'connector.php');
  191. copyFile(cs, cd);
  192. });
  193. // other
  194. desc('clean build dir')
  195. task('clean', function(){
  196. console.log('cleaning the floor')
  197. uf = ['js/elfinder.full.js', 'js/elfinder.min.js', 'css/elfinder.full.css', 'css/elfinder.min.css'];
  198. // clean images, js/i18n and php only if we are not in src
  199. if (src != path.resolve()) {
  200. uf = uf
  201. .concat(grep('img', '\\.png|\\.gif'))
  202. .concat(grep(path.join('js', 'i18n')))
  203. .concat(path.join('css', 'theme.css'))
  204. .concat(grep('php'))
  205. .concat([path.join('js', 'proxy', 'elFinderSupportVer1.js'), 'Changelog', 'README.md']);
  206. }
  207. for (f in uf) {
  208. var file = uf[f];
  209. if (path.existsSync(file)) {
  210. console.log('\tunlink ' + file);
  211. fs.unlinkSync(file);
  212. }
  213. }
  214. if (path.join(src, 'build') != path.resolve()) {
  215. fs.unlinkSync('elfinder.html');
  216. }
  217. if (src != path.resolve()) {
  218. var ud = ['css', path.join('js', 'proxy'), path.join('js', 'i18n'), 'js', 'img', 'php', 'files'];
  219. for (d in ud) {
  220. var dir = ud[d];
  221. if (path.existsSync(dir)) {
  222. console.log('\trmdir ' + dir);
  223. fs.rmdirSync(dir);
  224. }
  225. }
  226. }
  227. });