summaryrefslogtreecommitdiff
path: root/runtime/js/41_prompt.js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-02-07 20:22:46 +0100
committerGitHub <noreply@github.com>2023-02-07 20:22:46 +0100
commitb4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch)
tree3d008912affe8550692183bd2697a386db5e3c79 /runtime/js/41_prompt.js
parent65500f36e870b4ada3996b06aa287e30177d21a3 (diff)
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as ES modules. `__bootstrap`has been mostly replaced with static imports in form in `internal:[path to file from repo root]`. To specify if files are ESM, an `esm` method has been added to `Extension`, similar to the `js` method. A new ModuleLoader called `InternalModuleLoader` has been added to enable the loading of internal specifiers, which is used in all situations except when a snapshot is only loaded, and not a new one is created from it. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'runtime/js/41_prompt.js')
-rw-r--r--runtime/js/41_prompt.js112
1 files changed, 53 insertions, 59 deletions
diff --git a/runtime/js/41_prompt.js b/runtime/js/41_prompt.js
index 1d5acc028..441db9a2f 100644
--- a/runtime/js/41_prompt.js
+++ b/runtime/js/41_prompt.js
@@ -1,82 +1,76 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-"use strict";
-((window) => {
- const { stdin } = window.__bootstrap.files;
- const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } =
- window.__bootstrap.primordials;
- const { isatty } = window.__bootstrap.tty;
- const LF = StringPrototypeCharCodeAt("\n", 0);
- const CR = StringPrototypeCharCodeAt("\r", 0);
- const core = window.Deno.core;
+const core = globalThis.Deno.core;
+const primordials = globalThis.__bootstrap.primordials;
+import { isatty } from "internal:runtime/js/40_tty.js";
+import { stdin } from "internal:runtime/js/40_files.js";
+const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } =
+ primordials;
+const LF = StringPrototypeCharCodeAt("\n", 0);
+const CR = StringPrototypeCharCodeAt("\r", 0);
- function alert(message = "Alert") {
- if (!isatty(stdin.rid)) {
- return;
- }
-
- core.print(`${message} [Enter] `, false);
-
- readLineFromStdinSync();
+function alert(message = "Alert") {
+ if (!isatty(stdin.rid)) {
+ return;
}
- function confirm(message = "Confirm") {
- if (!isatty(stdin.rid)) {
- return false;
- }
-
- core.print(`${message} [y/N] `, false);
+ core.print(`${message} [Enter] `, false);
- const answer = readLineFromStdinSync();
+ readLineFromStdinSync();
+}
- return answer === "Y" || answer === "y";
+function confirm(message = "Confirm") {
+ if (!isatty(stdin.rid)) {
+ return false;
}
- function prompt(message = "Prompt", defaultValue) {
- defaultValue ??= null;
+ core.print(`${message} [y/N] `, false);
- if (!isatty(stdin.rid)) {
- return null;
- }
+ const answer = readLineFromStdinSync();
- core.print(`${message} `, false);
+ return answer === "Y" || answer === "y";
+}
- if (defaultValue) {
- core.print(`[${defaultValue}] `, false);
- }
+function prompt(message = "Prompt", defaultValue) {
+ defaultValue ??= null;
+
+ if (!isatty(stdin.rid)) {
+ return null;
+ }
- return readLineFromStdinSync() || defaultValue;
+ core.print(`${message} `, false);
+
+ if (defaultValue) {
+ core.print(`[${defaultValue}] `, false);
}
- function readLineFromStdinSync() {
- const c = new Uint8Array(1);
- const buf = [];
+ return readLineFromStdinSync() || defaultValue;
+}
+
+function readLineFromStdinSync() {
+ const c = new Uint8Array(1);
+ const buf = [];
- while (true) {
+ while (true) {
+ const n = stdin.readSync(c);
+ if (n === null || n === 0) {
+ break;
+ }
+ if (c[0] === CR) {
const n = stdin.readSync(c);
- if (n === null || n === 0) {
+ if (c[0] === LF) {
break;
}
- if (c[0] === CR) {
- const n = stdin.readSync(c);
- if (c[0] === LF) {
- break;
- }
- ArrayPrototypePush(buf, CR);
- if (n === null || n === 0) {
- break;
- }
- }
- if (c[0] === LF) {
+ ArrayPrototypePush(buf, CR);
+ if (n === null || n === 0) {
break;
}
- ArrayPrototypePush(buf, c[0]);
}
- return core.decode(new Uint8Array(buf));
+ if (c[0] === LF) {
+ break;
+ }
+ ArrayPrototypePush(buf, c[0]);
}
+ return core.decode(new Uint8Array(buf));
+}
- window.__bootstrap.prompt = {
- alert,
- confirm,
- prompt,
- };
-})(this);
+export { alert, confirm, prompt };