Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Add Deno.mainModule #6180

Merged
merged 5 commits into from Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli/js/lib.deno.ns.d.ts
Expand Up @@ -1899,6 +1899,9 @@ declare namespace Deno {
*/
export const args: string[];

/** The URL of the file that was originally executed from the command-line. */
export const scriptUrl: URL;

Alhadis marked this conversation as resolved.
Show resolved Hide resolved
/** A symbol which can be used as a key for a custom method which will be
* called when `Deno.inspect()` is called, or when the object is logged to
* the console. */
Expand Down
6 changes: 5 additions & 1 deletion cli/js/ops/runtime.ts
@@ -1,6 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { sendSync } from "./dispatch_json.ts";
import { URLImpl } from "../web/url.ts";

export interface Start {
args: string[];
Expand All @@ -10,6 +11,7 @@ export interface Start {
noColor: boolean;
pid: number;
repl: boolean;
scriptUrl: URL;
target: string;
tsVersion: string;
unstableFlag: boolean;
Expand All @@ -18,7 +20,9 @@ export interface Start {
}

export function opStart(): Start {
return sendSync("op_start");
const s = sendSync("op_start");
s.scriptUrl = new URLImpl(s.scriptUrl);
return s;
}

export interface Metrics {
Expand Down
3 changes: 2 additions & 1 deletion cli/js/runtime_main.ts
Expand Up @@ -95,11 +95,12 @@ export function bootstrapMainRuntime(): void {
}
});

const { args, cwd, noColor, pid, repl, unstableFlag } = runtime.start();
const { args, cwd, noColor, pid, repl, scriptUrl, unstableFlag } = runtime.start();

Object.defineProperties(denoNs, {
pid: readOnly(pid),
noColor: readOnly(noColor),
scriptUrl: readOnly(scriptUrl),
Alhadis marked this conversation as resolved.
Show resolved Hide resolved
args: readOnly(Object.freeze(args)),
});

Expand Down
1 change: 1 addition & 0 deletions cli/ops/runtime.rs
Expand Up @@ -31,6 +31,7 @@ fn op_start(
"noColor": !colors::use_color(),
"pid": std::process::id(),
"repl": gs.flags.subcommand == DenoSubcommand::Repl,
"scriptUrl": state.main_module.to_string(),
"target": env!("TARGET"),
"tsVersion": version::TYPESCRIPT,
"unstableFlag": gs.flags.unstable,
Expand Down
7 changes: 6 additions & 1 deletion cli/tests/integration_tests.rs
Expand Up @@ -442,7 +442,7 @@ fn ts_dependency_recompilation() {
function print(str: string): void {
console.log(str);
}

print(foo);",
)
.unwrap();
Expand Down Expand Up @@ -1645,6 +1645,11 @@ itest!(import_meta {
output: "import_meta.ts.out",
});

itest!(script_url {
args: "run --quiet --reload script_url.ts",
output: "script_url.ts.out",
});

itest!(lib_ref {
args: "run --quiet --unstable --reload lib_ref.ts",
output: "lib_ref.ts.out",
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/script_url.ts
@@ -0,0 +1,3 @@
console.log("script_url", Deno.scriptUrl.pathname);
Alhadis marked this conversation as resolved.
Show resolved Hide resolved

import "./script_url2.ts";
2 changes: 2 additions & 0 deletions cli/tests/script_url.ts.out
@@ -0,0 +1,2 @@
script_url2 [WILDCARD]tests/script_url.ts
script_url [WILDCARD]tests/script_url.ts
1 change: 1 addition & 0 deletions cli/tests/script_url2.ts
@@ -0,0 +1 @@
console.log("script_url2", Deno.scriptUrl.pathname);