2019-01-21 14:03:30 -05:00
|
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
|
import { core } from "./core.ts";
|
|
|
|
|
import * as dispatch from "./dispatch.ts";
|
|
|
|
|
import { sendSync } from "./dispatch_json.ts";
|
|
|
|
|
import { assert } from "./util.ts";
|
|
|
|
|
import * as util from "./util.ts";
|
|
|
|
|
import { window } from "./window.ts";
|
2019-09-06 12:57:15 -04:00
|
|
|
|
import { OperatingSystem, Arch } from "./build.ts";
|
2018-07-06 11:20:35 -04: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 } {
|
2019-08-26 08:50:21 -04:00
|
|
|
|
return sendSync(dispatch.OP_IS_TTY);
|
2019-02-02 22:05:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 19:09:42 -04:00
|
|
|
|
/** Get the hostname.
|
|
|
|
|
* Requires the `--allow-env` flag.
|
|
|
|
|
*
|
|
|
|
|
* console.log(Deno.hostname());
|
|
|
|
|
*/
|
|
|
|
|
export function hostname(): string {
|
|
|
|
|
return sendSync(dispatch.OP_HOSTNAME);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
|
/** Exit the Deno process with optional exit code. */
|
2019-08-23 01:30:14 -04:00
|
|
|
|
export function exit(code = 0): never {
|
2019-08-26 08:50:21 -04:00
|
|
|
|
sendSync(dispatch.OP_EXIT, { code });
|
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 {
|
2019-08-26 08:50:21 -04:00
|
|
|
|
sendSync(dispatch.OP_SET_ENV, { key, value });
|
2019-03-09 12:30:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-02 11:55:28 -04:00
|
|
|
|
function getEnv(key: string): string | undefined {
|
|
|
|
|
return sendSync(dispatch.OP_GET_ENV, { key })[0];
|
|
|
|
|
}
|
|
|
|
|
|
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-10-02 11:55:28 -04:00
|
|
|
|
* console.log(Deno.env("SHELL"));
|
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
|
|
|
|
*/
|
2019-10-02 11:55:28 -04:00
|
|
|
|
export function env(): { [index: string]: string };
|
|
|
|
|
export function env(key: string): string | undefined;
|
|
|
|
|
export function env(
|
|
|
|
|
key?: string
|
|
|
|
|
): { [index: string]: string } | string | undefined {
|
|
|
|
|
if (key) {
|
|
|
|
|
return getEnv(key);
|
|
|
|
|
}
|
2019-08-26 08:50:21 -04:00
|
|
|
|
const env = sendSync(dispatch.OP_ENV);
|
2019-08-23 01:30:14 -04:00
|
|
|
|
return new Proxy(env, {
|
|
|
|
|
set(obj, prop: string, value: string): boolean {
|
|
|
|
|
setEnv(prop, value);
|
|
|
|
|
return Reflect.set(obj, prop, value);
|
|
|
|
|
}
|
2018-08-31 07:51:12 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
2019-01-28 20:41:28 -05:00
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
|
interface Start {
|
|
|
|
|
cwd: string;
|
|
|
|
|
pid: number;
|
|
|
|
|
argv: string[];
|
|
|
|
|
mainModule: string; // Absolute URL.
|
|
|
|
|
debugFlag: boolean;
|
|
|
|
|
depsFlag: boolean;
|
|
|
|
|
typesFlag: boolean;
|
|
|
|
|
versionFlag: boolean;
|
|
|
|
|
denoVersion: string;
|
|
|
|
|
v8Version: string;
|
2019-09-03 22:12:21 -04:00
|
|
|
|
tsVersion: string;
|
2019-08-26 08:50:21 -04:00
|
|
|
|
noColor: boolean;
|
2019-09-06 12:57:15 -04:00
|
|
|
|
os: OperatingSystem;
|
|
|
|
|
arch: Arch;
|
2019-01-28 20:41:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function bootstraps an environment within Deno, it is shared both by
|
|
|
|
|
// the runtime and the compiler environments.
|
|
|
|
|
// @internal
|
2019-08-26 08:50:21 -04:00
|
|
|
|
export function start(preserveDenoNamespace = true, source?: string): Start {
|
2019-08-23 01:30:14 -04:00
|
|
|
|
core.setAsyncHandler(dispatch.asyncMsgFromRust);
|
2019-10-01 18:51:05 -04:00
|
|
|
|
const ops = core.ops();
|
|
|
|
|
// TODO(bartlomieju): this is a prototype, we should come up with
|
|
|
|
|
// something a bit more sophisticated
|
|
|
|
|
for (const [name, opId] of Object.entries(ops)) {
|
|
|
|
|
const opName = `OP_${name.toUpperCase()}`;
|
|
|
|
|
// Assign op ids to actual variables
|
2019-11-14 15:05:36 -05:00
|
|
|
|
// TODO(ry) This type casting is gross and should be fixed.
|
|
|
|
|
((dispatch as unknown) as { [key: string]: number })[opName] = opId;
|
2019-10-01 18:51:05 -04:00
|
|
|
|
}
|
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.
|
2019-11-13 10:35:56 -05:00
|
|
|
|
const startResponse = sendSync(dispatch.OP_START);
|
|
|
|
|
const { pid, noColor, debugFlag } = startResponse;
|
2019-01-28 20:41:28 -05:00
|
|
|
|
|
2019-11-13 10:35:56 -05:00
|
|
|
|
util.setLogDebug(debugFlag, source);
|
2019-01-28 20:41:28 -05:00
|
|
|
|
|
2019-09-02 17:07:11 -04:00
|
|
|
|
// pid and noColor need to be set in the Deno module before it's set to be
|
|
|
|
|
// frozen.
|
2019-11-13 10:35:56 -05:00
|
|
|
|
util.immutableDefine(window.Deno, "pid", pid);
|
|
|
|
|
util.immutableDefine(window.Deno, "noColor", noColor);
|
2019-09-02 17:07:11 -04:00
|
|
|
|
Object.freeze(window.Deno);
|
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-11-13 10:35:56 -05:00
|
|
|
|
return startResponse;
|
2019-01-28 20:41:28 -05:00
|
|
|
|
}
|
2019-06-25 12:05:41 -04:00
|
|
|
|
|
2019-12-18 09:29:00 -05:00
|
|
|
|
type DirKind =
|
|
|
|
|
| "home"
|
|
|
|
|
| "cache"
|
|
|
|
|
| "config"
|
|
|
|
|
| "data"
|
|
|
|
|
| "data_local"
|
|
|
|
|
| "audio"
|
|
|
|
|
| "desktop"
|
|
|
|
|
| "document"
|
|
|
|
|
| "download"
|
|
|
|
|
| "font"
|
|
|
|
|
| "picture"
|
|
|
|
|
| "public"
|
|
|
|
|
| "template"
|
|
|
|
|
| "video";
|
2019-12-15 00:14:20 -05:00
|
|
|
|
|
|
|
|
|
/**
|
2019-12-18 09:29:00 -05:00
|
|
|
|
* Returns the user and platform specific directories.
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* Requires the `--allow-env` flag.
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* Argument values: "home", "cache", "config", "data", "data_local", "audio",
|
|
|
|
|
* "desktop", "document", "download", "font", "picture", "public", "template",
|
|
|
|
|
* "video"
|
|
|
|
|
*
|
|
|
|
|
* "cache"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ----------------------------------- | ---------------------------- |
|
|
|
|
|
* | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache |
|
|
|
|
|
* | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches |
|
|
|
|
|
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "config"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ------------------------------------- | -------------------------------- |
|
|
|
|
|
* | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config |
|
|
|
|
|
* | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences |
|
|
|
|
|
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "data"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ---------------------------------------- | ---------------------------------------- |
|
|
|
|
|
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
|
|
|
|
|
* | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
|
|
|
|
|
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "data_local"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ---------------------------------------- | ---------------------------------------- |
|
|
|
|
|
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
|
|
|
|
|
* | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
|
|
|
|
|
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "audio"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ------------------ | -------------------- |
|
|
|
|
|
* | Linux | `XDG_MUSIC_DIR` | /home/alice/Music |
|
|
|
|
|
* | macOS | `$HOME`/Music | /Users/Alice/Music |
|
|
|
|
|
* | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "desktop"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | -------------------- | ---------------------- |
|
|
|
|
|
* | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop |
|
|
|
|
|
* | macOS | `$HOME`/Desktop | /Users/Alice/Desktop |
|
|
|
|
|
* | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "document"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ---------------------- | ------------------------ |
|
|
|
|
|
* | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents |
|
|
|
|
|
* | macOS | `$HOME`/Documents | /Users/Alice/Documents |
|
|
|
|
|
* | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "download"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ---------------------- | ------------------------ |
|
|
|
|
|
* | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads |
|
|
|
|
|
* | macOS | `$HOME`/Downloads | /Users/Alice/Downloads |
|
|
|
|
|
* | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "font"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ---------------------------------------------------- | ------------------------------ |
|
|
|
|
|
* | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts |
|
|
|
|
|
* | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts |
|
|
|
|
|
* | Windows | – | – |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "picture"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | --------------------- | ----------------------- |
|
|
|
|
|
* | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures |
|
|
|
|
|
* | macOS | `$HOME`/Pictures | /Users/Alice/Pictures |
|
|
|
|
|
* | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "public"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | --------------------- | ------------------- |
|
|
|
|
|
* | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public |
|
|
|
|
|
* | macOS | `$HOME`/Public | /Users/Alice/Public |
|
|
|
|
|
* | Windows | `{FOLDERID_Public}` | C:\Users\Public |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "template"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ---------------------- | ---------------------------------------------------------- |
|
|
|
|
|
* | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates |
|
|
|
|
|
* | macOS | – | – |
|
|
|
|
|
* | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates |
|
2019-12-18 09:29:00 -05:00
|
|
|
|
*
|
|
|
|
|
* "video"
|
2019-12-15 00:14:20 -05:00
|
|
|
|
* |Platform | Value | Example |
|
|
|
|
|
* | ------- | ------------------- | --------------------- |
|
|
|
|
|
* | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos |
|
|
|
|
|
* | macOS | `$HOME`/Movies | /Users/Alice/Movies |
|
|
|
|
|
* | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos |
|
|
|
|
|
*/
|
2019-12-18 09:29:00 -05:00
|
|
|
|
export function dir(kind: DirKind): string {
|
|
|
|
|
return sendSync(dispatch.OP_GET_DIR, { kind });
|
2019-06-25 12:05:41 -04:00
|
|
|
|
}
|
2019-08-06 17:05:47 -04:00
|
|
|
|
|
2019-08-06 20:32:25 -04:00
|
|
|
|
/**
|
|
|
|
|
* Returns the path to the current deno executable.
|
|
|
|
|
* Requires the `--allow-env` flag.
|
|
|
|
|
*/
|
2019-08-06 17:05:47 -04:00
|
|
|
|
export function execPath(): string {
|
2019-08-26 08:50:21 -04:00
|
|
|
|
return sendSync(dispatch.OP_EXEC_PATH);
|
2019-08-06 17:05:47 -04:00
|
|
|
|
}
|