From 766609674340c86d031929fa030c6da3a6c5d4ae Mon Sep 17 00:00:00 2001 From: Alan Schio <6757777+schirrel@users.noreply.github.com> Date: Sat, 25 Jan 2025 01:28:57 -0400 Subject: [PATCH 1/3] fix(module): check module name matches pkgName if it has pkgVersion fix #697 #701 --- packages/nuxi/src/commands/module/add.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/nuxi/src/commands/module/add.ts b/packages/nuxi/src/commands/module/add.ts index 7766790c8..2c9b03d5c 100644 --- a/packages/nuxi/src/commands/module/add.ts +++ b/packages/nuxi/src/commands/module/add.ts @@ -223,6 +223,7 @@ async function resolveModule(moduleName: string, cwd: string): Promise module.name === moduleName + || (pkgVersion && module.name === pkgName) || module.npm === pkgName || module.aliases?.includes(pkgName), ) From 37863a3250b157dfa6124d8290c8d344acd8c9eb Mon Sep 17 00:00:00 2001 From: Alan Schio Date: Sat, 25 Jan 2025 02:51:48 -0400 Subject: [PATCH 2/3] test: add test for module add with version --- .../test/unit/commands/module/add.spec.ts | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 packages/nuxi/test/unit/commands/module/add.spec.ts diff --git a/packages/nuxi/test/unit/commands/module/add.spec.ts b/packages/nuxi/test/unit/commands/module/add.spec.ts new file mode 100644 index 000000000..aa0c3f0a9 --- /dev/null +++ b/packages/nuxi/test/unit/commands/module/add.spec.ts @@ -0,0 +1,81 @@ +import { describe, expect, it, vi } from "vitest"; +import * as utils from "../../../../src/commands/module/_utils"; +// import * as c12 from "c12/update"; +import commands from "../../../../src/commands/module"; +import * as runCommands from "../../../../src/run"; + +const updateConfig = vi.fn(() => Promise.resolve()); +const addDependency = vi.fn(() => Promise.resolve()); + +const applyMocks = () => { + vi.mock("c12/update", async (importOriginal) => { + return { + updateConfig, + }; + }); + vi.mock("nypm", async (importOriginal) => { + return { + addDependency, + }; + }); + vi.mock("pkg-types", async (importOriginal) => { + return { + readPackageJSON: () => { + return new Promise((resolve) => { + resolve({ + devDependencies: { + nuxt: "3.0.0", + }, + }); + }); + }, + }; + }); +}; +describe("module add", () => { + applyMocks(); + vi.spyOn(runCommands, "runCommand").mockImplementation(vi.fn()); + vi.spyOn(utils, "getNuxtVersion").mockResolvedValue("3.0.0"); + vi.spyOn(utils, "fetchModules").mockResolvedValue([ + { + name: "content", + npm: "@nuxt/content", + compatibility: { + nuxt: "3.0.0", + }, + }, + ]); + + + it("should install Nuxt module", async () => { + const addCommand = await commands.subCommands.add(); + await addCommand.setup({ + args: { + cwd: "/fake-dir", + _: ["content"], + }, + }); + + expect(addDependency).toHaveBeenCalledWith(["@nuxt/content@3.0.0"], { + cwd: "/fake-dir", + dev: true, + installPeerDependencies: true, + }); + }); + + it("should convert versioned module to Nuxt module", async () => { + const addCommand = await commands.subCommands.add(); + await addCommand.setup({ + args: { + cwd: "/fake-dir", + _: ["content@2.9.0"], + }, + }); + + expect(addDependency).toHaveBeenCalledWith(["@nuxt/content@2.9.0"], { + cwd: "/fake-dir", + dev: true, + installPeerDependencies: true, + }); + }); +}); From f81c5aa4b19468140b4034984eaa9596969a0655 Mon Sep 17 00:00:00 2001 From: Alan Schio Date: Sat, 25 Jan 2025 03:03:39 -0400 Subject: [PATCH 3/3] lint: fixing types and lint --- .../test/unit/commands/module/add.spec.ts | 122 ++++++++++-------- 1 file changed, 71 insertions(+), 51 deletions(-) diff --git a/packages/nuxi/test/unit/commands/module/add.spec.ts b/packages/nuxi/test/unit/commands/module/add.spec.ts index aa0c3f0a9..ccc4d9189 100644 --- a/packages/nuxi/test/unit/commands/module/add.spec.ts +++ b/packages/nuxi/test/unit/commands/module/add.spec.ts @@ -1,81 +1,101 @@ -import { describe, expect, it, vi } from "vitest"; -import * as utils from "../../../../src/commands/module/_utils"; -// import * as c12 from "c12/update"; -import commands from "../../../../src/commands/module"; -import * as runCommands from "../../../../src/run"; +import { describe, expect, it, vi } from 'vitest' +import commands from '../../../../src/commands/module' +import * as utils from '../../../../src/commands/module/_utils' +import * as runCommands from '../../../../src/run' -const updateConfig = vi.fn(() => Promise.resolve()); -const addDependency = vi.fn(() => Promise.resolve()); - -const applyMocks = () => { - vi.mock("c12/update", async (importOriginal) => { +const updateConfig = vi.fn(() => Promise.resolve()) +const addDependency = vi.fn(() => Promise.resolve()) +interface CommandsType { + subCommands: { + // biome-ignore lint/correctness/noEmptyPattern: + add: () => Promise<{ setup: (args: any) => void }> + } +} +function applyMocks() { + vi.mock('c12/update', async () => { return { updateConfig, - }; - }); - vi.mock("nypm", async (importOriginal) => { + } + }) + vi.mock('nypm', async () => { return { addDependency, - }; - }); - vi.mock("pkg-types", async (importOriginal) => { + } + }) + vi.mock('pkg-types', async () => { return { readPackageJSON: () => { return new Promise((resolve) => { resolve({ devDependencies: { - nuxt: "3.0.0", + nuxt: '3.0.0', }, - }); - }); + }) + }) }, - }; - }); -}; -describe("module add", () => { - applyMocks(); - vi.spyOn(runCommands, "runCommand").mockImplementation(vi.fn()); - vi.spyOn(utils, "getNuxtVersion").mockResolvedValue("3.0.0"); - vi.spyOn(utils, "fetchModules").mockResolvedValue([ + } + }) +} +describe('module add', () => { + applyMocks() + vi.spyOn(runCommands, 'runCommand').mockImplementation(vi.fn()) + vi.spyOn(utils, 'getNuxtVersion').mockResolvedValue('3.0.0') + vi.spyOn(utils, 'fetchModules').mockResolvedValue([ { - name: "content", - npm: "@nuxt/content", + name: 'content', + npm: '@nuxt/content', compatibility: { - nuxt: "3.0.0", + nuxt: '3.0.0', + requires: {}, + versionMap: {}, + }, + description: '', + repo: '', + github: '', + website: '', + learn_more: '', + category: '', + type: 'community', + maintainers: [], + stats: { + downloads: 0, + stars: 0, + maintainers: 0, + contributors: 0, + modules: 0, }, }, - ]); - + ]) - it("should install Nuxt module", async () => { - const addCommand = await commands.subCommands.add(); + it('should install Nuxt module', async () => { + const addCommand = await (commands as CommandsType).subCommands.add() await addCommand.setup({ args: { - cwd: "/fake-dir", - _: ["content"], + cwd: '/fake-dir', + _: ['content'], }, - }); + }) - expect(addDependency).toHaveBeenCalledWith(["@nuxt/content@3.0.0"], { - cwd: "/fake-dir", + expect(addDependency).toHaveBeenCalledWith(['@nuxt/content@3.0.0'], { + cwd: '/fake-dir', dev: true, installPeerDependencies: true, - }); - }); + }) + }) - it("should convert versioned module to Nuxt module", async () => { - const addCommand = await commands.subCommands.add(); + it('should convert versioned module to Nuxt module', async () => { + const addCommand = await (commands as CommandsType).subCommands.add() await addCommand.setup({ args: { - cwd: "/fake-dir", - _: ["content@2.9.0"], + cwd: '/fake-dir', + _: ['content@2.9.0'], }, - }); + }) - expect(addDependency).toHaveBeenCalledWith(["@nuxt/content@2.9.0"], { - cwd: "/fake-dir", + expect(addDependency).toHaveBeenCalledWith(['@nuxt/content@2.9.0'], { + cwd: '/fake-dir', dev: true, installPeerDependencies: true, - }); - }); -}); + }) + }) +})