update_deps.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. var https = require("https");
  2. var http = require("http");
  3. var url = require("url");
  4. var fs = require("fs");
  5. var Path = require("path");
  6. var spawn = require("child_process").spawn;
  7. var async = require("asyncjs");
  8. var rootDir = __dirname + "/../lib/ace/";
  9. var SKIP_NPM = false;
  10. var deps = {
  11. csslint: {
  12. path: "mode/css/csslint.js",
  13. // url: "https://raw.github.com/stubbornella/csslint/master/release/csslint.js",
  14. browserify: {
  15. npmModule: "git+https://github.com/CSSLint/csslint.git#master",
  16. path: "jshint/src/jshint.js",
  17. exports: "jshint"
  18. },
  19. fetch: browserify,
  20. wrapAmd: true
  21. },
  22. requirejs: {
  23. path: "../../demo/kitchen-sink/require.js",
  24. url: "https://raw.github.com/jrburke/requirejs/master/require.js",
  25. wrapAmd: false
  26. },
  27. luaparse: {
  28. path: "mode/lua/luaparse.js",
  29. url: "https://raw.github.com/oxyc/luaparse/master/luaparse.js",
  30. wrapAmd: true,
  31. postProcess: function(src) {
  32. return src.replace(
  33. /\(function\s*\(root,\s*name,\s*factory\)\s*{[\s\S]*?}\(this,\s*'luaparse',/,
  34. "(function (root, name, factory) {\n factory(exports)\n}(this, 'luaparse',"
  35. )
  36. }
  37. },
  38. html5: {
  39. path: "mode/html/saxparser.js",
  40. browserify: {
  41. npmModule: "git+https://github.com/aredridel/html5.git#master",
  42. path: "html5/lib/sax/SAXParser.js",
  43. exports: "SAXParser"
  44. },
  45. fetch: browserify,
  46. wrapAmd: true,
  47. postProcess: function(src) {
  48. return src;
  49. }
  50. },
  51. xquery: {
  52. path: "mode/xquery/xquery_lexer.js",
  53. browserify: {
  54. npmModule: "git+https://github.com/wcandillon/xqlint.git#master",
  55. path: "xqlint/lib/lexers/xquery_lexer.js",
  56. exports: "XQueryLexer"
  57. },
  58. fetch: browserify,
  59. wrapAmd: true,
  60. postProcess: function(src){
  61. return src;
  62. }
  63. },
  64. jsoniq: {
  65. path: "mode/xquery/jsoniq_lexer.js",
  66. browserify: {
  67. npmModule: "git+https://github.com/wcandillon/xqlint.git#master",
  68. path: "xqlint/lib/lexers/jsoniq_lexer.js",
  69. exports: "JSONiqLexer"
  70. },
  71. fetch: browserify,
  72. wrapAmd: true,
  73. postProcess: function(src){
  74. return src;
  75. }
  76. },
  77. xqlint: {
  78. path: "mode/xquery/xqlint.js",
  79. browserify: {
  80. npmModule: "git+https://github.com/wcandillon/xqlint.git#master",
  81. path: "xqlint/lib/xqlint.js",
  82. exports: "XQLint"
  83. },
  84. fetch: browserify,
  85. wrapAmd: true,
  86. postProcess: function(src){
  87. return src;
  88. }
  89. },
  90. jshint: {
  91. path: "mode/javascript/jshint.js",
  92. browserify: {
  93. npmModule: "git+https://github.com/ajaxorg/jshint.git#master",
  94. path: "jshint/src/jshint.js",
  95. exports: "jshint"
  96. },
  97. fetch: browserify,
  98. wrapAmd: true,
  99. postProcess: function(src) {
  100. src = src.replace(
  101. /"Expected a conditional expression and instead saw an assignment."/g,
  102. '"Assignment in conditional expression"'
  103. );
  104. src = src.replace(/var defaultMaxListeners = 10;/, function(a) {return a.replace("10", "200")});
  105. return src;
  106. }
  107. },
  108. emmet: {
  109. path: "ext/emmet core.js",
  110. url: [
  111. "https://raw.github.com/sergeche/emmet-sublime/master/emmet/emmet-app.js",
  112. "https://raw.github.com/sergeche/emmet-sublime/master/emmet/snippets.json"
  113. ],
  114. postProcess: function(data) {
  115. return data[0]
  116. .replace("define(emmet)", "define('emmet', [], emmet)")
  117. .replace(/(emmet.define\('bootstrap'.*)[\s\S]*$/, function(_, x) {
  118. return x + "\n" +
  119. "var snippets = " + data[1] + ";\n" +
  120. "var res = require('resources');\n" +
  121. "var userData = res.getVocabulary('user') || {};\n" +
  122. "res.setVocabulary(require('utils').deepMerge(userData, snippets), 'user');\n" +
  123. "});";
  124. });
  125. }
  126. },
  127. vim: {
  128. fetch: function(){
  129. var rootHref = "https://raw.githubusercontent.com/codemirror/CodeMirror/master/"
  130. var fileMap = {"keymap/vim.js": "keyboard/vim.js", "test/vim_test.js": "keyboard/vim_test.js"};
  131. async.forEach(Object.keys(fileMap), function(x, next) {
  132. download(rootHref + x, function(e, d) {
  133. d = d.replace(/^\(function.*{[^{}]+^}[^{}]+{/m, "define(function(require, exports, module) {");
  134. d = d.replace(/^\s*return vimApi;\s*};/gm, " //};")
  135. .replace("var Vim = function() {", "$& return vimApi; } //{")
  136. fs.writeFile(rootDir + fileMap[x], d, next)
  137. })
  138. }, function() {
  139. console.log("done")
  140. });
  141. }
  142. },
  143. liveScript: {
  144. path: "mode/livescript.js",
  145. url: "https://raw.githubusercontent.com/gkz/LiveScript/master/lib/mode-ls.js"
  146. },
  147. coffee: {
  148. path: "mode/coffee/coffee.js",
  149. url: "https://raw.githubusercontent.com/jashkenas/coffeescript/master/extras/coffee-script.js",
  150. wrapAmd: true,
  151. postProcess: function(src){
  152. return "function define(f) { module.exports = f() }; define.amd = {};\n"
  153. + dereqire(src);
  154. }
  155. },
  156. xmldom: {
  157. fetch: function() {
  158. var rootHref = "https://raw.githubusercontent.com/iDeBugger/xmldom/master/"
  159. var fileMap = {
  160. "sax.js": "mode/xml/sax.js",
  161. "dom-parser.js": "mode/xml/dom-parser.js",
  162. "dom.js": "mode/xml/dom.js"
  163. };
  164. async.forEach(Object.keys(fileMap), function(x, next) {
  165. download(rootHref + x, function(e, d) {
  166. fs.writeFile(rootDir + fileMap[x], d, next)
  167. })
  168. }, function() {
  169. console.log("XmlDOM updating done")
  170. });
  171. }
  172. },
  173. };
  174. var download = function(href, callback) {
  175. if (Array.isArray(href))
  176. return async.map(href, download, callback);
  177. var options = url.parse(href);
  178. var protocol = options.protocol === "https:" ? https : http;
  179. console.log("connecting to " + options.host + " " + options.path);
  180. protocol.get(options, function(res) {
  181. var data = "";
  182. res.setEncoding("utf-8");
  183. res.on("data", function(chunk){
  184. data += chunk;
  185. });
  186. res.on("end", function(){
  187. callback(null, data);
  188. });
  189. });
  190. };
  191. var getDep = function(dep) {
  192. if (!dep.fetch)
  193. dep.fetch = download
  194. dep.fetch(dep.url, function(err, data) {
  195. if (dep.postProcess)
  196. data = dep.postProcess(data);
  197. if (dep.wrapAmd)
  198. data = "define(function(require, exports, module) {\n"
  199. + data
  200. + "\n});";
  201. fs.writeFile(rootDir + dep.path, data, "utf-8", function(err){
  202. if (err) throw err;
  203. console.log("File " + dep.path + " saved.");
  204. });
  205. });
  206. };
  207. function run(cmd, cb) {
  208. var proc = process.platform == "win32"
  209. ? spawn("cmd", ["/c", cmd], {cwd: __dirname})
  210. : spawn("bash", ["-c", cmd], {cwd: __dirname});
  211. var result = "", err = "";
  212. proc.stderr.setEncoding("utf8");
  213. proc.stderr.on('data', function (data) {
  214. err += data;
  215. });
  216. proc.stdout.setEncoding("utf8");
  217. proc.stdout.on('data', function (data) {
  218. result += data;
  219. });
  220. proc.on('close', done);
  221. function done(code) {
  222. if (code !== 0) {
  223. console.error(cmd + '::: process exited with code :::' + code);
  224. console.error(err)
  225. }
  226. cb(err, result)
  227. }
  228. }
  229. function unquote(str) {
  230. return str.replace(/\\(.)/g, function(x, a) {
  231. return a == "n" ? "\n"
  232. : a == "t" ? "\t"
  233. : a == "r" ? "\r"
  234. : a
  235. });
  236. }
  237. function dereqire(src) {
  238. return require("derequire")(src, [
  239. {from: 'require', to: '_dereq_'},
  240. {from: 'define', to: '_defi_'}
  241. ]);
  242. }
  243. function browserify(_, cb) {
  244. var br = this.browserify;
  245. var path = Path.join("node_modules", br.path)
  246. process.chdir(__dirname);
  247. if (Path.sep == "\\" && !Path._relative) {
  248. Path._relative = Path.relative;
  249. Path.relative = function() {
  250. var v = Path._relative.apply(this, arguments);
  251. return v.replace(/\\/g, "/");
  252. }
  253. }
  254. function done() {
  255. var browserify = require('browserify');
  256. var absPath = require.resolve(__dirname + "/" + path);
  257. var defaultPreludePath = Path.join(require.resolve("browser-pack"), "../prelude.js");
  258. var defaultPrelude = "module.exports = " + fs.readFileSync(defaultPreludePath, 'utf8')
  259. .replace(/^[ \t]*\/\/.*/gm, "")
  260. .replace(/^\s*\n\r?/gm, "")
  261. .replace(/return newRequire;/, "return newRequire(entry[0]);")
  262. var opts = {
  263. expose: br.exports,
  264. prelude: defaultPrelude,
  265. exposeAll: true
  266. }
  267. var b = browserify(opts);
  268. b.plugin(require("deps-sort"), opts);
  269. b.add(absPath);
  270. var p = b.bundle();
  271. var buffer = "";
  272. p.on("data", function(e) { buffer += e; });
  273. p.on("end", function() {
  274. var src = dereqire(buffer);
  275. cb(null, src);
  276. });
  277. }
  278. if (SKIP_NPM) return done();
  279. run("npm install " + br.npmModule, done);
  280. }
  281. var args = process.argv.slice(2);
  282. SKIP_NPM = args.indexOf("--skip-npm") != -1;
  283. args = args.filter(function(x) {return x[0] != "-" });
  284. if (!args.length)
  285. args = Object.keys(deps);
  286. args.forEach(function(key) {
  287. getDep(deps[key])
  288. });