From f932494bf3bbf74f44c25f89a0cef8ce7628b607 Mon Sep 17 00:00:00 2001 From: CokaKoala <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:14:37 -0500 Subject: [PATCH] fix(next): normalize `ui` and `hooks` path aliases (#1516) --- .changeset/swift-tips-share.md | 5 + .github/workflows/ci.yml | 5 +- packages/cli/src/utils/get-config.ts | 8 +- packages/cli/test/commands/init.spec.ts | 2 +- .../test/fixtures/config-full/components.json | 2 +- packages/cli/test/utils/get-config.spec.ts | 112 ++++++------------ .../cli/test/utils/registry/index.spec.ts | 5 +- 7 files changed, 53 insertions(+), 86 deletions(-) create mode 100644 .changeset/swift-tips-share.md diff --git a/.changeset/swift-tips-share.md b/.changeset/swift-tips-share.md new file mode 100644 index 000000000..32351671f --- /dev/null +++ b/.changeset/swift-tips-share.md @@ -0,0 +1,5 @@ +--- +"shadcn-svelte": patch +--- + +fix: ensure `ui` and `hooks` paths are normalized diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6558c3868..bc737215c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,7 +43,10 @@ jobs: cli-test: name: CLI-Tests - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v4 diff --git a/packages/cli/src/utils/get-config.ts b/packages/cli/src/utils/get-config.ts index 07ce90dd4..055478520 100644 --- a/packages/cli/src/utils/get-config.ts +++ b/packages/cli/src/utils/get-config.ts @@ -114,8 +114,8 @@ export async function resolveConfigPaths(cwd: string, config: RawConfig) { let utilsPath = resolveImport(config.aliases.utils, pathAliases); let componentsPath = resolveImport(config.aliases.components, pathAliases); - const hooksPath = resolveImport(config.aliases.hooks, pathAliases); - const uiPath = resolveImport(config.aliases.ui, pathAliases); + let hooksPath = resolveImport(config.aliases.hooks, pathAliases); + let uiPath = resolveImport(config.aliases.ui, pathAliases); const aliasError = (type: string, alias: string) => new ConfigError( @@ -126,9 +126,13 @@ export async function resolveConfigPaths(cwd: string, config: RawConfig) { if (utilsPath === undefined) throw aliasError("utils", config.aliases.utils); if (componentsPath === undefined) throw aliasError("components", config.aliases.components); + if (hooksPath === undefined) throw aliasError("hooks", config.aliases.hooks); + if (uiPath === undefined) throw aliasError("ui", config.aliases.ui); utilsPath = path.normalize(utilsPath); componentsPath = path.normalize(componentsPath); + hooksPath = path.normalize(hooksPath); + uiPath = path.normalize(uiPath); return v.parse(configSchema, { ...config, diff --git a/packages/cli/test/commands/init.spec.ts b/packages/cli/test/commands/init.spec.ts index df39d43cd..b1501c753 100644 --- a/packages/cli/test/commands/init.spec.ts +++ b/packages/cli/test/commands/init.spec.ts @@ -60,7 +60,7 @@ it("init (config-full)", async () => { ); expect(mockMkdir).toHaveBeenNthCalledWith( 4, - expect.stringContaining("src/lib/components"), + expect.stringContaining(path.join("src", "lib", "components")), expect.anything() ); diff --git a/packages/cli/test/fixtures/config-full/components.json b/packages/cli/test/fixtures/config-full/components.json index bfde2ad1e..3ad8aeaff 100644 --- a/packages/cli/test/fixtures/config-full/components.json +++ b/packages/cli/test/fixtures/config-full/components.json @@ -13,4 +13,4 @@ }, "typescript": true, "registry": "https://next.shadcn-svelte.com/registry" -} \ No newline at end of file +} diff --git a/packages/cli/test/utils/get-config.spec.ts b/packages/cli/test/utils/get-config.spec.ts index 7c71e1368..453f196e3 100644 --- a/packages/cli/test/utils/get-config.spec.ts +++ b/packages/cli/test/utils/get-config.spec.ts @@ -1,10 +1,12 @@ -import path from "node:path"; +import { fileURLToPath } from "node:url"; import { describe, expect, it, vi } from "vitest"; import { getConfig, getRawConfig } from "../../src/utils/get-config"; import { SITE_BASE_URL } from "../../src/constants"; vi.mock("execa"); +const resolvePath = (path: string) => fileURLToPath(new URL(path, import.meta.url)); + vi.mock("node:fs", async () => { return { ...(await vi.importActual("node:fs")), @@ -14,7 +16,7 @@ vi.mock("node:fs", async () => { // gets the raw config from a fixture directory async function getRaw(fixtureDir: string) { - return await getRawConfig(path.resolve(__dirname, `../fixtures/${fixtureDir}`)); + return await getRawConfig(resolvePath(`../fixtures/${fixtureDir}`)); } describe("getRawConfig", () => { @@ -50,7 +52,7 @@ describe("getRawConfig", () => { // gets the config from a fixture directory async function getConf(fixtureDir: string) { - return await getConfig(path.resolve(__dirname, `../fixtures/${fixtureDir}`)); + return await getConfig(resolvePath(`../fixtures/${fixtureDir}`)); } describe("getConfig", () => { @@ -75,29 +77,13 @@ describe("getConfig", () => { ui: "$lib/components/ui", }, resolvedPaths: { - components: path.resolve( - __dirname, - "../fixtures/config-partial", - "./src/lib/components" - ), - tailwindConfig: path.resolve( - __dirname, - "../fixtures/config-partial", - "./tailwind.config.js" - ), - tailwindCss: path.resolve( - __dirname, - "../fixtures/config-partial", - "./src/app.pcss" - ), - utils: path.resolve(__dirname, "../fixtures/config-partial", "./src/lib/utils"), - cwd: path.resolve(__dirname, "../fixtures/config-partial"), - hooks: path.resolve(__dirname, "../fixtures/config-partial", "./src/lib/hooks"), - ui: path.resolve( - __dirname, - "../fixtures/config-partial", - "./src/lib/components/ui" - ), + components: resolvePath("../fixtures/config-partial/src/lib/components"), + tailwindConfig: resolvePath("../fixtures/config-partial/tailwind.config.js"), + tailwindCss: resolvePath("../fixtures/config-partial/src/app.pcss"), + utils: resolvePath("../fixtures/config-partial/src/lib/utils"), + cwd: resolvePath("../fixtures/config-partial"), + hooks: resolvePath("../fixtures/config-partial/src/lib/hooks"), + ui: resolvePath("../fixtures/config-partial/src/lib/components/ui"), }, typescript: true, registry: `${SITE_BASE_URL}/registry`, @@ -119,21 +105,13 @@ describe("getConfig", () => { ui: "$lib/components/ui", }, resolvedPaths: { - components: path.resolve( - __dirname, - "../fixtures/config-full", - "./src/lib/components" - ), - tailwindConfig: path.resolve( - __dirname, - "../fixtures/config-full", - "./tailwind.config.js" - ), - tailwindCss: path.resolve(__dirname, "../fixtures/config-full", "./src/app.pcss"), - utils: path.resolve(__dirname, "../fixtures/config-full", "./src/lib/utils"), - cwd: path.resolve(__dirname, "../fixtures/config-full"), - hooks: path.resolve(__dirname, "../fixtures/config-full", "./src/lib/hooks"), - ui: path.resolve(__dirname, "../fixtures/config-full", "./src/lib/components/ui"), + components: resolvePath("../fixtures/config-full/src/lib/components"), + tailwindConfig: resolvePath("../fixtures/config-full/tailwind.config.js"), + tailwindCss: resolvePath("../fixtures/config-full/src/app.pcss"), + utils: resolvePath("../fixtures/config-full/src/lib/utils"), + cwd: resolvePath("../fixtures/config-full"), + hooks: resolvePath("../fixtures/config-full/src/lib/hooks"), + ui: resolvePath("../fixtures/config-full/src/lib/components/ui"), }, typescript: true, registry: `${SITE_BASE_URL}/registry`, @@ -155,21 +133,13 @@ describe("getConfig", () => { ui: "$lib/components/ui", }, resolvedPaths: { - components: path.resolve( - __dirname, - "../fixtures/config-vite", - "./src/lib/components" - ), - tailwindConfig: path.resolve( - __dirname, - "../fixtures/config-vite", - "./tailwind.config.js" - ), - tailwindCss: path.resolve(__dirname, "../fixtures/config-vite", "./src/app.pcss"), - utils: path.resolve(__dirname, "../fixtures/config-vite", "./src/lib/utils"), - hooks: path.resolve(__dirname, "../fixtures/config-vite", "./src/lib/hooks"), - ui: path.resolve(__dirname, "../fixtures/config-vite", "./src/lib/components/ui"), - cwd: path.resolve(__dirname, "../fixtures/config-vite"), + components: resolvePath("../fixtures/config-vite/src/lib/components"), + tailwindConfig: resolvePath("../fixtures/config-vite/tailwind.config.js"), + tailwindCss: resolvePath("../fixtures/config-vite/src/app.pcss"), + utils: resolvePath("../fixtures/config-vite/src/lib/utils"), + hooks: resolvePath("../fixtures/config-vite/src/lib/hooks"), + ui: resolvePath("../fixtures/config-vite/src/lib/components/ui"), + cwd: resolvePath("../fixtures/config-vite"), }, typescript: true, registry: `${SITE_BASE_URL}/registry`, @@ -193,29 +163,13 @@ describe("getConfig", () => { hooks: "$lib/hooks", }, resolvedPaths: { - components: path.resolve( - __dirname, - "../fixtures/config-jsconfig", - "./src/lib/components" - ), - tailwindConfig: path.resolve( - __dirname, - "../fixtures/config-jsconfig", - "./tailwind.config.js" - ), - tailwindCss: path.resolve( - __dirname, - "../fixtures/config-jsconfig", - "./src/app.pcss" - ), - utils: path.resolve(__dirname, "../fixtures/config-jsconfig", "./src/lib/utils"), - hooks: path.resolve(__dirname, "../fixtures/config-jsconfig", "./src/lib/hooks"), - ui: path.resolve( - __dirname, - "../fixtures/config-jsconfig", - "./src/lib/components/ui" - ), - cwd: path.resolve(__dirname, "../fixtures/config-jsconfig"), + components: resolvePath("../fixtures/config-jsconfig/src/lib/components"), + tailwindConfig: resolvePath("../fixtures/config-jsconfig/tailwind.config.js"), + tailwindCss: resolvePath("../fixtures/config-jsconfig/src/app.pcss"), + utils: resolvePath("../fixtures/config-jsconfig/src/lib/utils"), + hooks: resolvePath("../fixtures/config-jsconfig/src/lib/hooks"), + ui: resolvePath("../fixtures/config-jsconfig/src/lib/components/ui"), + cwd: resolvePath("../fixtures/config-jsconfig"), }, typescript: false, registry: `${SITE_BASE_URL}/registry`, diff --git a/packages/cli/test/utils/registry/index.spec.ts b/packages/cli/test/utils/registry/index.spec.ts index ed2e91991..aa335461f 100644 --- a/packages/cli/test/utils/registry/index.spec.ts +++ b/packages/cli/test/utils/registry/index.spec.ts @@ -1,3 +1,4 @@ +import path from "node:path"; import { describe, expect, it } from "vitest"; import { getItemTargetPath } from "../../../src/utils/registry/index"; import { SITE_BASE_URL } from "../../../src/constants"; @@ -75,7 +76,7 @@ describe("getItemTargetPath", () => { }, "./override-path" ) - ).toEqual("src/lib/components/ui"); + ).toEqual(path.join("src", "lib", "components", "ui")); }); it("resolves item target path", async () => { @@ -89,6 +90,6 @@ describe("getItemTargetPath", () => { ], type: "registry:ui", }) - ).toEqual("src/lib/components/ui"); + ).toEqual(path.join("src", "lib", "components", "ui")); }); });