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