Skip to content

Commit

Permalink
feat: add @deno/shim-prompts package (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Dec 22, 2021
1 parent 2e8c071 commit f4560b3
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 91 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ Deno shims for Node.js
## Packages

- [deno.ns](packages/deno.ns) - Deno namespace shim.
- [@deno/shim-prompts](packages/shim-prompts) - Shims for `alert`, `confirm` and
`prompt`.
- [@deno/shim-timers](packages/shim-timers) - Shims for `setTimeout` and
`setInterval`.
42 changes: 41 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "deno.ns-workspace",
"workspaces": [
"packages/deno.ns",
"packages/shim-prompts",
"packages/shim-timers"
],
"scripts": {
Expand Down
3 changes: 0 additions & 3 deletions packages/deno.ns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ See

```js
const {
alert,
Blob,
confirm,
crypto,
Deno,
fetch,
File,
FormData,
Headers,
prompt,
Request,
Response,
} = require("deno.ns");
Expand Down
36 changes: 11 additions & 25 deletions packages/deno.ns/lib/deno.ns.lib.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/deno.ns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"clean": "git clean -fXde !node_modules/",
"test": "node --loader=ts-node/esm tools/run_tests.mjs",
"denolib": "deno run --allow-run --allow-write=src/deno tools/denolib.ts",
"generate-declaration-file": "deno run --allow-write=lib --allow-read=. ctools/generateDeclarationFile.ts",
"generate-declaration-file": "deno run --allow-write=lib --allow-read=. tools/generateDeclarationFile.ts",
"update-progress": "deno run --allow-read tools/missing.ts > PROGRESS.md"
},
"engines": {
Expand Down
1 change: 0 additions & 1 deletion packages/deno.ns/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { webcrypto as crypto } from "crypto";
export * as Deno from "./deno.js";
export * from "./util/mod.js";

// TODO: Wait for node builtin WhatWG Streams or fetch-blob to export them without assigning to globalThis
import * as undici from "undici";
Expand Down
14 changes: 0 additions & 14 deletions packages/deno.ns/src/util/mod.test.ts

This file was deleted.

35 changes: 0 additions & 35 deletions packages/deno.ns/src/util/mod.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/deno.ns/tools/generateDeclarationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function getDeclarationProject() {
tsConfigFilePath: `./tsconfig.json`,
});

// exitIfDiagnostics(project, project.getPreEmitDiagnostics());
exitIfDiagnostics(project, project.getPreEmitDiagnostics());

const dtsEmitResult = project.emitToMemory({
emitOnlyDtsFiles: true,
Expand Down
20 changes: 20 additions & 0 deletions packages/shim-prompts/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright 2021 the Deno authors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions packages/shim-prompts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @deno/shim-prompts

Shims for `alert`, `confirm`, and `prompt` that work in Node.js.
35 changes: 35 additions & 0 deletions packages/shim-prompts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@deno/shim-prompts",
"version": "0.1.0",
"description": "alert, confirm, and prompt for Node.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"prepare": "echo \"Prepared.\"",
"build": "tsc",
"test": "echo \"No tests yet\""
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/denoland/deno.ns.git"
},
"keywords": [
"shim",
"deno",
"node.js",
"prompts"
],
"author": "The Deno authors",
"license": "MIT",
"bugs": {
"url": "https://github.com/denoland/deno.ns/issues"
},
"homepage": "https://github.com/denoland/deno.ns#readme",
"devDependencies": {
"@types/node": "^16.4.13",
"typescript": "^4.5.2"
}
}
42 changes: 42 additions & 0 deletions packages/shim-prompts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { writeSync } from "fs";
import { readlineSync } from "./readlineSync.js";

export const alert: (typeof globalThis) extends { [P in "alert"]: infer T } ? T
: (message?: any) => void = (globalThis as any)["alert"] ??
function alert(message) {
writeSync(
process.stdout.fd,
new TextEncoder().encode(`${message} [Enter] `),
);
readlineSync();
};

export const confirm: (typeof globalThis) extends { [P in "confirm"]: infer T }
? T
: (message?: string) => boolean = (globalThis as any)["confirm"] ??
function confirm(message) {
writeSync(
process.stdout.fd,
new TextEncoder().encode(`${message} [y/N] `),
);
const result = readlineSync();
return ["y", "Y"].includes(result);
};

export const prompt: (typeof globalThis) extends { [P in "prompt"]: infer T }
? T
: (message?: string, _default?: string) => string | null =
(globalThis as any)["prompt"] ??
function prompt(
message,
defaultValue = undefined,
) {
writeSync(
process.stdout.fd,
new TextEncoder().encode(
`${message} ${defaultValue == null ? "" : `[${defaultValue}]`} `,
),
);
const result = readlineSync();
return result.length > 0 ? result : defaultValue ?? null;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readSync } from "fs";
export const readlineSync = () => {

export function readlineSync() {
let line = "";
let char = "";
const buf = Buffer.alloc(1);
Expand All @@ -10,8 +11,7 @@ export const readlineSync = () => {
if (bytesRead === 0) {
return line;
}
} catch (err) {
// @ts-expect-error Property 'code' does not exist on type 'Error'.
} catch (err: any) {
if (err.code === "EOF") {
return line;
}
Expand All @@ -23,4 +23,4 @@ export const readlineSync = () => {
readSync(process.stdin.fd, buf, 0, 1, 0);
}
return line;
};
}
18 changes: 18 additions & 0 deletions packages/shim-prompts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"module": "CommonJS",
"strict": true,
"target": "ES2019",
"declaration": true,
"outDir": "dist",
"stripInternal": true
},
"ts-node": {
"transpileOnly": true,
"files": true,
"compilerOptions": {
"module": "ES2015"
}
},
"files": ["src/index.ts"]
}
5 changes: 3 additions & 2 deletions packages/shim-timers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"prepare": "echo \"Prepared.\"",
"build": "tsc",
"test": "echo \"No tests yet\""
"test": "ts-node --project tsconfig.json src/index.test.ts"
},
"files": [
"dist"
Expand All @@ -29,6 +29,7 @@
},
"homepage": "https://github.com/denoland/deno.ns#readme",
"devDependencies": {
"typescript": "^4.5.2"
"typescript": "^4.5.2",
"ts-node": "^10.4.0"
}
}
Loading

0 comments on commit f4560b3

Please sign in to comment.