diff options
author | Armen Kesablyan | 2012-02-07 16:51:14 -0800 |
---|---|---|
committer | Armen Kesablyan | 2012-02-07 16:51:14 -0800 |
commit | 8110e01dc5093ac6baf621103dcaa5f769e5be92 (patch) | |
tree | ecf339e3097b6ba25d81cbe44d913ee67591bf4b /node_modules/montage/node.js | |
parent | 5f506542d28e12b707c3bfa41b53383519838477 (diff) | |
parent | 668510892537eaaeb2e11520831d87b44b2489b7 (diff) | |
download | ninja-8110e01dc5093ac6baf621103dcaa5f769e5be92.tar.gz |
Merge branch 'master' of github.com:Motorola-Mobility/ninja-internal
Diffstat (limited to 'node_modules/montage/node.js')
-rw-r--r-- | node_modules/montage/node.js | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/node_modules/montage/node.js b/node_modules/montage/node.js new file mode 100644 index 00000000..5ac51ed7 --- /dev/null +++ b/node_modules/montage/node.js | |||
@@ -0,0 +1,79 @@ | |||
1 | |||
2 | var FS = require("fs"); | ||
3 | var PATH = require("path"); | ||
4 | |||
5 | var MontageBoot = require("./montage"); | ||
6 | |||
7 | var Require = require("./require/require"); | ||
8 | var Promise = require("./core/promise"); | ||
9 | var URL = require("./core/url"); | ||
10 | |||
11 | exports.bootstrap = function (callback) { | ||
12 | var command = process.argv.slice(0, 3); | ||
13 | var args = process.argv.slice(2); | ||
14 | var program = args.shift(); | ||
15 | FS.realpath(program, function (error, program) { | ||
16 | if (error) { | ||
17 | throw new Error(error); | ||
18 | } | ||
19 | findPackage(program, function (error, directory) { | ||
20 | if (error === "Can't find package") { | ||
21 | loadFreeModule(program, command, args); | ||
22 | } else if (error) { | ||
23 | throw new Error(error); | ||
24 | } else { | ||
25 | loadPackagedModule(directory, program, command, args); | ||
26 | } | ||
27 | }); | ||
28 | }); | ||
29 | }; | ||
30 | |||
31 | MontageBoot.loadPackage = function (location, config) { | ||
32 | var config = {}; | ||
33 | |||
34 | config.location = URL.resolve('file:/', location + '/'); | ||
35 | |||
36 | // setup the reel loader | ||
37 | config.makeLoader = function (config) { | ||
38 | return MontageBoot.ReelLoader(config, | ||
39 | Require.DefaultLoaderConstructor(config)); | ||
40 | }; | ||
41 | |||
42 | // setup serialization compiler | ||
43 | config.makeCompiler = function (config) { | ||
44 | return MontageBoot.TemplateCompiler(config, | ||
45 | MontageBoot.SerializationCompiler(config, | ||
46 | Require.DefaultCompilerConstructor(config))); | ||
47 | }; | ||
48 | |||
49 | return Require.PackageSandbox(config.location, config); | ||
50 | }; | ||
51 | |||
52 | var findPackage = function (path, callback) { | ||
53 | var directory = PATH.dirname(path); | ||
54 | if (directory === path) | ||
55 | return callback("Can't find package"); | ||
56 | var packageJson = PATH.join(directory, "package.json"); | ||
57 | FS.stat(path, function (error, stat) { | ||
58 | if (error) callback(error); | ||
59 | if (stat.isFile()) { | ||
60 | callback(null, directory); | ||
61 | } else { | ||
62 | findPackage(directory, callback); | ||
63 | } | ||
64 | }); | ||
65 | } | ||
66 | |||
67 | var loadFreeModule = function (program, command, args) { | ||
68 | throw new Error("Can't load module that is not in a package"); | ||
69 | }; | ||
70 | |||
71 | var loadPackagedModule = function (directory, program, command, args) { | ||
72 | MontageBoot.loadPackage(directory) | ||
73 | .then(function (require) { | ||
74 | var id = program.slice(directory.length + 1); | ||
75 | return require.async(id); | ||
76 | }) | ||
77 | .end(); | ||
78 | }; | ||
79 | |||