Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions community-addon-template/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineAddon({
setup: ({ kit, unsupported }) => {
if (!kit) unsupported('Requires SvelteKit');
},
run: ({ sv, options, typescript }) => {
run: ({ sv, options, language }) => {
sv.file('addon-template-demo.txt', (content) => {
if (options.demo) {
return 'This is a text file made by the Community Addon Template demo!';
Expand All @@ -25,7 +25,7 @@ export default defineAddon({
sv.file('src/DemoComponent.svelte', (content) => {
if (!options.demo) return content;
const { ast, generateCode } = parseSvelte(content);
svelte.ensureScript(ast, { langTs: typescript });
svelte.ensureScript(ast, { language });
js.imports.addDefault(ast.instance.content, {
from: '../addon-template-demo.txt?raw',
as: 'demo'
Expand Down
4 changes: 2 additions & 2 deletions packages/sv/lib/addons/_tests/devtools-json/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const { test, testCases } = setupTest(
test.concurrent.for(testCases)('devtools-json $variant', (testCase, ctx) => {
const cwd = ctx.cwd(testCase);

const ext = testCase.variant.includes('ts') ? 'ts' : 'js';
const viteFile = path.resolve(cwd, `vite.config.${ext}`);
const language = testCase.variant.includes('ts') ? 'ts' : 'js';
const viteFile = path.resolve(cwd, `vite.config.${language}`);
const viteContent = fs.readFileSync(viteFile, 'utf8');

// Check if we have the import part
Expand Down
4 changes: 2 additions & 2 deletions packages/sv/lib/addons/_tests/lucia/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ test.concurrent.for(testCases)('lucia $variant', async (testCase, { page, ...ctx
// kill server process when we're done
ctx.onTestFinished(async () => await close());

const ext = testCase.variant.includes('ts') ? 'ts' : 'js';
const filePath = path.resolve(cwd, `src/routes/demo/lucia/+page.server.${ext}`);
const language = testCase.variant.includes('ts') ? 'ts' : 'js';
const filePath = path.resolve(cwd, `src/routes/demo/lucia/+page.server.${language}`);
const fileContent = fs.readFileSync(filePath, 'utf8');
expect(fileContent).toContain(`export const actions`);
});
4 changes: 2 additions & 2 deletions packages/sv/lib/addons/_tests/playwright/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const { test, testCases } = setupTest(
test.concurrent.for(testCases)('playwright $variant', (testCase, { expect, ...ctx }) => {
const cwd = ctx.cwd(testCase);

const ext = testCase.variant.includes('ts') ? 'ts' : 'js';
const playwrightConfig = path.resolve(cwd, `playwright.config.${ext}`);
const language = testCase.variant.includes('ts') ? 'ts' : 'js';
const playwrightConfig = path.resolve(cwd, `playwright.config.${language}`);
const configContent = fs.readFileSync(playwrightConfig, 'utf8');

// Check if we have the imports
Expand Down
4 changes: 2 additions & 2 deletions packages/sv/lib/addons/_tests/vitest/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ test.concurrent.for(testCases)('vitest $variant', (testCase, { expect, ...ctx })

expect(() => execSync('pnpm test', { cwd, stdio: 'pipe' })).not.toThrow();

const ext = testCase.variant.includes('ts') ? 'ts' : 'js';
const viteFile = path.resolve(cwd, `vite.config.${ext}`);
const language = testCase.variant.includes('ts') ? 'ts' : 'js';
const viteFile = path.resolve(cwd, `vite.config.${language}`);
const viteContent = fs.readFileSync(viteFile, 'utf8');

expect(viteContent).toContain(`vitest/config`);
Expand Down
11 changes: 8 additions & 3 deletions packages/sv/lib/addons/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { imports, exports, common } from '../core/tooling/js/index.ts';
import { toFragment, type SvelteAst, ensureScript } from '../core/tooling/svelte/index.ts';
import { toFragment, type SvelteAst } from '../core/tooling/svelte/index.ts';
import { parseScript, parseSvelte } from '../core/tooling/parsers.ts';
import process from 'node:process';
import { svelte } from '../core/index.ts';

export function addEslintConfigPrettier(content: string): string {
const { ast, generateCode } = parseScript(content);
Expand Down Expand Up @@ -64,8 +65,13 @@ export function addEslintConfigPrettier(content: string): string {
return generateCode();
}

export function addToDemoPage(existingContent: string, path: string, langTs: boolean): string {
export function addToDemoPage(
existingContent: string,
path: string,
language: 'ts' | 'js'
): string {
const { ast, generateCode } = parseSvelte(existingContent);
svelte.ensureScript(ast, { language });

for (const node of ast.fragment.nodes) {
if (node.type === 'RegularElement') {
Expand All @@ -86,7 +92,6 @@ export function addToDemoPage(existingContent: string, path: string, langTs: boo
}
}

ensureScript(ast, { langTs });
imports.addNamed(ast.instance.content, { imports: ['resolve'], from: '$app/paths' });

ast.fragment.nodes.unshift(...toFragment(`<a href={resolve('/demo/${path}')}>${path}</a>`));
Expand Down
10 changes: 5 additions & 5 deletions packages/sv/lib/addons/drizzle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ export default defineAddon({

if (!kit) return unsupported('Requires SvelteKit');
},
run: ({ sv, typescript, options, kit, dependencyVersion, cwd, cancel, files }) => {
run: ({ sv, language, options, kit, dependencyVersion, cwd, cancel, files }) => {
if (!kit) throw new Error('SvelteKit is required');

const ext = typescript ? 'ts' : 'js';
const typescript = language === 'ts';
const baseDBPath = path.resolve(cwd, kit.libDirectory, 'server', 'db');
const paths = {
'drizzle config': path.resolve(cwd, `drizzle.config.${ext}`),
'database schema': path.resolve(baseDBPath, `schema.${ext}`),
database: path.resolve(baseDBPath, `index.${ext}`)
'drizzle config': path.resolve(cwd, `drizzle.config.${language}`),
'database schema': path.resolve(baseDBPath, `schema.${language}`),
database: path.resolve(baseDBPath, `index.${language}`)
};

for (const [fileType, filePath] of Object.entries(paths)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/sv/lib/addons/eslint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default defineAddon({
shortDescription: 'linter',
homepage: 'https://eslint.org',
options: {},
run: ({ sv, typescript, dependencyVersion, files }) => {
run: ({ sv, language, dependencyVersion, files }) => {
const typescript = language === 'ts';
const prettierInstalled = Boolean(dependencyVersion('prettier'));

sv.devDependency('eslint', '^9.39.1');
Expand Down
18 changes: 9 additions & 9 deletions packages/sv/lib/addons/lucia/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export default defineAddon({

runsAfter('tailwindcss');
},
run: ({ sv, typescript, options, kit, dependencyVersion }) => {
const ext = typescript ? 'ts' : 'js';
run: ({ sv, language, options, kit, dependencyVersion }) => {
const typescript = language === 'ts';

sv.devDependency('@oslojs/crypto', '^1.0.1');
sv.devDependency('@oslojs/encoding', '^1.1.0');
Expand All @@ -56,7 +56,7 @@ export default defineAddon({
sv.dependency('@node-rs/argon2', '^2.0.2');
}

sv.file(`drizzle.config.${ext}`, (content) => {
sv.file(`drizzle.config.${language}`, (content) => {
const { ast, generateCode } = parseScript(content);
const isProp = (name: string, node: AstTypes.Property) =>
node.key.type === 'Identifier' && node.key.name === name;
Expand Down Expand Up @@ -222,7 +222,7 @@ export default defineAddon({
return code;
});

sv.file(`${kit?.libDirectory}/server/auth.${ext}`, (content) => {
sv.file(`${kit?.libDirectory}/server/auth.${language}`, (content) => {
const { ast, generateCode } = parseScript(content);

js.imports.addNamespace(ast, { from: '$lib/server/db/schema', as: 'table' });
Expand Down Expand Up @@ -383,11 +383,11 @@ export default defineAddon({
});
}

sv.file(`src/hooks.server.${ext}`, (content) => {
sv.file(`src/hooks.server.${language}`, (content) => {
const { ast, generateCode } = parseScript(content);
js.imports.addNamespace(ast, { from: '$lib/server/auth', as: 'auth' });
js.kit.addHooksHandle(ast, {
typescript,
language,
newHandleName: 'handleAuth',
handleContent: getAuthHandleContent()
});
Expand All @@ -396,10 +396,10 @@ export default defineAddon({

if (options.demo) {
sv.file(`${kit?.routesDirectory}/demo/+page.svelte`, (content) => {
return addToDemoPage(content, 'lucia', typescript);
return addToDemoPage(content, 'lucia', language);
});

sv.file(`${kit!.routesDirectory}/demo/lucia/login/+page.server.${ext}`, (content) => {
sv.file(`${kit!.routesDirectory}/demo/lucia/login/+page.server.${language}`, (content) => {
if (content) {
const filePath = `${kit!.routesDirectory}/demo/lucia/login/+page.server.${typescript ? 'ts' : 'js'}`;
log.warn(`Existing ${colors.yellow(filePath)} file. Could not update.`);
Expand Down Expand Up @@ -572,7 +572,7 @@ export default defineAddon({
`;
});

sv.file(`${kit!.routesDirectory}/demo/lucia/+page.server.${ext}`, (content) => {
sv.file(`${kit!.routesDirectory}/demo/lucia/+page.server.${language}`, (content) => {
if (content) {
const filePath = `${kit!.routesDirectory}/demo/lucia/+page.server.${typescript ? 'ts' : 'js'}`;
log.warn(`Existing ${colors.yellow(filePath)} file. Could not update.`);
Expand Down
16 changes: 7 additions & 9 deletions packages/sv/lib/addons/paraglide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export default defineAddon({
setup: ({ kit, unsupported }) => {
if (!kit) unsupported('Requires SvelteKit');
},
run: ({ sv, options, files, typescript, kit }) => {
const ext = typescript ? 'ts' : 'js';
run: ({ sv, options, files, language, kit }) => {
if (!kit) throw new Error('SvelteKit is required');

const paraglideOutDir = 'src/lib/paraglide';
Expand All @@ -87,7 +86,7 @@ export default defineAddon({
});

// reroute hook
sv.file(`src/hooks.${ext}`, (content) => {
sv.file(`src/hooks.${language}`, (content) => {
const { ast, generateCode } = parseScript(content);
imports.addNamed(ast, {
from: '$lib/paraglide/runtime',
Expand All @@ -113,7 +112,7 @@ export default defineAddon({
});

// handle hook
sv.file(`src/hooks.server.${ext}`, (content) => {
sv.file(`src/hooks.server.${language}`, (content) => {
const { ast, generateCode } = parseScript(content);
imports.addNamed(ast, {
from: '$lib/paraglide/server',
Expand All @@ -127,7 +126,7 @@ export default defineAddon({
});
});`;
kitJs.addHooksHandle(ast, {
typescript,
language,
newHandleName: 'handleParaglide',
handleContent: hookHandleContent
});
Expand Down Expand Up @@ -182,7 +181,7 @@ export default defineAddon({

sv.file(`${kit.routesDirectory}/+layout.svelte`, (content) => {
const { ast, generateCode } = parseSvelte(content);
svelte.ensureScript(ast);
svelte.ensureScript(ast, { language });
imports.addNamed(ast.instance.content, {
imports: ['locales', 'localizeHref'],
from: '$lib/paraglide/runtime'
Expand All @@ -200,14 +199,13 @@ export default defineAddon({

if (options.demo) {
sv.file(`${kit.routesDirectory}/demo/+page.svelte`, (content) => {
return addToDemoPage(content, 'paraglide', typescript);
return addToDemoPage(content, 'paraglide', language);
});

// add usage example
sv.file(`${kit.routesDirectory}/demo/paraglide/+page.svelte`, (content) => {
const { ast, generateCode } = parseSvelte(content);
svelte.ensureScript(ast, { langTs: typescript });

svelte.ensureScript(ast, { language });
imports.addNamed(ast.instance.content, {
imports: { m: 'm' },
from: '$lib/paraglide/messages.js'
Expand Down
8 changes: 3 additions & 5 deletions packages/sv/lib/addons/playwright/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export default defineAddon({
shortDescription: 'browser testing',
homepage: 'https://playwright.dev',
options: {},
run: ({ sv, typescript, files }) => {
const ext = typescript ? 'ts' : 'js';

run: ({ sv, language, files }) => {
sv.devDependency('@playwright/test', '^1.57.0');

sv.file(files.package, (content) => {
Expand All @@ -30,7 +28,7 @@ export default defineAddon({
return 'test-results\n' + content.trim();
});

sv.file(`e2e/demo.test.${ext}`, (content) => {
sv.file(`e2e/demo.test.${language}`, (content) => {
if (content) return content;

return dedent`
Expand All @@ -43,7 +41,7 @@ export default defineAddon({
`;
});

sv.file(`playwright.config.${ext}`, (content) => {
sv.file(`playwright.config.${language}`, (content) => {
const { ast, generateCode } = parseScript(content);
const defineConfig = common.parseExpression('defineConfig({})');
const { value: defaultExport } = exports.createDefault(ast, { fallback: defineConfig });
Expand Down
4 changes: 2 additions & 2 deletions packages/sv/lib/addons/sveltekit-adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default defineAddon({
setup: ({ kit, unsupported }) => {
if (!kit) unsupported('Requires SvelteKit');
},
run: ({ sv, options, files, cwd, packageManager, typescript }) => {
run: ({ sv, options, files, cwd, packageManager, language }) => {
const adapter = adapters.find((a) => a.id === options.adapter)!;

// removes previously installed adapters
Expand Down Expand Up @@ -172,7 +172,7 @@ export default defineAddon({
});

const jsconfig = fileExists(cwd, 'jsconfig.json');
const typeChecked = typescript || jsconfig;
const typeChecked = language === 'ts' || jsconfig;

if (typeChecked) {
// Ignore generated Cloudflare Types
Expand Down
6 changes: 3 additions & 3 deletions packages/sv/lib/addons/tailwindcss/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default defineAddon({
shortDescription: 'css framework',
homepage: 'https://tailwindcss.com',
options,
run: ({ sv, options, files, kit, dependencyVersion, typescript }) => {
run: ({ sv, options, files, kit, dependencyVersion, language }) => {
const prettierInstalled = Boolean(dependencyVersion('prettier'));

sv.devDependency('tailwindcss', '^4.1.17');
Expand Down Expand Up @@ -89,7 +89,7 @@ export default defineAddon({
const stylesheetRelative = files.getRelative({ from: appSvelte, to: files.stylesheet });
sv.file(appSvelte, (content) => {
const { ast, generateCode } = parseSvelte(content);
svelte.ensureScript(ast, { langTs: typescript });
svelte.ensureScript(ast, { language });
imports.addEmpty(ast.instance.content, { from: stylesheetRelative });
return generateCode();
});
Expand All @@ -98,7 +98,7 @@ export default defineAddon({
const stylesheetRelative = files.getRelative({ from: layoutSvelte, to: files.stylesheet });
sv.file(layoutSvelte, (content) => {
const { ast, generateCode } = parseSvelte(content);
svelte.ensureScript(ast, { langTs: typescript });
svelte.ensureScript(ast, { language });
imports.addEmpty(ast.instance.content, { from: stylesheetRelative });

if (content.length === 0) {
Expand Down
13 changes: 6 additions & 7 deletions packages/sv/lib/addons/vitest-addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export default defineAddon({
homepage: 'https://vitest.dev',
options,

run: ({ sv, files, typescript, kit, options, dependencyVersion }) => {
const ext = typescript ? 'ts' : 'js';
run: ({ sv, files, language, kit, options, dependencyVersion }) => {
const unitTesting = options.usages.includes('unit');
const componentTesting = options.usages.includes('component');

Expand Down Expand Up @@ -56,7 +55,7 @@ export default defineAddon({
});

if (unitTesting) {
sv.file(`src/demo.spec.${ext}`, (content) => {
sv.file(`src/demo.spec.${language}`, (content) => {
if (content) return content;

return dedent`
Expand All @@ -73,8 +72,8 @@ export default defineAddon({

if (componentTesting) {
const fileName = kit
? `${kit.routesDirectory}/page.svelte.spec.${ext}`
: `src/App.svelte.test.${ext}`;
? `${kit.routesDirectory}/page.svelte.spec.${language}`
: `src/App.svelte.test.${language}`;

sv.file(fileName, (content) => {
if (content) return content;
Expand Down Expand Up @@ -160,7 +159,7 @@ export default defineAddon({
});
},

nextSteps: ({ highlighter, typescript, options }) => {
nextSteps: ({ highlighter, language, options }) => {
const toReturn: string[] = [];

if (vitestV3Installed) {
Expand All @@ -175,7 +174,7 @@ export default defineAddon({
`${highlighter.optional('Optional')} Check ${highlighter.path('./vite.config.ts')} and remove duplicate project definitions`
);
toReturn.push(
`${highlighter.optional('Optional')} Remove ${highlighter.path('./vitest-setup-client' + (typescript ? '.ts' : '.js'))} file`
`${highlighter.optional('Optional')} Remove ${highlighter.path('./vitest-setup-client.' + language)} file`
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sv/lib/cli/add/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export async function createWorkspace({
return {
cwd: resolvedCwd,
packageManager: packageManager ?? (await detect({ cwd }))?.name ?? getUserAgent() ?? 'npm',
typescript,
language: typescript ? 'ts' : 'js',
files: {
viteConfig,
svelteConfig,
Expand Down
2 changes: 1 addition & 1 deletion packages/sv/lib/cli/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export async function createVirtualWorkspace({

const virtualWorkspace: Workspace = {
...tentativeWorkspace,
typescript: type === 'typescript',
language: type === 'typescript' ? 'ts' : 'js',
files: {
...tentativeWorkspace.files,
viteConfig: type === 'typescript' ? commonFilePaths.viteConfigTS : commonFilePaths.viteConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script lang="ts">
<script>
import { resolve } from '$app/paths';
</script>

Expand Down
Loading
Loading