Skip to content

Commit 0841275

Browse files
jycouetmanuel3108
andauthored
chore(cli): improve typescript detection (#710)
Co-authored-by: Manuel <[email protected]>
1 parent 55632de commit 0841275

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

.changeset/orange-rules-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"sv": patch
3+
---
4+
5+
chore(cli): improve `typescript` detection

packages/cli/commands/add/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export function fileExists(cwd: string, filePath: string): boolean {
110110
export const commonFilePaths = {
111111
packageJson: 'package.json',
112112
svelteConfig: 'svelte.config.js',
113+
jsconfig: 'jsconfig.json',
113114
tsconfig: 'tsconfig.json',
114115
viteConfig: 'vite.config.js',
115116
viteConfigTS: 'vite.config.ts'

packages/cli/commands/add/workspace.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { common, object, type AstTypes } from '@sveltejs/cli-core/js';
55
import { parseScript } from '@sveltejs/cli-core/parsers';
66
import { detect } from 'package-manager-detector';
77
import type { OptionValues, PackageManager, Workspace } from '@sveltejs/cli-core';
8-
import { TESTING } from '../../utils/env.ts';
98
import { commonFilePaths, getPackageJson, readFile } from './utils.ts';
109
import { getUserAgent } from '../../utils/package-manager.ts';
1110

@@ -20,18 +19,17 @@ export async function createWorkspace({
2019
packageManager
2120
}: CreateWorkspaceOptions): Promise<Workspace<any>> {
2221
const resolvedCwd = path.resolve(cwd);
23-
const viteConfigPath = path.join(resolvedCwd, commonFilePaths.viteConfigTS);
24-
let usesTypescript = fs.existsSync(viteConfigPath);
2522

26-
const viteConfigFile = usesTypescript ? commonFilePaths.viteConfigTS : commonFilePaths.viteConfig;
23+
// Will go up and prioritize jsconfig.json as it's first in the array
24+
const tjsconfig = find.any([commonFilePaths.jsconfig, commonFilePaths.tsconfig], { cwd });
25+
// If the file is not ending with jsconfig.json, then we are using typescript
26+
const usesTypescript = !tjsconfig?.endsWith(commonFilePaths.jsconfig);
2727

28-
if (TESTING) {
29-
// while executing tests, we only look into the direct `cwd`
30-
// as we might detect the monorepo `tsconfig.json` otherwise.
31-
usesTypescript ||= fs.existsSync(path.join(resolvedCwd, commonFilePaths.tsconfig));
32-
} else {
33-
usesTypescript ||= find.up(commonFilePaths.tsconfig, { cwd }) !== undefined;
34-
}
28+
// This is not linked with typescript detection
29+
const viteConfigPath = path.join(resolvedCwd, commonFilePaths.viteConfigTS);
30+
const viteConfigFile = fs.existsSync(viteConfigPath)
31+
? commonFilePaths.viteConfigTS
32+
: commonFilePaths.viteConfig;
3533

3634
let dependencies: Record<string, string> = {};
3735
let directory = resolvedCwd;

0 commit comments

Comments
 (0)