From 5abeb83deb94fe7e46254819c19ee89ad5cce8bb Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 5 Jan 2024 13:26:05 -0500 Subject: [PATCH 1/3] support jsconfig --- packages/cli/src/utils/find-tsconfig.ts | 86 +++++++++++++++++++++++++ packages/cli/src/utils/get-config.ts | 3 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/utils/find-tsconfig.ts diff --git a/packages/cli/src/utils/find-tsconfig.ts b/packages/cli/src/utils/find-tsconfig.ts new file mode 100644 index 000000000..7d20643bb --- /dev/null +++ b/packages/cli/src/utils/find-tsconfig.ts @@ -0,0 +1,86 @@ +// Sourced from tsconfck v2 + +import { promises as fs } from "fs"; +import path from "path"; + +/** + * find the closest tsconfig.json file + * + * @param {string} filename - path to file to find tsconfig for (absolute or relative to cwd) + * @param {TSConfckFindOptions} options - options + * @returns {Promise} absolute path to closest tsconfig.json + */ +export async function find(filename: string, options?: TSConfckFindOptions) { + let dir = path.dirname(path.resolve(filename)); + const root = options?.root ? path.resolve(options.root) : null; + while (dir) { + const tsconfig = await tsconfigInDir(dir, options); + if (tsconfig) { + return tsconfig; + } else { + if (root === dir) { + break; + } + const parent = path.dirname(dir); + if (parent === dir) { + break; + } else { + dir = parent; + } + } + } + throw new Error(`no tsconfig file found for ${filename}`); +} + +// Modified to also search for jsconfig.json +async function tsconfigInDir( + dir: string, + options?: TSConfckFindOptions +): Promise { + const tsconfig = path.join(dir, "tsconfig.json"); + const jsconfig = path.join(dir, "jsconfig.json"); + + if (options?.tsConfigPaths) { + return options.tsConfigPaths.has(tsconfig) ? tsconfig : undefined; + } + try { + const stat = await fs.stat(tsconfig); + if (stat.isFile() || stat.isFIFO()) { + return tsconfig; + } + } catch (e: any) { + // ignore does not exist error + if (e.code !== "ENOENT") { + throw e; + } + } + + try { + let stat = await fs.stat(jsconfig); + if (stat.isFile() || stat.isFIFO()) { + return jsconfig; + } + } catch (e: any) { + // ignore does not exist error + if (e.code !== "ENOENT") { + throw e; + } + } +} + +export interface TSConfckFindOptions { + /** + * Set of known tsconfig file locations to use instead of scanning the file system + * + * This is better for performance in projects like vite where find is called frequently but tsconfig locations rarely change + * You can use `findAll` to build this + */ + tsConfigPaths?: Set; + + /** + * project root dir, does not continue scanning outside of this directory. + * + * Improves performance but may lead to different results from native typescript when no tsconfig is found inside root + */ + root?: string; +} diff --git a/packages/cli/src/utils/get-config.ts b/packages/cli/src/utils/get-config.ts index fc0bf45b7..d3740b939 100644 --- a/packages/cli/src/utils/get-config.ts +++ b/packages/cli/src/utils/get-config.ts @@ -2,8 +2,9 @@ import { promises as fs } from "fs"; import path from "path"; import chalk from "chalk"; import { execa } from "execa"; -import { find, parseNative } from "tsconfck"; +import { parseNative } from "tsconfck"; import * as z from "zod"; +import { find } from "./find-tsconfig"; import { isUsingSvelteKit } from "./get-package-info"; import { getPackageManager } from "./get-package-manager"; import { logger } from "./logger"; From e760f039b0706bf64a586eca8498026df5397f68 Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 5 Jan 2024 13:27:01 -0500 Subject: [PATCH 2/3] changeset --- .changeset/eighty-mayflies-fly.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/eighty-mayflies-fly.md diff --git a/.changeset/eighty-mayflies-fly.md b/.changeset/eighty-mayflies-fly.md new file mode 100644 index 000000000..87d9e133e --- /dev/null +++ b/.changeset/eighty-mayflies-fly.md @@ -0,0 +1,5 @@ +--- +"shadcn-svelte": patch +--- + +fix: Support use of `jsconfig.json` From fd2b640360f30525beb8e04f3d6fffaa326d7fe5 Mon Sep 17 00:00:00 2001 From: AdrianGonz97 <31664583+AdrianGonz97@users.noreply.github.com> Date: Fri, 5 Jan 2024 13:29:31 -0500 Subject: [PATCH 3/3] added test of jsconfig --- .../fixtures/config-jsconfig/components.json | 12 ++++++ .../fixtures/config-jsconfig/jsconfig.json | 17 +++++++++ .../fixtures/config-jsconfig/package.json | 14 +++++++ packages/cli/test/utils/get-config.spec.ts | 37 +++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 packages/cli/test/fixtures/config-jsconfig/components.json create mode 100644 packages/cli/test/fixtures/config-jsconfig/jsconfig.json create mode 100644 packages/cli/test/fixtures/config-jsconfig/package.json diff --git a/packages/cli/test/fixtures/config-jsconfig/components.json b/packages/cli/test/fixtures/config-jsconfig/components.json new file mode 100644 index 000000000..804221b5a --- /dev/null +++ b/packages/cli/test/fixtures/config-jsconfig/components.json @@ -0,0 +1,12 @@ +{ + "style": "new-york", + "tailwind": { + "config": "tailwind.config.js", + "css": "src/app.pcss", + "baseColor": "zinc" + }, + "aliases": { + "utils": "$lib/utils", + "components": "$lib/components" + } +} diff --git a/packages/cli/test/fixtures/config-jsconfig/jsconfig.json b/packages/cli/test/fixtures/config-jsconfig/jsconfig.json new file mode 100644 index 000000000..456bb65aa --- /dev/null +++ b/packages/cli/test/fixtures/config-jsconfig/jsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "resolveJsonModule": true, + "skipLibCheck": true, + "sourceMap": true, + "strict": true, + "moduleResolution": "bundler", + "paths": { + "$lib": ["./src/lib"], + "$lib/*": ["./src/lib/*"] + } + } +} diff --git a/packages/cli/test/fixtures/config-jsconfig/package.json b/packages/cli/test/fixtures/config-jsconfig/package.json new file mode 100644 index 000000000..29857aab7 --- /dev/null +++ b/packages/cli/test/fixtures/config-jsconfig/package.json @@ -0,0 +1,14 @@ +{ + "name": "test-cli-config-full", + "version": "0.0.0", + "private": true, + "devDependencies": { + "@sveltejs/vite-plugin-svelte": "^3.0.0", + "svelte": "^4.2.7", + "svelte-check": "^3.6.0", + "tslib": "^2.4.1", + "typescript": "^5.0.0", + "vite": "^5.0.3" + }, + "type": "module" +} diff --git a/packages/cli/test/utils/get-config.spec.ts b/packages/cli/test/utils/get-config.spec.ts index 45ac91dfb..99d591f44 100644 --- a/packages/cli/test/utils/get-config.spec.ts +++ b/packages/cli/test/utils/get-config.spec.ts @@ -165,4 +165,41 @@ describe("getConfig", () => { } }); }); + + it("handles cases where the project uses jsconfig.json", async () => { + expect(await getConf("config-jsconfig")).toEqual({ + style: "new-york", + tailwind: { + config: "tailwind.config.js", + css: "src/app.pcss", + baseColor: "zinc" + }, + aliases: { + utils: "$lib/utils", + components: "$lib/components" + }, + 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" + ) + } + }); + }); });