configNode.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 if(arg[0] == "mapPackage") {
  10. var parts = arg[1].split(":"),
  11. name = parts[0],
  12. location=parts[1],
  13. isPrexisting = false;
  14. for (var j = 0; j < config.packages.length; j++) {
  15. var pkg = config.packages[j];
  16. if (pkg.name === name) {
  17. pkg.location = location;
  18. isPrexisting = true;
  19. break;
  20. }
  21. }
  22. if (!isPrexisting) {
  23. config.packages.push({
  24. name: name,
  25. location: location
  26. });
  27. }
  28. }else{
  29. args.push(arg);
  30. }
  31. }
  32. var fs = require("fs");
  33. // make sure global require exists
  34. //if (typeof global.require=="undefined"){
  35. // global.require= {};
  36. //}
  37. // reset the has cache with node-appropriate values;
  38. var hasCache = {
  39. "host-node":1,
  40. "host-browser":0,
  41. "dom":0,
  42. "dojo-has-api":1,
  43. "dojo-xhr-factory":0,
  44. "dojo-inject-api":1,
  45. "dojo-timeout-api":0,
  46. "dojo-trace-api":1,
  47. "dojo-dom-ready-api":0,
  48. "dojo-publish-privates":1,
  49. "dojo-sniff":0,
  50. "dojo-loader":1,
  51. "dojo-test-xd":0,
  52. "dojo-test-sniff":0
  53. };
  54. for(var p in hasCache){
  55. config.hasCache[p] = hasCache[p];
  56. }
  57. var vm = require('vm'),
  58. path = require('path');
  59. // reset some configuration switches with node-appropriate values
  60. var nodeConfig = {
  61. baseUrl: path.dirname(process.argv[1]),
  62. commandLineArgs:args,
  63. deps:deps,
  64. timeout:0,
  65. // TODO: really get the locale
  66. locale:"en-us",
  67. loaderPatch: {
  68. log:function(item){
  69. // define debug for console messages during dev instead of console.log
  70. // (node's heavy async makes console.log confusing sometimes)
  71. var util = require("util");
  72. util.debug(util.inspect(item));
  73. },
  74. eval: function(__text, __urlHint){
  75. return vm.runInThisContext(__text, __urlHint);
  76. },
  77. injectUrl: function(url, callback){
  78. try{
  79. vm.runInThisContext(fs.readFileSync(url, "utf8"), url);
  80. callback();
  81. }catch(e){
  82. this.log("failed to load resource (" + url + ")");
  83. this.log(e);
  84. }
  85. },
  86. getText: function(url, sync, onLoad){
  87. // TODO: implement async and http/https handling
  88. onLoad(fs.readFileSync(url, "utf8"));
  89. }
  90. }
  91. };
  92. for(p in nodeConfig){
  93. config[p] = nodeConfig[p];
  94. }
  95. };