Skip to content

Commit

Permalink
feat: Non-interactive mode (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone authored Mar 28, 2022
1 parent 3bdebc5 commit fb53fda
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 275 deletions.
9 changes: 9 additions & 0 deletions .changeset/shy-dryers-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

feat: Non-interactive mode

Continuing the work from https://github.com/cloudflare/wrangler2/pull/325, this detects when wrangler is running inside an environment where "raw" mode is not available on stdin, and disables the features for hot keys and the shortcut bar. This also adds stubs for testing local mode functionality in `local-mode-tests`, and deletes the previous hacky `dev2.test.tsx`.

Fixes https://github.com/cloudflare/wrangler2/issues/322
83 changes: 11 additions & 72 deletions package-lock.json

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

26 changes: 26 additions & 0 deletions packages/local-mode-tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "local-mode-tests",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"test": "npx jest --forceExit"
},
"keywords": [],
"author": "",
"license": "ISC",
"jest": {
"restoreMocks": true,
"testTimeout": 30000,
"testRegex": ".*.(test|spec)\\.[jt]sx?$",
"transform": {
"^.+\\.c?(t|j)sx?$": [
"esbuild-jest",
{
"sourcemap": true
}
]
}
}
}
5 changes: 5 additions & 0 deletions packages/local-mode-tests/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
async fetch(_request: Request): Promise<Response> {
return new Response("Hello World!");
},
};
46 changes: 46 additions & 0 deletions packages/local-mode-tests/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { spawn } from "child_process";
import { fetch } from "undici";
import type { ChildProcess } from "child_process";
import type { Response } from "undici";

const waitUntilReady = async (url: string): Promise<Response> => {
let response: Response | undefined = undefined;

while (response === undefined) {
await new Promise((resolvePromise) => setTimeout(resolvePromise, 100));

try {
response = await fetch(url);
} catch {}
}

return response as Response;
};
const isWindows = process.platform === "win32";

let wranglerProcess: ChildProcess;

beforeAll(async () => {
wranglerProcess = spawn("npx", ["wrangler", "dev", "--local"], {
shell: isWindows,
});
});

afterAll(async () => {
await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill();
});
});

it("renders", async () => {
const response = await waitUntilReady("http://localhost:8787/");
const text = await response.text();
expect(text).toContain("Hello World!");
});
14 changes: 14 additions & 0 deletions packages/local-mode-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"isolatedModules": true,
"esModuleInterop": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"target": "esnext",
"strict": true,
"noEmit": true,
"skipLibCheck": true
}
}
3 changes: 3 additions & 0 deletions packages/local-mode-tests/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "local-mode-tests"
main = "src/index.ts"
compatibility_date = "2022-03-27"
2 changes: 0 additions & 2 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"@types/mime": "^2.0.3",
"@types/prompts": "^2.0.14",
"@types/react": "^17.0.37",
"@types/react-test-renderer": "^17.0.1",
"@types/serve-static": "^1.13.10",
"@types/signal-exit": "^3.0.1",
"@types/supports-color": "^8.1.1",
Expand Down Expand Up @@ -86,7 +85,6 @@
"prompts": "^2.4.2",
"react": "^17.0.2",
"react-error-boundary": "^3.1.4",
"react-test-renderer": "^17.0.2",
"serve-static": "^1.14.1",
"signal-exit": "^3.0.6",
"supports-color": "^9.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe("wrangler dev", () => {
);

// and the command would pass through
expect((Dev as jest.Mock).mock.calls[0][0].buildCommand).toEqual({
expect((Dev as jest.Mock).mock.calls[0][0].build).toEqual({
command:
"node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
cwd: undefined,
Expand Down
Loading

0 comments on commit fb53fda

Please sign in to comment.