Skip to content

Commit

Permalink
fix(next): normalize ui and hooks path aliases (#1516)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonz97 authored Nov 22, 2024
1 parent 22a3a81 commit f932494
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 86 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-tips-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-svelte": patch
---

fix: ensure `ui` and `hooks` paths are normalized
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/src/utils/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/commands/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/fixtures/config-full/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
},
"typescript": true,
"registry": "https://next.shadcn-svelte.com/registry"
}
}
112 changes: 33 additions & 79 deletions packages/cli/test/utils/get-config.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import("node:fs")>("node:fs")),
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/test/utils/registry/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 () => {
Expand All @@ -89,6 +90,6 @@ describe("getItemTargetPath", () => {
],
type: "registry:ui",
})
).toEqual("src/lib/components/ui");
).toEqual(path.join("src", "lib", "components", "ui"));
});
});

0 comments on commit f932494

Please sign in to comment.