configNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. exports.config = function(config){
  2. // summary:
  3. // This module provides bootstrap configuration for running dojo in node.js
  4. // any command line arguments with the load flag are pushed into deps
  5. for(var deps = [], args = [], i = 0; i < process.argv.length; i++){
  6. var arg = (process.argv[i] + "").split("=");
  7. if(arg[0] == "load"){
  8. deps.push(arg[1]);
  9. }else{
  10. args.push(arg);
  11. }
  12. }
  13. var fs = require("fs");
  14. // make sure global require exists
  15. //if (typeof global.require=="undefined"){
  16. // global.require= {};
  17. //}
  18. // reset the has cache with node-appropriate values;
  19. var hasCache = {
  20. "host-node":1,
  21. "host-browser":0,
  22. "dom":0,
  23. "dojo-has-api":1,
  24. "dojo-xhr-factory":0,
  25. "dojo-inject-api":1,
  26. "dojo-timeout-api":0,
  27. "dojo-trace-api":1,
  28. "dojo-dom-ready-api":0,
  29. "dojo-publish-privates":1,
  30. "dojo-sniff":0,
  31. "dojo-loader":1,
  32. "dojo-test-xd":0,
  33. "dojo-test-sniff":0
  34. };
  35. for(var p in hasCache){
  36. config.hasCache[p] = hasCache[p];
  37. }
  38. var vm = require('vm'),
  39. path = require('path');
  40. // reset some configuration switches with node-appropriate values
  41. var nodeConfig = {
  42. baseUrl: path.dirname(process.argv[1]),
  43. commandLineArgs:args,
  44. deps:deps,
  45. timeout:0,
  46. // TODO: really get the locale
  47. locale:"en-us",
  48. loaderPatch: {
  49. log:function(item){
  50. // define debug for console messages during dev instead of console.log
  51. // (node's heavy async makes console.log confusing sometimes)
  52. var util = require("util");
  53. util.debug(util.inspect(item));
  54. },
  55. eval: function(__text, __urlHint){
  56. return vm.runInThisContext(__text, __urlHint);
  57. },
  58. injectUrl: function(url, callback){
  59. try{
  60. vm.runInThisContext(fs.readFileSync(url, "utf8"), url);
  61. callback();
  62. }catch(e){
  63. this.log("failed to load resource (" + url + ")");
  64. this.log(e);
  65. }
  66. },
  67. getText: function(url, sync, onLoad){
  68. // TODO: implement async and http/https handling
  69. onLoad(fs.readFileSync(url, "utf8"));
  70. }
  71. }
  72. };
  73. for(p in nodeConfig){
  74. config[p] = nodeConfig[p];
  75. }
  76. };