-
Notifications
You must be signed in to change notification settings - Fork 92
/
gulp-utils.js
88 lines (79 loc) · 2.91 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
79
80
81
82
83
84
85
86
87
88
// FUTURE PEOPLE: This file is called "gulp-utils" but it's not related to the deprecated library called "gulp-utils". Don't panic.
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",
"--runtime", "stub"
], callback);
}
let asc;
function getAsc() {
if (asc) {
return asc;
}
asc = require("assemblyscript/bin/asc");
const fs = require("fs");
const pathModule = require("path");
asc.main = (main => (args, options, fn) => {
if (typeof options === "function") {
fn = options;
options = undefined;
}
const logLn = process.browser ? window.logLn : console.log;
return main(args, options || {
stdout: process.stdout || asc.createMemoryStream(logLn),
stderr: process.stderr || asc.createMemoryStream(logLn),
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",
"bignum/integer/u128.ts" : "./node_modules/bignum/assembly/integer/u128.ts",
};
if (path in mapping) {
path = mapping[path]
} else if (path.startsWith("assembly/node_modules/bignum/assembly")) {
// TODO: resolve two ways of importing bignum due to need to test near-runtime-ts separately
path = path.replace("assembly", ".");
} else if (path.startsWith("assembly/bignum")) {
path = path.replace("assembly/bignum", "./node_modules/bignum/assembly");
}
}
if (!fs.existsSync(path)) {
return null;
}
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 };