Skip to content

Commit 10cefa9

Browse files
sacrosancticjycouetmanuel3108
authored
fix(cli): kit projects were detected incorrectly (#810)
* fix typo * initial attempt * reuse stylesheet variable * cleanup * lint * rerun test * . * lint * option to override for virtual workspace * adding a cli test will all addons * make it an object * Revert "make it an object" This reverts commit 98d7fe0. * Reapply "make it an object" This reverts commit 7ec105a. * Create proud-baboons-cheat.md --------- Co-authored-by: jycouet <jycouet@gmail.com> Co-authored-by: Manuel <30698007+manuel3108@users.noreply.github.com>
1 parent e34d065 commit 10cefa9

File tree

5 files changed

+60
-21
lines changed

5 files changed

+60
-21
lines changed

.changeset/proud-baboons-cheat.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+
fix(cli): `kit` projects were detected incorrectly

packages/addons/prettier/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineAddon({
77
shortDescription: 'formatter',
88
homepage: 'https://prettier.io',
99
options: {},
10-
run: ({ sv, dependencyVersion, kit, files }) => {
10+
run: ({ sv, dependencyVersion, files }) => {
1111
const tailwindcssInstalled = Boolean(dependencyVersion('tailwindcss'));
1212
if (tailwindcssInstalled) sv.devDependency('prettier-plugin-tailwindcss', '^0.7.1');
1313

@@ -53,7 +53,7 @@ export default defineAddon({
5353
if (!plugins.includes('prettier-plugin-tailwindcss')) {
5454
data.plugins.unshift('prettier-plugin-tailwindcss');
5555
}
56-
data.tailwindStylesheet ??= kit ? `${kit?.routesDirectory}/layout.css` : './src/app.css';
56+
data.tailwindStylesheet ??= files.getRelative({ to: files.stylesheet });
5757
}
5858
if (!plugins.includes('prettier-plugin-svelte')) {
5959
data.plugins.unshift('prettier-plugin-svelte');

packages/cli/commands/add/workspace.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ import { getUserAgent } from '../../utils/package-manager.ts';
1111
type CreateWorkspaceOptions = {
1212
cwd: string;
1313
packageManager?: PackageManager;
14+
override?: {
15+
kit?: Workspace['kit'];
16+
};
1417
};
1518
export async function createWorkspace({
1619
cwd,
17-
packageManager
20+
packageManager,
21+
override
1822
}: CreateWorkspaceOptions): Promise<Workspace> {
1923
const resolvedCwd = path.resolve(cwd);
2024

@@ -28,8 +32,8 @@ export async function createWorkspace({
2832
const viteConfig = fs.existsSync(viteConfigPath)
2933
? commonFilePaths.viteConfigTS
3034
: commonFilePaths.viteConfig;
31-
const sveteConfigPath = path.join(resolvedCwd, commonFilePaths.svelteConfigTS);
32-
const svelteConfig = fs.existsSync(sveteConfigPath)
35+
const svelteConfigPath = path.join(resolvedCwd, commonFilePaths.svelteConfigTS);
36+
const svelteConfig = fs.existsSync(svelteConfigPath)
3337
? commonFilePaths.svelteConfigTS
3438
: commonFilePaths.svelteConfig;
3539

@@ -59,7 +63,12 @@ export async function createWorkspace({
5963
dependencies[key] = value.replaceAll(/[^\d|.]/g, '');
6064
}
6165

62-
const kit = dependencies['@sveltejs/kit'] ? parseKitOptions(resolvedCwd) : undefined;
66+
const kit = override?.kit
67+
? override.kit
68+
: dependencies['@sveltejs/kit']
69+
? parseKitOptions(resolvedCwd)
70+
: undefined;
71+
6372
const stylesheet: `${string}/layout.css` | 'src/app.css' = kit
6473
? `${kit.routesDirectory}/layout.css`
6574
: 'src/app.css';

packages/cli/commands/create.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,18 @@ export async function createVirtualWorkspace({
363363
packageManager,
364364
type
365365
}: CreateVirtualWorkspaceOptions): Promise<Workspace> {
366-
const tentativeWorkspace = await createWorkspace({ cwd, packageManager });
366+
const override: { kit?: Workspace['kit'] } = {};
367+
368+
// These are our default project structure so we know that it's a kit project
369+
if (template === 'minimal' || template === 'demo' || template === 'library') {
370+
override.kit = {
371+
routesDirectory: 'src/routes',
372+
libDirectory: 'src/lib'
373+
};
374+
}
375+
376+
const tentativeWorkspace = await createWorkspace({ cwd, packageManager, override });
377+
367378
const virtualWorkspace: Workspace = {
368379
...tentativeWorkspace,
369380
typescript: type === 'typescript',
@@ -372,17 +383,8 @@ export async function createVirtualWorkspace({
372383
viteConfig: type === 'typescript' ? commonFilePaths.viteConfigTS : commonFilePaths.viteConfig,
373384
svelteConfig:
374385
type === 'typescript' ? commonFilePaths.svelteConfigTS : commonFilePaths.svelteConfig
375-
},
376-
kit: undefined,
377-
dependencyVersion: () => undefined
386+
}
378387
};
379388

380-
if (template === 'minimal' || template === 'demo' || template === 'library') {
381-
virtualWorkspace.kit = {
382-
routesDirectory: 'src/routes',
383-
libDirectory: 'src/lib'
384-
};
385-
}
386-
387389
return virtualWorkspace;
388390
}

packages/cli/tests/cli.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,32 @@ beforeAll(() => {
1616
});
1717

1818
describe('cli', () => {
19-
it('should be able to create a new project with cli command', async () => {
19+
const testCases = [
20+
{ projectName: 'create-only', args: ['--no-add-ons'] },
21+
{
22+
projectName: 'create-with-all-addons',
23+
args: [
24+
'--add',
25+
'prettier',
26+
'eslint',
27+
'vitest=usages:unit,component',
28+
'playwright',
29+
'tailwindcss=plugins:typography,forms',
30+
'sveltekit-adapter=adapter:node',
31+
'devtools-json',
32+
'drizzle=database:sqlite+sqlite:libsql',
33+
'lucia=demo:yes',
34+
'mdsvex',
35+
'paraglide=languageTags:en,es+demo:yes',
36+
'mcp=ide:claude-code,cursor,gemini,opencode,vscode,other+setup:local'
37+
]
38+
}
39+
];
40+
41+
it.for(testCases)('should create a new project with name $projectName', async (testCase) => {
42+
const { projectName, args } = testCase;
2043
const svBinPath = path.resolve(monoRepoPath, 'packages', 'cli', 'dist', 'bin.js');
21-
const testOutputPath = path.resolve(monoRepoPath, '.test-output', 'cli', 'test-project');
44+
const testOutputPath = path.resolve(monoRepoPath, '.test-output', 'cli', projectName);
2245

2346
const result = await exec(
2447
'node',
@@ -31,7 +54,7 @@ describe('cli', () => {
3154
'--types',
3255
'ts',
3356
'--no-install',
34-
'--no-add-ons'
57+
...args
3558
],
3659
{ nodeOptions: { stdio: 'ignore' } }
3760
);
@@ -45,6 +68,6 @@ describe('cli', () => {
4568
// package.json has a name
4669
const packageJsonPath = path.resolve(testOutputPath, 'package.json');
4770
const packageJson = parseJson(fs.readFileSync(packageJsonPath, 'utf-8'));
48-
expect(packageJson.name).toBe('test-project');
71+
expect(packageJson.name).toBe(projectName);
4972
});
5073
});

0 commit comments

Comments
 (0)