Skip to content

Commit

Permalink
fix: Support jsconfig.json (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonz97 authored Jan 5, 2024
1 parent 5b3bd31 commit 59dca2c
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eighty-mayflies-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-svelte": patch
---

fix: Support use of `jsconfig.json`
86 changes: 86 additions & 0 deletions packages/cli/src/utils/find-tsconfig.ts
Original file line number Diff line number Diff line change
@@ -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<string>} 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<string | void> {
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<string>;

/**
* 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;
}
3 changes: 2 additions & 1 deletion packages/cli/src/utils/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/test/fixtures/config-jsconfig/components.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
17 changes: 17 additions & 0 deletions packages/cli/test/fixtures/config-jsconfig/jsconfig.json
Original file line number Diff line number Diff line change
@@ -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/*"]
}
}
}
14 changes: 14 additions & 0 deletions packages/cli/test/fixtures/config-jsconfig/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
37 changes: 37 additions & 0 deletions packages/cli/test/utils/get-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
});
});
});

0 comments on commit 59dca2c

Please sign in to comment.