mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-18 01:11:32 +00:00
Implement asc in js for dist
This commit is contained in:
139
bin/asc.js
139
bin/asc.js
@ -1,2 +1,137 @@
|
||||
require("ts-node").register({ project: require("path").join(__dirname, "..", "src") });
|
||||
require("./asc.ts");
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var minimist = require("minimist");
|
||||
|
||||
var assemblyscript;
|
||||
var isDev = true;
|
||||
try {
|
||||
assemblyscript = require("../dist/assemblyscript.js");
|
||||
require("source-map-support").install();
|
||||
isDev = false;
|
||||
} catch (e) {
|
||||
require("ts-node").register({ project: require("path").join(__dirname, "..", "src") });
|
||||
require("../src/glue/js");
|
||||
assemblyscript = require("../src");
|
||||
}
|
||||
|
||||
var conf = require("./asc.json");
|
||||
var opts = {};
|
||||
|
||||
Object.keys(conf).forEach(key => {
|
||||
var opt = conf[key];
|
||||
if (opt.aliases)
|
||||
(opts.alias || (opts.alias = {}))[key] = opt.aliases;
|
||||
if (opt.default !== undefined)
|
||||
(opts.default || (opts.default = {}))[key] = opt.default;
|
||||
if (opt.type === "string")
|
||||
(opts.string || (opts.string = [])).push(key);
|
||||
else if (opt.type === "boolean")
|
||||
(opts.boolean || (opts.boolean = [])).push(key);
|
||||
});
|
||||
|
||||
var args = minimist(process.argv.slice(2), opts);
|
||||
var version = require("../package.json").version;
|
||||
if (isDev) version += "-dev";
|
||||
|
||||
if (args.version) {
|
||||
console.log([
|
||||
"Version " + version
|
||||
].join("\n"));
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (args.help || args._.length < 1) {
|
||||
var options = [];
|
||||
Object.keys(conf).forEach(name => {
|
||||
var option = conf[name];
|
||||
var text = "";
|
||||
if (option.aliases) {
|
||||
option.aliases.forEach((alias, i) => {
|
||||
if (i > 0)
|
||||
text += ", ";
|
||||
text += "-" + alias;
|
||||
});
|
||||
text += ", ";
|
||||
}
|
||||
text += "--" + name;
|
||||
while (text.length < 20)
|
||||
text += " ";
|
||||
options.push(text + option.desc);
|
||||
});
|
||||
console.log([
|
||||
"Version " + version,
|
||||
"Syntax: asc [options] [file ...]",
|
||||
"",
|
||||
"Examples: asc hello.ts",
|
||||
"",
|
||||
"Options:"
|
||||
].concat(options).join("\n"));
|
||||
process.exit(args.help ? 0 : 1);
|
||||
}
|
||||
|
||||
var entryPath = args._[0];
|
||||
var entryText = fs.readFileSync(entryPath, { encoding: "utf8" });
|
||||
|
||||
var parser = assemblyscript.parseFile(entryText, entryPath);
|
||||
|
||||
var nextPath;
|
||||
var nextText;
|
||||
|
||||
while ((nextPath = parser.nextFile()) != null) {
|
||||
try {
|
||||
nextText = fs.readFileSync(path.join(path.dirname(entryPath), nextPath + ".ts"), { encoding: "utf8" });
|
||||
} catch (e) {
|
||||
nextText = fs.readFileSync(path.join(path.dirname(entryPath), nextPath, "index.ts"), { encoding: "utf8" });
|
||||
}
|
||||
assemblyscript.parseFile(nextText, nextPath, parser);
|
||||
}
|
||||
|
||||
var diagnostic;
|
||||
var hasErrors = false;
|
||||
|
||||
while ((diagnostic = assemblyscript.nextDiagnostic(parser)) != null) {
|
||||
console.error(assemblyscript.formatDiagnostic(diagnostic, process.stderr.isTTY, true));
|
||||
if (assemblyscript.isError(diagnostic))
|
||||
hasErrors = true;
|
||||
}
|
||||
|
||||
if (hasErrors)
|
||||
process.exit(1);
|
||||
|
||||
var module = assemblyscript.compile(parser);
|
||||
|
||||
hasErrors = false;
|
||||
while ((diagnostic = assemblyscript.nextDiagnostic(parser)) != null) {
|
||||
console.error(assemblyscript.formatDiagnostic(diagnostic, process.stderr.isTTY, true));
|
||||
if (assemblyscript.isError(diagnostic))
|
||||
hasErrors = true;
|
||||
}
|
||||
|
||||
if (hasErrors) {
|
||||
module.dispose();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (args.validate)
|
||||
if (!module.validate()) {
|
||||
module.dispose();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (args.optimize)
|
||||
module.optimize();
|
||||
|
||||
var hasOutput = false;
|
||||
|
||||
if (args.outFile != null) {
|
||||
fs.writeFileSync(args.outFile, module.toBinary());
|
||||
hasOutput = true;
|
||||
}
|
||||
if (args.textFile != null) {
|
||||
fs.writeFileSync(args.textFile, module.toText(), { encoding: "utf8" });
|
||||
hasOutput = true;
|
||||
}
|
||||
if (!hasOutput)
|
||||
module.print();
|
||||
|
||||
module.dispose();
|
||||
|
Reference in New Issue
Block a user