Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/narwhal.js
blob: 4967afb7e6d0f0071ba4b9263b132111dbbf1342 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
(function (system) {

// global reference
global = system.global;
global.global = global;
global.system = system;
global.print = system.print;

// logger shim
var logFake = function () {
    if (system.debug) {
        system.print(Array.prototype.join.apply(arguments, [" "]));
    }
};
var log = {fatal:logFake, error:logFake, warn:logFake, info:logFake, debug:logFake};
system.log = log;

// this only works for modules with no dependencies and a known absolute path
var requireFake = function(id, path, modules) {
    modules = modules || {};
    var exports = {};
    var module = {id: id, path: path};

    var factory = system.evaluate(system.fs.read(path), path, 1);
    factory(
        function(id) { return modules[id]; }, // require
        exports, // exports
        module, // module
        system, // system
        system.print // print
    );

    return exports;
}

// bootstrap sandbox module
var sandbox = requireFake("sandbox", system.prefix + "/lib/sandbox.js");

// bootstrap file module
var fs = {};
requireFake("sandbox", system.prefix + "/lib/file-bootstrap.js", { "file" : fs });
for (var name in system.fs) {
    if (Object.prototype.hasOwnProperty.call(system.fs, name)) {
        fs[name] = system.fs[name];
    }
}
system.fs = fs;

// construct the initial paths
var paths = [];
for (var i = 0; i < system.platforms.length; i++) {
    var platform = system.platforms[i];
    paths.push(system.prefix + '/platforms/' + platform + '/lib');
}
paths.push(system.prefix + '/lib');

// create the primary Loader and Sandbox:
var loader = sandbox.MultiLoader({
    paths: paths,
    debug: system.verbose
});
if (system.loaders) {
    loader.loaders.unshift.apply(loader.loaders, system.loaders);
    delete system.loaders;
}
var modules = {system: system, sandbox: sandbox};
global.require = sandbox.Sandbox({
    loader: loader,
    modules: modules,
    debug: system.verbose
});

// patch the primordials (or: save the whales)
// to bring them up to at least the neighborhood of ES5 compliance.
try {
    require("global");
} catch (e) {
    system.log.error("Couldn't load global/primordial patches ("+e+")");
}

// load the complete system module
global.require.force("system");

// augment the path search array with those provided in
//  environment variables
paths.push([
    system.env.JS_PATH || '',
    system.env.NARWHAL_PATH || ''
].join(':').split(':').filter(function (path) {
    return !!path;
}));

// parse command line options
var parser = require("narwhal").parser;
var options = parser.parse(system.args);
system.packagePrefixes = system.packagePrefixes ||
    [system.prefix];
if (options.debug !== undefined)
    system.debug = options.debug;
var wasVerbose = system.verbose;
if (options.verbose !== undefined) {
    system.verbose = options.verbose;
    require.verbose = system.verbose;
}

// enable loader tracing
global.require.debug = options.verbose;
// in verbose mode, list all the modules that are 
// already loaded
if (!wasVerbose && system.verbose) {
    Object.keys(modules).forEach(function (name) {
        print('@ ' + name);
    });
}

// find the program module and its prefix
var program;
if (system.args.length && !options.interactive && !options.main) {
    if (!program)
        program = system.fs.path(system.args[0]).canonical();

    // add package prefixes for all of the packages
    // containing the program, from specific to general
    var parts = system.fs.split(program);
    for (var i = 0; i < parts.length; i++) {
        var path = system.fs.join.apply(null, parts.slice(0, i));
        var packageJson = system.fs.join(path, 'package.json');
        if (system.fs.isFile(packageJson))
            system.packagePrefixes.unshift(path);
    }

    if (program.isDirectory()) {
        if (!program.join('package.json').isFile())
            throw new Error("Program directory does not contain a package.json");
        system.packagePrefixes.unshift(program);
    }
}

// user package prefixes
if (system.env.PACKAGE_HOME)
    system.packagePrefixes.unshift(system.env.PACKAGE_HOME);
system.packagePrefixes.unshift.apply(system.packagePrefixes, options.packagePrefixes);

// load packages
var packages;
if (!options.noPackages) {
    try {
        packages = require("packages");
        packages.main();
    } catch (e) {
        system.log.error("Warning: Couldn't load packages. Packages won't be available. ("+e+")");
    }
} else {
    packages = {
        catalog: {},
        packageOrder: []
    }
}

// run command options
//  -I, --include lib
//  -r, --require module
//  -e, -c , --command command
//  -:, --path delimiter
options.todo.forEach(function (item) {
    var action = item[0];
    var value = item[1];
    if (action == "include") {
        require.paths.unshift(value);
    } else if (action == "require") {
        require(value);
    } else if (action == "eval") {
        system.evalGlobal(value);
    } else if (action == "path") {
        var paths = packages.packageOrder.map(function (pkg) {
            return pkg.directory.join('bin');
        }).filter(function (path) {
            return path.isDirectory();
        });
        var oldPaths = system.env.PATH.split(value);
        while (oldPaths.length) {
            var path = oldPaths.shift();
            if (paths.indexOf(path) < 0)
                paths.push(path);
        }
        print(paths.join(value));
    }
});

// load the program module
if (options.interactive) {
    require('narwhal/repl');
} else if (options.main) {
    require.main(options.main);
} else if (program) {
    if (program.isDirectory()) {
        require.main(packages.root.directory.resolve(packages.root.main || 'main').toString());
    } else {
        require.main(program.toString());
    }
}

// send an unload event if that module has been required
if (require.loader.isLoaded('unload')) {
    require('unload').send();
}

})