1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 02:29:06 -05:00
denoland-deno/js/main.ts

74 lines
2 KiB
TypeScript
Raw Normal View History

// Copyright 2018 the Deno authors. All rights reserved. MIT license.
2018-07-04 14:50:28 -04:00
import { flatbuffers } from "flatbuffers";
2018-09-09 19:21:22 -04:00
import * as fbs from "gen/msg_generated";
import { assert, log, setLogDebug } from "./util";
import * as os from "./os";
import { DenoCompiler } from "./compiler";
import { libdeno } from "./libdeno";
import { args } from "./deno";
import { sendSync, handleAsyncMsgFromRust } from "./dispatch";
2018-06-11 21:54:55 -04:00
function sendStart(): fbs.StartRes {
const builder = new flatbuffers.Builder();
fbs.Start.startStart(builder);
const startOffset = fbs.Start.endStart(builder);
const baseRes = sendSync(builder, fbs.Any.Start, startOffset);
assert(baseRes != null);
assert(fbs.Any.StartRes === baseRes!.msgType());
const startRes = new fbs.StartRes();
assert(baseRes!.msg(startRes) != null);
return startRes;
}
function onGlobalError(
message: string,
source: string,
lineno: number,
colno: number,
error: Error
) {
console.log(error.stack);
os.exit(1);
}
2018-07-21 21:14:52 -04:00
/* tslint:disable-next-line:no-default-export */
export default function denoMain() {
libdeno.recv(handleAsyncMsgFromRust);
libdeno.setGlobalErrorHandler(onGlobalError);
const compiler = DenoCompiler.instance();
2018-09-24 15:33:50 -04:00
// First we send an empty "Start" message to let the privileged side know we
// are ready. The response should be a "StartRes" message containing the CLI
// args and other info.
const startResMsg = sendStart();
setLogDebug(startResMsg.debugFlag());
const cwd = startResMsg.cwd();
log("cwd", cwd);
2018-06-13 19:40:35 -04:00
2018-08-23 19:46:21 -04:00
// TODO handle shebang.
for (let i = 1; i < startResMsg.argvLength(); i++) {
args.push(startResMsg.argv(i));
2018-06-13 19:40:35 -04:00
}
log("args", args);
Object.freeze(args);
const inputFn = args[0];
if (!inputFn) {
console.log("No input script specified.");
os.exit(1);
}
const printDeps = startResMsg.depsFlag();
if (printDeps) {
for (const dep of compiler.getModuleDependencies(inputFn, `${cwd}/`)) {
console.log(dep);
}
os.exit(0);
}
2018-09-24 15:33:50 -04:00
compiler.recompile = startResMsg.recompileFlag();
compiler.run(inputFn, `${cwd}/`);
2018-07-21 21:14:52 -04:00
}