2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
import { isatty } from "internal:runtime/40_tty.js";
|
2023-03-04 22:37:37 -05:00
|
|
|
import { stdin } from "internal:deno_io/12_io.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } =
|
|
|
|
primordials;
|
|
|
|
const LF = StringPrototypeCharCodeAt("\n", 0);
|
|
|
|
const CR = StringPrototypeCharCodeAt("\r", 0);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function alert(message = "Alert") {
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return;
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.print(`${message} [Enter] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
readLineFromStdinSync();
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function confirm(message = "Confirm") {
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return false;
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.print(`${message} [y/N] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const answer = readLineFromStdinSync();
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return answer === "Y" || answer === "y";
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function prompt(message = "Prompt", defaultValue) {
|
|
|
|
defaultValue ??= null;
|
|
|
|
|
|
|
|
if (!isatty(stdin.rid)) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
core.print(`${message} `, false);
|
|
|
|
|
|
|
|
if (defaultValue) {
|
|
|
|
core.print(`[${defaultValue}] `, false);
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return readLineFromStdinSync() || defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readLineFromStdinSync() {
|
|
|
|
const c = new Uint8Array(1);
|
|
|
|
const buf = [];
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
while (true) {
|
|
|
|
const n = stdin.readSync(c);
|
|
|
|
if (n === null || n === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c[0] === CR) {
|
2020-10-13 09:31:59 -04:00
|
|
|
const n = stdin.readSync(c);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (c[0] === LF) {
|
2020-10-29 13:35:58 -04:00
|
|
|
break;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(buf, CR);
|
|
|
|
if (n === null || n === 0) {
|
2020-10-13 09:31:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (c[0] === LF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ArrayPrototypePush(buf, c[0]);
|
2020-10-13 09:31:59 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return core.decode(new Uint8Array(buf));
|
|
|
|
}
|
2020-10-13 09:31:59 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { alert, confirm, prompt };
|