From ac168f4f62851ad3fe2e2705655baf8229c421ea Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sat, 29 Jan 2022 20:07:01 +0000 Subject: [PATCH] refactor: use helpers to manage npm commands This also speeds up tests and avoids us checking that npm did what it is supposed to do. --- .changeset/pretty-flowers-bathe.md | 7 +++++ packages/wrangler/src/__tests__/index.test.ts | 27 +++++++---------- packages/wrangler/src/index.tsx | 30 ++++--------------- packages/wrangler/src/npm-installer.ts | 24 +++++++++++++++ 4 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 .changeset/pretty-flowers-bathe.md create mode 100644 packages/wrangler/src/npm-installer.ts diff --git a/.changeset/pretty-flowers-bathe.md b/.changeset/pretty-flowers-bathe.md new file mode 100644 index 000000000000..78a32b5d7310 --- /dev/null +++ b/.changeset/pretty-flowers-bathe.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +refactor: use helpers to manage npm commands + +This change speeds up tests and avoids us checking that npm did what it is supposed to do. diff --git a/packages/wrangler/src/__tests__/index.test.ts b/packages/wrangler/src/__tests__/index.test.ts index 8d75933a3461..099f6f817872 100644 --- a/packages/wrangler/src/__tests__/index.test.ts +++ b/packages/wrangler/src/__tests__/index.test.ts @@ -1,6 +1,8 @@ import * as fs from "node:fs"; import * as fsp from "node:fs/promises"; import * as TOML from "@iarna/toml"; +import { version as wranglerVersion } from "../../package.json"; +import { npm } from "../npm-installer"; import { mockConsoleMethods } from "./helpers/mock-console"; import { mockConfirm } from "./helpers/mock-dialogs"; import { runInTempDir } from "./helpers/run-in-tmp"; @@ -9,6 +11,11 @@ import { runWrangler } from "./helpers/run-wrangler"; describe("wrangler", () => { runInTempDir(); + beforeEach(() => { + jest.spyOn(npm, "addDevDep").mockResolvedValue(); + jest.spyOn(npm, "install").mockResolvedValue(); + }); + const std = mockConsoleMethods(); describe("no command", () => { @@ -153,6 +160,7 @@ describe("wrangler", () => { wrangler: expect.any(String), }); expect(fs.existsSync("./tsconfig.json")).toBe(false); + expect(npm.install).toHaveBeenCalled(); }); it("should not touch an existing package.json in the same directory", async () => { @@ -205,9 +213,7 @@ describe("wrangler", () => { ); expect(packageJson.name).toEqual("test"); expect(packageJson.version).toEqual("1.0.0"); - expect(packageJson.devDependencies).toEqual({ - wrangler: expect.any(String), - }); + expect(npm.addDevDep).toHaveBeenCalledWith("wrangler", wranglerVersion); }); it("should not touch an existing package.json in an ancestor directory", async () => { @@ -265,13 +271,7 @@ describe("wrangler", () => { expect(tsconfigJson.compilerOptions.types).toEqual([ "@cloudflare/workers-types", ]); - const packageJson = JSON.parse( - fs.readFileSync("./package.json", "utf-8") - ); - expect(packageJson.devDependencies).toEqual({ - "@cloudflare/workers-types": expect.any(String), - wrangler: expect.any(String), - }); + expect(npm.addDevDep).toHaveBeenCalledWith("@cloudflare/workers-types"); }); it("should not touch an existing tsconfig.json in the same directory", async () => { @@ -331,12 +331,7 @@ describe("wrangler", () => { ); // unchanged tsconfig expect(tsconfigJson.compilerOptions).toEqual({}); - const packageJson = JSON.parse( - fs.readFileSync("./package.json", "utf-8") - ); - expect(packageJson.devDependencies).toEqual({ - "@cloudflare/workers-types": expect.any(String), - }); + expect(npm.addDevDep).toHaveBeenCalledWith("@cloudflare/workers-types"); }); it("should not touch an existing tsconfig.json in an ancestor directory", async () => { diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 2666fb7579d7..0451aac2107b 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -3,7 +3,6 @@ import { readFile, writeFile } from "node:fs/promises"; import path from "node:path/posix"; import { setTimeout } from "node:timers/promises"; import TOML from "@iarna/toml"; -import { execa } from "execa"; import { findUp } from "find-up"; import { render } from "ink"; import React from "react"; @@ -25,6 +24,7 @@ import { createNamespace, isValidNamespaceBinding, } from "./kv"; +import { npm } from "./npm-installer"; import { pages } from "./pages"; import publish from "./publish"; import { getAssetPaths } from "./sites"; @@ -38,8 +38,8 @@ import { getAccountId, validateScopeKeys, } from "./user"; - import { whoami } from "./whoami"; + import type { Config } from "./config"; import type Yargs from "yargs"; @@ -234,9 +234,7 @@ export async function main(argv: string[]): Promise { " " ) + "\n" ); - await execa("npm", ["install"], { - stdio: "inherit", - }); + await npm.install(); console.log(`✨ Created package.json`); pathToPackageJson = path.join(process.cwd(), "package.json"); } else { @@ -258,13 +256,7 @@ export async function main(argv: string[]): Promise { "Would you like to install wrangler into your package.json?" ); if (shouldInstall) { - await execa( - "npm", - ["install", `wrangler@${wranglerVersion}`, "--save-dev"], - { - stdio: "inherit", - } - ); + await npm.addDevDep("wrangler", wranglerVersion); console.log(`✨ Installed wrangler`); } } @@ -298,11 +290,7 @@ export async function main(argv: string[]): Promise { " " ) + "\n" ); - await execa( - "npm", - ["install", "@cloudflare/workers-types", "--save-dev"], - { stdio: "inherit" } - ); + await npm.addDevDep("@cloudflare/workers-types"); console.log( `✨ Created tsconfig.json, installed @cloudflare/workers-types into devDependencies` ); @@ -324,13 +312,7 @@ export async function main(argv: string[]): Promise { "Would you like to install the type definitions for Workers into your package.json?" ); if (shouldInstall) { - await execa( - "npm", - ["install", "@cloudflare/workers-types", "--save-dev"], - { - stdio: "inherit", - } - ); + await npm.addDevDep("@cloudflare/workers-types"); // We don't update the tsconfig.json because // it could be complicated in existing projects // and we don't want to break them. Instead, we simply diff --git a/packages/wrangler/src/npm-installer.ts b/packages/wrangler/src/npm-installer.ts new file mode 100644 index 000000000000..8bcc0823a4d1 --- /dev/null +++ b/packages/wrangler/src/npm-installer.ts @@ -0,0 +1,24 @@ +import { execa } from "execa"; + +/** + * Helpers for running npm commands + */ +export const npm = { + /** Add and install a new devDependency into the local package.json. */ + async addDevDep(packageName: string, versionRange = "latest"): Promise { + await execa( + "npm", + ["install", `${packageName}@${versionRange}`, "--save-dev"], + { + stdio: "inherit", + } + ); + }, + + /** Install all the dependencies in the local package.json. */ + async install(): Promise { + await execa("npm", ["install"], { + stdio: "inherit", + }); + }, +};