forked from smooth80/defold
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs-web-pre.js
51 lines (44 loc) · 1.24 KB
/
js-web-pre.js
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
if (typeof window === 'undefined') {
// TODO: Remove global variables. Use the "module" pattern in js?
console.log("node.js detected")
var node_fs = require('fs')
var _created_files = {}
function preload(name) {
var str_name = UTF8ToString(name);
if (!_created_files[str_name] && node_fs.existsSync(str_name)) {
//console.log("loading file: %s", str_name);
var a = str_name.split("/")
var parent = a.slice(0,-1).join("/")
if (parent != "") {
FS.createPath("", parent, true, true);
}
// TODO: Use parent instead of analyzePath here?
var pi = FS.analyzePath(UTF8ToString(name));
var data = node_fs.readFileSync(UTF8ToString(name));
var target = FS.createDataFile(pi.parentPath, pi.name, data, true, false);
_created_files[str_name] = true;
}
}
if (typeof _fopen != 'undefined') {
_old_fopen = _fopen
_fopen = function(name, mode) {
preload(name);
var ret = _old_fopen(name, mode);
return ret;
}
}
if (typeof _stat != 'undefined') {
_old_stat = _stat
_stat = function(path, buf) {
preload(path);
var r = _old_stat(path, buf);
return r;
}
}
try {
XMLHttpRequest = require('xhr2');
console.log("xhr2 for XMLHttpRequest loaded");
} catch (err) {
console.log("xhr2 not found");
}
}