Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
fix: lazy shims (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi authored Apr 19, 2023
1 parent 0f16b8c commit 20b6155
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
24 changes: 19 additions & 5 deletions _tasks/dnt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ await Promise.all([
name: "ws",
version: "8.13.0",
},
"./deps/shims/shim-deno.ts": "@deno/shim-deno",
"./deps/shims/upgradeWebSocket.ts": "./deps/shims/upgradeWebSocket.node.ts",
"./deps/shims/register.ts": "./deps/shims/register.node.ts",
"./deps/std/http.ts": "./deps/std/http.node.ts",
Expand Down Expand Up @@ -136,8 +137,21 @@ await Promise.all([
fs.copy("server/static/", path.join(outDir, "esm/server/static/")),
])

await Deno.writeTextFile(
"target/npm/esm/main.js",
(await Deno.readTextFile("target/npm/esm/main.js"))
.replace(/^#!.+/, "#!/usr/bin/env -S node --loader ts-node/esm"),
)
await Promise.all([
editFile(
"target/npm/esm/main.js",
(content) =>
content
.replace(/^#!.+/, "#!/usr/bin/env -S node --loader ts-node/esm"),
),
editFile(
"target/npm/esm/_dnt.shims.js",
(content) =>
content
.replace(/"@deno\/shim-deno"/g, `"./deps/shims/Deno.node.js"`),
),
])

async function editFile(path: string, modify: (content: string) => string) {
await Deno.writeTextFile(path, modify(await Deno.readTextFile(path)))
}
34 changes: 34 additions & 0 deletions deps/shims/Deno.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Command } from "./command.node.ts"

declare const process: { env: Record<string, string> }
let _deno = {
errors: {
NotFound: class NotFound extends Error {},
AlreadyExists: class AlreadyExists extends Error {},
},
env: {
// @ts-ignore .
get: (key: string) => process.env[key],
set: (key: string, value: string) => {
process.env[key] = value
},
has: (key: string) => key in process.env,
delete: (key: string) => {
delete process.env[key]
},
toObject: () => ({ ...process.env }),
},
} as any as typeof globalThis.Deno

export const Deno = new Proxy(
{},
new Proxy(Reflect, {
// @ts-ignore .
get: (_, key) => (_, ...x) => Reflect[key](_deno, ...x),
}),
)

export function register(newDeno: typeof globalThis.Deno) {
_deno = newDeno
_deno.Command = Command
}
6 changes: 1 addition & 5 deletions deps/shims/command.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readableStreamFromReader, writableStreamFromWriter } from "../std/streams.ts"

class Command implements Deno.Command {
export class Command implements Deno.Command {
#command
#options
constructor(command: string, options?: Deno.CommandOptions) {
Expand Down Expand Up @@ -128,7 +128,3 @@ class ChildProcess implements Deno.ChildProcess {
}
}
}

if (!("Command" in Deno)) {
Deno.Command = Command
}
4 changes: 3 additions & 1 deletion deps/shims/register.node.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import "./command.node.ts"
import { register } from "./Deno.node.ts"
import { Deno } from "./shim-deno.ts"
register(Deno)
3 changes: 3 additions & 0 deletions deps/shims/shim-deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This will be shimmed to the `@deno/shim-deno` npm package by DNT

export declare const Deno: typeof globalThis.Deno

0 comments on commit 20b6155

Please sign in to comment.