/
gulp-utils.js
78 lines (70 loc) · 2.14 KB
/
gulp-utils.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
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
function generateBindings(inputFile, outputFile, callback) {
const asc = getAsc();
asc.main([
inputFile,
"--baseDir", "assembly",
"--nearFile", outputFile,
"--measure"
], callback);
}
function compile(inputFile, outputFile, callback) {
const asc = getAsc();
asc.main([
inputFile,
"-O3",
"--baseDir", "assembly",
"--binaryFile", outputFile,
"--sourceMap",
"--measure"
], callback);
}
let asc;
function getAsc() {
if (asc) {
return asc;
}
asc = require("assemblyscript/bin/asc");
if (asc.runningInStudio) {
return asc;
}
const fs = require("fs");
const pathModule = require("path");
asc.main = (main => (args, options, fn) => {
if (typeof options === "function") {
fn = options;
options = undefined;
}
return main(args, options || {
stdout: process.stdout,
stderr: process.stderr,
readFile: (filename, baseDir) => {
baseDir = pathModule.relative(process.cwd(), baseDir);
let path = pathModule.join(baseDir, filename);
if (path.startsWith("out/") && path.indexOf(".near.ts") == -1) {
path = path.replace(/^out/, baseDir );
} else if (path.startsWith(baseDir) && path.indexOf(".near.ts") != -1) {
path = path.replace(new RegExp("^" + baseDir), "out");
}
if (!fs.existsSync(path)) {
// TODO: Try node_modules instead of fixed hardcode
const mapping = {
"assembly/near.ts" : "./node_modules/near-runtime-ts/near.ts",
"assembly/json/encoder.ts" : "./node_modules/assemblyscript-json/assembly/encoder.ts",
"assembly/json/decoder.ts" : "./node_modules/assemblyscript-json/assembly/decoder.ts",
}
if (path in mapping) {
path = mapping[path]
}
}
return fs.readFileSync(path).toString("utf8");
},
writeFile: (filename, contents) => {
const name = filename.startsWith("../") ? filename.substring(3) : filename;
fs.writeFileSync(name, contents);
},
listFiles: () => []
}, fn);
})(asc.main);
return asc;
}
module.exports = { generateBindings, compile };