-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathweb-repl-export
More file actions
executable file
·102 lines (87 loc) · 3.51 KB
/
web-repl-export
File metadata and controls
executable file
·102 lines (87 loc) · 3.51 KB
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
#!/usr/bin/env node
// vim:ft=javascript:ts=4:et
"use strict";
var fs = require('fs');
var path = require('path');
var base = path.normalize(path.resolve(path.join(path.dirname(__dirname))));
var meta = JSON.parse(fs.readFileSync(path.join(base, 'package.json'), {'encoding':'utf-8'}));
var commit_sha = fs.readFileSync(path.join(base, '.git', 'refs', 'heads', 'master'), {'encoding':'utf-8'}).trim();
var compiler_dir = path.join(base, 'dev');
if (!path_exists(path.join(compiler_dir, 'compiler.js'))) compiler_dir = path.join(base, 'release');
var manifest = {}, total = 0;
['compiler.js', 'baselib-plain-pretty.js'].forEach(function(x) {
manifest[x] = fs.readFileSync(path.join(compiler_dir, x), {'encoding':'utf-8'});
total += manifest[x].length;
});
['web_repl.js', 'embedded_compiler.js', 'utils.js', 'completer.js', 'msgfmt.js', 'gettext.js'].forEach(function(x) {
x = 'tools/' + x;
manifest[x] = fs.readFileSync(path.join(base, x), {'encoding':'utf-8'});
total += manifest[x].length;
});
var stdlib = path.join(base, 'src', 'lib');
function path_exists(path) {
try {
fs.statSync(path);
return true;
} catch(e) {
if (e.code != 'ENOENT') throw e;
}
}
function process_dir(relpath) {
var fullpath = (relpath) ? path.join(stdlib, relpath) : stdlib;
fs.readdirSync(fullpath).forEach(function (x) {
var q = path.join(fullpath, x);
var s = fs.statSync(q);
if (s.isDirectory()) return process_dir(relpath + '/' + x);
if (!x.endsWith('.pyj')) return;
var iname = path.normalize('__stdlib__' + '/' + relpath + '/' + x);
var raw = fs.readFileSync(q, {'encoding':'utf-8'});
manifest[iname] = raw;
total += s.size;
});
}
process_dir('');
var rs = '// vim:fileencoding=utf-8\n';
rs += '(function(external_namespace) {\n';
rs += '"use strict;"\n';
rs += 'var rs_version = ' + JSON.stringify(meta.version) + ';\n';
rs += 'var rs_commit_sha = ' + JSON.stringify(commit_sha) + ';\n';
rs += '\n// Embedded modules {{{\n';
rs += 'var data = ' + JSON.stringify(manifest) + ';\n\n';
rs += '// End embedded modules }}}\n\n';
rs += fs.readFileSync(path.join(base, 'web-repl', 'env.js'));
rs += '\n// Embedded sha1 implementation {{{\n';
rs += '(function() {\n';
rs += fs.readFileSync(path.join(base, 'web-repl', 'sha1.js'));
rs += '}).call(jsSHA);\n';
rs += '// End embedded sha1 implementation }}}\n\n';
rs += 'var exports = namespace;\n';
rs += fs.readFileSync(path.join(base, 'tools', 'export.js'), {'encoding':'utf-8'});
rs += 'external_namespace.RapydScript = namespace;\n';
rs += '})(this);\n';
var base_dir = process.argv.slice(-1)[0];
if (process.argv.length !== 3) {
console.error('Usage: web-repl-export /path/to/export/directory');
process.exit(1);
}
base_dir = path.normalize(path.resolve(base_dir));
try {
fs.mkdirSync(base_dir);
} catch(e) {
if (e.code !== 'EEXIST') throw e;
}
try {
process.chdir(base_dir);
} catch(e) {
if (e.code === 'ENOTDIR') { console.error(base_dir + ' is not a directory'); process.exit(1); }
throw e;
}
fs.writeFileSync('rapydscript.js', rs, {'encoding':'utf-8'});
var web_repl = path.join(base, 'web-repl');
fs.readdirSync(web_repl).forEach(function(x) {
if (['sha1.js', 'env.js'].indexOf(x) !== -1) return;
var data = fs.readFileSync(path.join(web_repl, x), {'encoding':'utf-8'});
fs.writeFileSync(x, data, {'encoding':'utf-8'});
});
console.log('RapydScript compiler (uncompressed) size: ' + (total/(1024)).toFixed(1) + ' KB');
console.log('web-repl exported to: ' + base_dir);