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

refactor: remove deno.run #1036

Merged
merged 18 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/dev/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,23 @@ const manifest = {
export default manifest;
`;

const proc = Deno.run({
cmd: [Deno.execPath(), "fmt", "-"],
const proc = new Deno.Command(Deno.execPath(), {
args: ["fmt", "-"],
stdin: "piped",
stdout: "piped",
stderr: "null",
});
}).spawn();

const raw = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(output));
controller.close();
},
});
await raw.pipeTo(proc.stdin.writable);
const out = await proc.output();
await proc.status();
proc.close();
await raw.pipeTo(proc.stdin);
const { stdout } = await proc.output();

const manifestStr = new TextDecoder().decode(out);
const manifestStr = new TextDecoder().decode(stdout);
const manifestPath = join(directory, "./fresh.gen.ts");

await Deno.writeTextFile(manifestPath, manifestStr);
Expand Down
2 changes: 1 addition & 1 deletion src/server/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface JSXConfig {
let esbuildInitialized: boolean | Promise<void> = false;
async function ensureEsbuildInitialized() {
if (esbuildInitialized === false) {
if (Deno.run === undefined) {
if (Deno.Command === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may you consider to check it by typeof??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that in Deno, and other recent js runtime, you cannot anymore re-defined undefined and so using typeof is not required. tested in repl

$ deno
Deno 1.30.3+e0074b5
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> globalThis.undefined = 1
Uncaught TypeError: Cannot assign to read only property 'undefined' of object '#<Window>'
    at <anonymous>:2:22

Is there any other reasons to use typeof ?

lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
const wasmURL = new URL("./esbuild_v0.14.51.wasm", import.meta.url).href;
esbuildInitialized = fetch(wasmURL).then(async (r) => {
const resp = new Response(r.body, {
Expand Down
2 changes: 1 addition & 1 deletion src/server/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export * as rutt from "https://deno.land/x/rutt@0.0.14/mod.ts";
import * as esbuildWasm from "https://deno.land/x/esbuild@v0.14.51/wasm.js";
import * as esbuildNative from "https://deno.land/x/esbuild@v0.14.51/mod.js";
// @ts-ignore trust me
const esbuild: typeof esbuildWasm = Deno.run === undefined
const esbuild: typeof esbuildWasm = Deno.Command === undefined
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
? esbuildWasm
: esbuildNative;
export { esbuild, esbuildWasm as esbuildTypes };
Expand Down
75 changes: 27 additions & 48 deletions tests/cli_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ Deno.test({
const tmpDirName = await Deno.makeTempDir();

await t.step("execute init command", async () => {
const cliProcess = Deno.run({
cmd: [
"deno",
const cliProcess = new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
"init.ts",
Expand All @@ -53,8 +52,7 @@ Deno.test({
stdin: "null",
stdout: "null",
});
const { code } = await cliProcess.status();
cliProcess.close();
const { code } = await cliProcess.output();
assertEquals(code, 0);
});

Expand Down Expand Up @@ -113,15 +111,15 @@ Deno.test({
});

await t.step("start up the server and access the root page", async () => {
const serverProcess = Deno.run({
cmd: ["deno", "run", "-A", "--check", "main.ts"],
const serverProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "--check", "main.ts"],
stdin: "null",
stdout: "piped",
stderr: "inherit",
cwd: tmpDirName,
});
}).spawn();

const lines = serverProcess.stdout.readable
const lines = serverProcess.stdout
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());

Expand Down Expand Up @@ -166,8 +164,6 @@ Deno.test({

await lines.cancel();
serverProcess.kill("SIGTERM");
await serverProcess.status();
serverProcess.close();
});

await retry(() => Deno.remove(tmpDirName, { recursive: true }));
Expand All @@ -183,9 +179,8 @@ Deno.test({
const tmpDirName = await Deno.makeTempDir();

await t.step("execute init command", async () => {
const cliProcess = Deno.run({
cmd: [
"deno",
const cliProcess = new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
"init.ts",
Expand All @@ -196,8 +191,7 @@ Deno.test({
stdin: "null",
stdout: "null",
});
const { code } = await cliProcess.status();
cliProcess.close();
const { code } = await cliProcess.output();
assertEquals(code, 0);
});

Expand Down Expand Up @@ -265,15 +259,15 @@ Deno.test({
});

await t.step("start up the server and access the root page", async () => {
const serverProcess = Deno.run({
cmd: ["deno", "run", "-A", "--check", "main.ts"],
const serverProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "--check", "main.ts"],
stdin: "null",
stdout: "piped",
stderr: "inherit",
cwd: tmpDirName,
});
}).spawn();

const lines = serverProcess.stdout.readable
const lines = serverProcess.stdout
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());

Expand Down Expand Up @@ -322,8 +316,6 @@ Deno.test({

await lines.cancel();
serverProcess.kill("SIGTERM");
await serverProcess.status();
serverProcess.close();
});

await retry(() => Deno.remove(tmpDirName, { recursive: true }));
Expand All @@ -332,62 +324,49 @@ Deno.test({
sanitizeResources: false,
});

Deno.test("fresh-init error(help)", async function (t) {
Deno.test("fresh-init error(help)", { sanitizeOps: false }, async function (t) {
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
const includeText = "fresh-init";

await t.step(
"execute invalid init command (deno run -A init.ts)",
async () => {
const cliProcess = Deno.run({
cmd: ["deno", "run", "-A", "init.ts"],
const cliProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "init.ts"],
stdin: "null",
stderr: "piped",
});
const { code } = await cliProcess.status();
cliProcess.close();
const { code, stderr } = await cliProcess.output();
assertEquals(code, 1);

const rawError = await cliProcess.stderrOutput();
const errorString = new TextDecoder().decode(rawError);

const errorString = new TextDecoder().decode(stderr);
assertStringIncludes(errorString, includeText);
},
);

await t.step(
"execute invalid init command (deno run -A init.ts -f)",
async () => {
const cliProcess = Deno.run({
cmd: ["deno", "run", "-A", "init.ts", "-f"],
stdin: "null",
stderr: "piped",
const cliProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "init.ts", "-f"],
});
const { code } = await cliProcess.status();
cliProcess.close();
const { code, stderr } = await cliProcess.output();
assertEquals(code, 1);

const rawError = await cliProcess.stderrOutput();
const errorString = new TextDecoder().decode(rawError);

const errorString = new TextDecoder().decode(stderr);
assertStringIncludes(errorString, includeText);
},
);

await t.step(
"execute invalid init command (deno run -A init.ts --foo)",
async () => {
const cliProcess = Deno.run({
cmd: ["deno", "run", "-A", "init.ts", "--foo"],
stdin: "null",
stderr: "piped",
const cliProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "init.ts", "--foo"],
});
const { code } = await cliProcess.status();
cliProcess.close();
const { code, stderr } = await cliProcess.output();
assertEquals(code, 1);

const rawError = await cliProcess.stderrOutput();
const errorString = new TextDecoder().decode(rawError);

const errorString = new TextDecoder().decode(stderr);
assertStringIncludes(errorString, includeText);
},
);
Expand Down
18 changes: 8 additions & 10 deletions tests/islands_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Deno.test({
name: "island tests",
async fn(t) {
// Preparation
const serverProcess = Deno.run({
cmd: ["deno", "run", "-A", "./tests/fixture/main.ts"],
const serverProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "./tests/fixture/main.ts"],
stdout: "piped",
stderr: "inherit",
});
}).spawn();

const decoder = new TextDecoderStream();
const lines = serverProcess.stdout.readable
const lines = serverProcess.stdout
.pipeThrough(decoder)
.pipeThrough(new TextLineStream());

Expand Down Expand Up @@ -77,7 +77,6 @@ Deno.test({

await lines.cancel();
serverProcess.kill("SIGTERM");
serverProcess.close();
},
sanitizeOps: false,
sanitizeResources: false,
Expand All @@ -87,14 +86,14 @@ Deno.test({
name: "island tests with </script>",
async fn(t) {
// Preparation
const serverProcess = Deno.run({
cmd: ["deno", "run", "-A", "./tests/fixture/main.ts"],
const serverProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "./tests/fixture/main.ts"],
stdout: "piped",
stderr: "inherit",
});
}).spawn();

const decoder = new TextDecoderStream();
const lines = serverProcess.stdout.readable
const lines = serverProcess.stdout
.pipeThrough(decoder)
.pipeThrough(new TextLineStream());

Expand Down Expand Up @@ -136,7 +135,6 @@ Deno.test({

await lines.cancel();
serverProcess.kill("SIGTERM");
serverProcess.close();
},
sanitizeOps: false,
sanitizeResources: false,
Expand Down
19 changes: 8 additions & 11 deletions tests/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,8 @@ Deno.test("experimental Deno.serve", {
ignore: Deno.build.os === "windows", // TODO: Deno.serve hang on Windows?
}, async (t) => {
// Preparation
const serverProcess = Deno.run({
cmd: [
"deno",
const serverProcess = new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
"--unstable",
Expand All @@ -488,10 +487,10 @@ Deno.test("experimental Deno.serve", {
],
stdout: "piped",
stderr: "inherit",
});
}).spawn();

const decoder = new TextDecoderStream();
const lines = serverProcess.stdout.readable
const lines = serverProcess.stdout
.pipeThrough(decoder)
.pipeThrough(new TextLineStream());

Expand Down Expand Up @@ -539,22 +538,21 @@ Deno.test("experimental Deno.serve", {

await lines.cancel();
serverProcess.kill("SIGTERM");
serverProcess.close();
});

Deno.test("jsx pragma works", {
sanitizeOps: false,
sanitizeResources: false,
}, async (t) => {
// Preparation
const serverProcess = Deno.run({
cmd: ["deno", "run", "-A", "./tests/fixture_jsx_pragma/main.ts"],
const serverProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "./tests/fixture_jsx_pragma/main.ts"],
stdout: "piped",
stderr: "inherit",
});
}).spawn();

const decoder = new TextDecoderStream();
const lines = serverProcess.stdout.readable
const lines = serverProcess.stdout
.pipeThrough(decoder)
.pipeThrough(new TextLineStream());

Expand Down Expand Up @@ -594,5 +592,4 @@ Deno.test("jsx pragma works", {

await lines.cancel();
serverProcess.kill("SIGTERM");
serverProcess.close();
});
9 changes: 4 additions & 5 deletions tests/plugin_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ Deno.test({
name: "/with-island hydration",
async fn(t) {
// Preparation
const serverProcess = Deno.run({
cmd: ["deno", "run", "-A", "./tests/fixture_plugin/main.ts"],
const serverProcess = new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "./tests/fixture_plugin/main.ts"],
stdout: "piped",
stderr: "inherit",
});
}).spawn();

const decoder = new TextDecoderStream();
const lines = serverProcess.stdout.readable
const lines = serverProcess.stdout
.pipeThrough(decoder)
.pipeThrough(new TextLineStream());

Expand Down Expand Up @@ -98,7 +98,6 @@ Deno.test({

await lines.cancel();
serverProcess.kill("SIGTERM");
serverProcess.close();
},
sanitizeOps: false,
sanitizeResources: false,
Expand Down
2 changes: 1 addition & 1 deletion www/deno.lock

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

Loading