2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-30 14:45:36 -04:00
|
|
|
import * as msg from "gen/cli/msg_generated";
|
2019-03-26 08:22:07 -04:00
|
|
|
import { core } from "./core";
|
2019-01-28 20:41:28 -05:00
|
|
|
import { handleAsyncMsgFromRust, sendSync } from "./dispatch";
|
|
|
|
import * as flatbuffers from "./flatbuffers";
|
2018-07-13 03:24:07 -04:00
|
|
|
import { assert } from "./util";
|
|
|
|
import * as util from "./util";
|
2019-04-19 20:39:54 -04:00
|
|
|
import { window } from "./window";
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
/** The current process id of the runtime. */
|
2019-01-06 14:16:42 -05:00
|
|
|
export let pid: number;
|
|
|
|
|
2019-02-12 10:08:56 -05:00
|
|
|
/** Reflects the NO_COLOR environment variable: https://no-color.org/ */
|
2019-02-08 22:13:04 -05:00
|
|
|
export let noColor: boolean;
|
|
|
|
|
2019-08-06 01:45:36 -04:00
|
|
|
/** Path to the current deno process's executable file.
|
|
|
|
* Requires the `--allow-env` flag, otherwise it'll be set to an empty `string`.
|
|
|
|
*/
|
2019-02-15 11:22:02 -05:00
|
|
|
export let execPath: string;
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
function setGlobals(pid_: number, noColor_: boolean, execPath_: string): void {
|
2019-01-06 14:16:42 -05:00
|
|
|
assert(!pid);
|
|
|
|
pid = pid_;
|
2019-02-08 22:13:04 -05:00
|
|
|
noColor = noColor_;
|
2019-02-15 11:22:02 -05:00
|
|
|
execPath = execPath_;
|
2019-01-06 14:16:42 -05:00
|
|
|
}
|
|
|
|
|
2019-02-02 22:05:30 -05:00
|
|
|
/** Check if running in terminal.
|
|
|
|
*
|
2019-02-12 10:08:56 -05:00
|
|
|
* console.log(Deno.isTTY().stdout);
|
2019-02-02 22:05:30 -05:00
|
|
|
*/
|
|
|
|
export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.IsTTY.createIsTTY(builder);
|
2019-02-02 22:05:30 -05:00
|
|
|
const baseRes = sendSync(builder, msg.Any.IsTTY, inner)!;
|
|
|
|
assert(msg.Any.IsTTYRes === baseRes.innerType());
|
|
|
|
const res = new msg.IsTTYRes();
|
|
|
|
assert(baseRes.inner(res) != null);
|
|
|
|
|
|
|
|
return { stdin: res.stdin(), stdout: res.stdout(), stderr: res.stderr() };
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Exit the Deno process with optional exit code. */
|
2018-08-01 15:56:27 -04:00
|
|
|
export function exit(exitCode = 0): never {
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.Exit.createExit(builder, exitCode);
|
2018-10-03 21:18:23 -04:00
|
|
|
sendSync(builder, msg.Any.Exit, inner);
|
2018-08-19 15:04:27 -04:00
|
|
|
return util.unreachable();
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function setEnv(key: string, value: string): void {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const key_ = builder.createString(key);
|
|
|
|
const value_ = builder.createString(value);
|
|
|
|
const inner = msg.SetEnv.createSetEnv(builder, key_, value_);
|
2019-03-09 12:30:38 -05:00
|
|
|
sendSync(builder, msg.Any.SetEnv, inner);
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:09:10 -04:00
|
|
|
function createEnv(inner: msg.EnvironRes): { [index: string]: string } {
|
2018-09-01 10:45:26 -04:00
|
|
|
const env: { [index: string]: string } = {};
|
2018-08-31 07:51:12 -04:00
|
|
|
|
2018-11-02 20:09:10 -04:00
|
|
|
for (let i = 0; i < inner.mapLength(); i++) {
|
|
|
|
const item = inner.map(i)!;
|
2018-08-31 07:51:12 -04:00
|
|
|
env[item.key()!] = item.value()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Proxy(env, {
|
2019-04-21 16:40:10 -04:00
|
|
|
set(obj, prop: string, value: string): boolean {
|
2019-01-23 20:29:18 -05:00
|
|
|
setEnv(prop, value);
|
2018-08-31 07:51:12 -04:00
|
|
|
return Reflect.set(obj, prop, value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns a snapshot of the environment variables at invocation. Mutating a
|
2018-08-31 07:51:12 -04:00
|
|
|
* property in the object will set that variable in the environment for
|
2019-01-23 20:29:18 -05:00
|
|
|
* the process. The environment object will only accept `string`s
|
2018-08-31 07:51:12 -04:00
|
|
|
* as values.
|
|
|
|
*
|
2019-02-12 10:08:56 -05:00
|
|
|
* const myEnv = Deno.env();
|
2018-10-14 16:29:50 -04:00
|
|
|
* console.log(myEnv.SHELL);
|
|
|
|
* myEnv.TEST_VAR = "HELLO";
|
2019-02-12 10:08:56 -05:00
|
|
|
* const newEnv = Deno.env();
|
2018-10-14 16:29:50 -04:00
|
|
|
* console.log(myEnv.TEST_VAR == newEnv.TEST_VAR);
|
2018-08-31 07:51:12 -04:00
|
|
|
*/
|
2018-09-01 10:45:26 -04:00
|
|
|
export function env(): { [index: string]: string } {
|
2018-08-31 07:51:12 -04:00
|
|
|
/* Ideally we could write
|
2018-09-09 18:54:42 -04:00
|
|
|
const res = sendSync({
|
2018-10-03 21:18:23 -04:00
|
|
|
command: msg.Command.ENV,
|
2018-08-31 07:51:12 -04:00
|
|
|
});
|
|
|
|
*/
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.Environ.createEnviron(builder);
|
2018-10-03 21:18:23 -04:00
|
|
|
const baseRes = sendSync(builder, msg.Any.Environ, inner)!;
|
|
|
|
assert(msg.Any.EnvironRes === baseRes.innerType());
|
|
|
|
const res = new msg.EnvironRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes.inner(res) != null);
|
2018-08-31 07:51:12 -04:00
|
|
|
// TypeScript cannot track assertion above, therefore not null assertion
|
|
|
|
return createEnv(res);
|
|
|
|
}
|
2019-01-28 20:41:28 -05:00
|
|
|
|
|
|
|
/** Send to the privileged side that we have setup and are ready. */
|
|
|
|
function sendStart(): msg.StartRes {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const startOffset = msg.Start.createStart(builder, 0 /* unused */);
|
2019-01-28 20:41:28 -05:00
|
|
|
const baseRes = sendSync(builder, msg.Any.Start, startOffset);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(msg.Any.StartRes === baseRes!.innerType());
|
|
|
|
const startResMsg = new msg.StartRes();
|
|
|
|
assert(baseRes!.inner(startResMsg) != null);
|
|
|
|
return startResMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function bootstraps an environment within Deno, it is shared both by
|
|
|
|
// the runtime and the compiler environments.
|
|
|
|
// @internal
|
2019-08-05 07:23:41 -04:00
|
|
|
export function start(
|
|
|
|
preserveDenoNamespace = true,
|
|
|
|
source?: string
|
|
|
|
): msg.StartRes {
|
2019-03-26 08:22:07 -04:00
|
|
|
core.setAsyncHandler(handleAsyncMsgFromRust);
|
2019-01-28 20:41:28 -05: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();
|
|
|
|
|
2019-02-02 18:27:53 -05:00
|
|
|
util.setLogDebug(startResMsg.debugFlag(), source);
|
2019-01-28 20:41:28 -05:00
|
|
|
|
2019-02-15 11:22:02 -05:00
|
|
|
setGlobals(startResMsg.pid(), startResMsg.noColor(), startResMsg.execPath()!);
|
2019-02-10 20:07:02 -05:00
|
|
|
|
2019-08-05 07:23:41 -04:00
|
|
|
if (preserveDenoNamespace) {
|
|
|
|
util.immutableDefine(window, "Deno", window.Deno);
|
|
|
|
// Deno.core could ONLY be safely frozen here (not in globals.ts)
|
|
|
|
// since shared_queue.js will modify core properties.
|
|
|
|
Object.freeze(window.Deno.core);
|
|
|
|
// core.sharedQueue is an object so we should also freeze it.
|
|
|
|
Object.freeze(window.Deno.core.sharedQueue);
|
|
|
|
} else {
|
|
|
|
// Remove window.Deno
|
|
|
|
delete window.Deno;
|
|
|
|
assert(window.Deno === undefined);
|
|
|
|
}
|
2019-04-19 20:39:54 -04:00
|
|
|
|
2019-01-28 20:41:28 -05:00
|
|
|
return startResMsg;
|
|
|
|
}
|
2019-06-25 12:05:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current user's home directory.
|
2019-08-06 01:45:36 -04:00
|
|
|
* Requires the `--allow-env` flag.
|
2019-06-25 12:05:41 -04:00
|
|
|
*/
|
|
|
|
export function homeDir(): string {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
|
|
|
const inner = msg.HomeDir.createHomeDir(builder);
|
|
|
|
const baseRes = sendSync(builder, msg.Any.HomeDir, inner)!;
|
|
|
|
assert(msg.Any.HomeDirRes === baseRes.innerType());
|
|
|
|
const res = new msg.HomeDirRes();
|
|
|
|
assert(baseRes.inner(res) != null);
|
|
|
|
const path = res.path();
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
throw new Error("Could not get home directory.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|