Skip to content

Commit 7d45873

Browse files
authored
clean things up (#1014)
1 parent 58eba1a commit 7d45873

12 files changed

Lines changed: 260 additions & 287 deletions

File tree

packages/sv-utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,4 @@ export { color } from './color.ts';
8282

8383
// Types
8484
export type { Comments, AstTypes, SvelteAst } from './tooling/index.ts';
85+
export type { TransformFn } from './tooling/transforms.ts';

packages/sv-utils/src/tooling/transforms.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { type RootWithInstance, ensureScript } from './svelte/index.ts';
1818
import * as svelteNs from './svelte/index.ts';
1919
import * as textNs from './text.ts';
2020

21+
export type TransformFn = (content: string) => string;
22+
2123
type TransformOptions = {
2224
/** Called when parsing fails. If provided, the original content is returned unchanged. */
2325
onParseError?: (error: unknown) => void;
@@ -69,7 +71,7 @@ export const transforms = {
6971
* Return `false` from the callback to abort - the original content is returned unchanged.
7072
*/
7173
script(
72-
cb: (args: {
74+
cb: (file: {
7375
ast: TsEstree.Program;
7476
comments: Comments;
7577
content: string;
@@ -92,7 +94,7 @@ export const transforms = {
9294
* Return `false` from the callback to abort - the original content is returned unchanged.
9395
*/
9496
svelte(
95-
cb: (args: {
97+
cb: (file: {
9698
ast: SvelteAst.Root;
9799
content: string;
98100
svelte: typeof svelteNs;
@@ -119,14 +121,14 @@ export const transforms = {
119121
*/
120122
svelteScript(
121123
scriptOptions: { language: 'ts' | 'js' },
122-
cb: (args: {
124+
cb: (file: {
123125
ast: RootWithInstance;
124126
content: string;
125127
svelte: typeof svelteNs;
126128
js: typeof jsNs;
127129
}) => void | false,
128130
options?: TransformOptions
129-
): (content: string) => string {
131+
): TransformFn {
130132
return (content) => {
131133
const parsed = withParseError(() => parseSvelte(content), options);
132134
if (!parsed) return content;
@@ -148,13 +150,13 @@ export const transforms = {
148150
* Return `false` from the callback to abort - the original content is returned unchanged.
149151
*/
150152
css(
151-
cb: (args: {
153+
cb: (file: {
152154
ast: Omit<SvelteAst.CSS.StyleSheetBase, 'attributes' | 'content'>;
153155
content: string;
154156
css: typeof cssNs;
155157
}) => void | false,
156158
options?: TransformOptions
157-
): (content: string) => string {
159+
): TransformFn {
158160
return (content) => {
159161
const parsed = withParseError(() => parseCss(content), options);
160162
if (!parsed) return content;
@@ -170,9 +172,9 @@ export const transforms = {
170172
* Return `false` from the callback to abort - the original content is returned unchanged.
171173
*/
172174
json<T = any>(
173-
cb: (args: { data: T; content: string; json: typeof jsonNs }) => void | false,
175+
cb: (file: { data: T; content: string; json: typeof jsonNs }) => void | false,
174176
options?: TransformOptions
175-
): (content: string) => string {
177+
): TransformFn {
176178
return (content) => {
177179
const parsed = withParseError(() => parseJson(content), options);
178180
if (!parsed) return content;
@@ -188,9 +190,9 @@ export const transforms = {
188190
* Return `false` from the callback to abort - the original content is returned unchanged.
189191
*/
190192
yaml(
191-
cb: (args: { data: ReturnType<typeof parseYaml>['data']; content: string }) => void | false,
193+
cb: (file: { data: ReturnType<typeof parseYaml>['data']; content: string }) => void | false,
192194
options?: TransformOptions
193-
): (content: string) => string {
195+
): TransformFn {
194196
return (content) => {
195197
const parsed = withParseError(() => parseYaml(content), options);
196198
if (!parsed) return content;
@@ -206,9 +208,9 @@ export const transforms = {
206208
* Return `false` from the callback to abort - the original content is returned unchanged.
207209
*/
208210
toml(
209-
cb: (args: { data: TomlTable; content: string }) => void | false,
211+
cb: (file: { data: TomlTable; content: string }) => void | false,
210212
options?: TransformOptions
211-
): (content: string) => string {
213+
): TransformFn {
212214
return (content) => {
213215
const parsed = withParseError(() => parseToml(content), options);
214216
if (!parsed) return content;
@@ -224,9 +226,9 @@ export const transforms = {
224226
* Return `false` from the callback to abort - the original content is returned unchanged.
225227
*/
226228
html(
227-
cb: (args: { ast: SvelteAst.Fragment; content: string; html: typeof htmlNs }) => void | false,
229+
cb: (file: { ast: SvelteAst.Fragment; content: string; html: typeof htmlNs }) => void | false,
228230
options?: TransformOptions
229-
): (content: string) => string {
231+
): TransformFn {
230232
return (content) => {
231233
const parsed = withParseError(() => parseHtml(content), options);
232234
if (!parsed) return content;
@@ -242,9 +244,7 @@ export const transforms = {
242244
* Unlike other transforms there's no AST here - just string in, string out.
243245
* Return the new content, or `false` to abort (original content is returned unchanged).
244246
*/
245-
text(
246-
cb: (args: { content: string; text: typeof textNs }) => string | false
247-
): (content: string) => string {
247+
text(cb: (file: { content: string; text: typeof textNs }) => string | false): TransformFn {
248248
return (content) => {
249249
const result = cb({ content, text: textNs });
250250
if (result === false) return content;

packages/sv/src/addons/better-auth.ts

Lines changed: 81 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
Walker,
55
color,
66
dedent,
7-
text,
87
transforms,
98
resolveCommand,
10-
createPrinter
9+
createPrinter,
10+
type TransformFn
1111
} from '@sveltejs/sv-utils';
1212
import crypto from 'node:crypto';
1313
import { defineAddon, defineAddonOptions } from '../core/config.ts';
@@ -86,14 +86,8 @@ export default defineAddon({
8686
})
8787
);
8888

89-
sv.file(
90-
'.env',
91-
transforms.text(({ content }) => generateEnvFileContent(content, demoGithub, false))
92-
);
93-
sv.file(
94-
'.env.example',
95-
transforms.text(({ content }) => generateEnvFileContent(content, demoGithub, true))
96-
);
89+
sv.file('.env', generateEnv(demoGithub, false));
90+
sv.file('.env.example', generateEnv(demoGithub, true));
9791

9892
sv.file(
9993
`${directory.lib}/server/auth.${language}`,
@@ -296,7 +290,7 @@ export default defineAddon({
296290

297291
sv.file(
298292
`${directory.kitRoutes}/demo/better-auth/login/+page.server.${language}`,
299-
transforms.text(({ content }) => {
293+
(content) => {
300294
if (content) {
301295
const filePath = `${directory.kitRoutes}/demo/better-auth/login/+page.server.${language}`;
302296
log.warn(`Existing ${color.warning(filePath)} file. Could not update.`);
@@ -397,31 +391,29 @@ export default defineAddon({
397391
export const actions${ts(': Actions')} = {${signInEmailAction}${signInSocialAction}
398392
};
399393
`;
400-
})
394+
}
401395
);
402396

403-
sv.file(
404-
`${directory.kitRoutes}/demo/better-auth/login/+page.svelte`,
405-
transforms.text(({ content }) => {
406-
if (content) {
407-
const filePath = `${directory.kitRoutes}/demo/better-auth/login/+page.svelte`;
408-
log.warn(`Existing ${color.warning(filePath)} file. Could not update.`);
409-
return false;
410-
}
397+
sv.file(`${directory.kitRoutes}/demo/better-auth/login/+page.svelte`, (content) => {
398+
if (content) {
399+
const filePath = `${directory.kitRoutes}/demo/better-auth/login/+page.svelte`;
400+
log.warn(`Existing ${color.warning(filePath)} file. Could not update.`);
401+
return false;
402+
}
411403

412-
const tailwind = dependencyVersion('@tailwindcss/vite') !== undefined;
413-
const input = tailwind
414-
? ' class="mt-1 px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"'
415-
: '';
416-
const btn = tailwind
417-
? ' class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 transition"'
418-
: '';
404+
const tailwind = dependencyVersion('@tailwindcss/vite') !== undefined;
405+
const input = tailwind
406+
? ' class="mt-1 px-3 py-2 bg-white border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"'
407+
: '';
408+
const btn = tailwind
409+
? ' class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 transition"'
410+
: '';
419411

420-
const svelte5 = !!dependencyVersion('svelte')?.startsWith('5');
421-
const [ts, s5] = createPrinter(language === 'ts', svelte5);
412+
const svelte5 = !!dependencyVersion('svelte')?.startsWith('5');
413+
const [ts, s5] = createPrinter(language === 'ts', svelte5);
422414

423-
const passwordForm = demoPassword
424-
? `
415+
const passwordForm = demoPassword
416+
? `
425417
<form method="post" action="?/signInEmail" use:enhance>
426418
<label>
427419
Email
@@ -439,23 +431,23 @@ export default defineAddon({
439431
<button formaction="?/signUpEmail"${btn}>Register</button>
440432
</form>
441433
${tailwind ? `<p class="text-red-500">{form?.message ?? ''}</p>` : `<p style="color: red">{form?.message ?? ''}</p>`}`
442-
: '';
434+
: '';
443435

444-
const separator =
445-
demoPassword && demoGithub
446-
? `\n\n\t\t\t\t\t<hr ${tailwind ? 'class="my-4"' : ''} />\n`
447-
: '';
436+
const separator =
437+
demoPassword && demoGithub
438+
? `\n\n\t\t\t\t\t<hr ${tailwind ? 'class="my-4"' : ''} />\n`
439+
: '';
448440

449-
const githubForm = demoGithub
450-
? `
441+
const githubForm = demoGithub
442+
? `
451443
<form method="post" action="?/signInSocial" use:enhance>
452444
<input type="hidden" name="provider" value="github" />
453445
<input type="hidden" name="callbackURL" value="/demo/better-auth" />
454446
<button${btn}>Sign in with GitHub</button>
455447
</form>`
456-
: '';
448+
: '';
457449

458-
return dedent`
450+
return dedent`
459451
<script ${ts("lang='ts'")}>
460452
import { enhance } from '$app/forms';
461453
${ts("import type { ActionData } from './$types';\n")}
@@ -464,21 +456,18 @@ export default defineAddon({
464456
465457
<h1>Login</h1>${passwordForm}${separator}${githubForm}
466458
`;
467-
})
468-
);
459+
});
469460

470-
sv.file(
471-
`${directory.kitRoutes}/demo/better-auth/+page.server.${language}`,
472-
transforms.text(({ content }) => {
473-
if (content) {
474-
const filePath = `${directory.kitRoutes}/demo/better-auth/+page.server.${language}`;
475-
log.warn(`Existing ${color.warning(filePath)} file. Could not update.`);
476-
return false;
477-
}
461+
sv.file(`${directory.kitRoutes}/demo/better-auth/+page.server.${language}`, (content) => {
462+
if (content) {
463+
const filePath = `${directory.kitRoutes}/demo/better-auth/+page.server.${language}`;
464+
log.warn(`Existing ${color.warning(filePath)} file. Could not update.`);
465+
return false;
466+
}
478467

479-
const [ts] = createPrinter(language === 'ts');
480-
const d1AuthLine = d1 ? '\n\t\t\t\t\t\t\tconst { auth } = event.locals;\n' : '';
481-
return dedent`
468+
const [ts] = createPrinter(language === 'ts');
469+
const d1AuthLine = d1 ? '\n\t\t\t\t\t\t\tconst { auth } = event.locals;\n' : '';
470+
return dedent`
482471
import { redirect } from '@sveltejs/kit';
483472
${ts("import type { Actions } from './$types';")}
484473
${ts("import type { PageServerLoad } from './$types';")}
@@ -500,25 +489,22 @@ export default defineAddon({
500489
}
501490
};
502491
`;
503-
})
504-
);
492+
});
505493

506-
sv.file(
507-
`${directory.kitRoutes}/demo/better-auth/+page.svelte`,
508-
transforms.text(({ content }) => {
509-
if (content) {
510-
const filePath = `${directory.kitRoutes}/demo/better-auth/+page.svelte`;
511-
log.warn(`Existing ${color.warning(filePath)} file. Could not update.`);
512-
return false;
513-
}
494+
sv.file(`${directory.kitRoutes}/demo/better-auth/+page.svelte`, (content) => {
495+
if (content) {
496+
const filePath = `${directory.kitRoutes}/demo/better-auth/+page.svelte`;
497+
log.warn(`Existing ${color.warning(filePath)} file. Could not update.`);
498+
return false;
499+
}
514500

515-
const tailwind = dependencyVersion('@tailwindcss/vite') !== undefined;
516-
const twBtnClasses =
517-
'class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 transition"';
501+
const tailwind = dependencyVersion('@tailwindcss/vite') !== undefined;
502+
const twBtnClasses =
503+
'class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 transition"';
518504

519-
const svelte5 = !!dependencyVersion('svelte')?.startsWith('5');
520-
const [ts, s5] = createPrinter(language === 'ts', svelte5);
521-
return dedent`
505+
const svelte5 = !!dependencyVersion('svelte')?.startsWith('5');
506+
const [ts, s5] = createPrinter(language === 'ts', svelte5);
507+
return dedent`
522508
<script ${ts("lang='ts'")}>
523509
import { enhance } from '$app/forms';
524510
${ts("import type { PageServerData } from './$types';\n")}
@@ -531,8 +517,7 @@ export default defineAddon({
531517
<button ${tailwind ? twBtnClasses : ''}>Sign out</button>
532518
</form>
533519
`;
534-
})
535-
);
520+
});
536521
}
537522
},
538523

@@ -558,29 +543,30 @@ export default defineAddon({
558543
return steps;
559544
}
560545
});
561-
562-
function generateEnvFileContent(content: string, demoGithub: boolean, isExample: boolean) {
563-
content = text.upsert(content, 'ORIGIN', {
564-
value: isExample ? `""` : `"http://localhost:5173"`,
565-
separator: true
566-
});
567-
content = text.upsert(content, 'BETTER_AUTH_SECRET', {
568-
value: isExample ? `""` : `"${crypto.randomUUID()}"`,
569-
comment: [
570-
'Better Auth',
571-
'For production use 32 characters and generated with high entropy',
572-
'https://www.better-auth.com/docs/installation'
573-
],
574-
separator: true
575-
});
576-
577-
if (demoGithub) {
578-
content = text.upsert(content, 'GITHUB_CLIENT_ID', {
579-
value: `""`,
580-
comment: 'GitHub OAuth\n# https://www.better-auth.com/docs/authentication/github'
546+
type GenerateEnv = (demoGithub: boolean, isExample: boolean) => TransformFn;
547+
const generateEnv: GenerateEnv = (demoGithub, isExample) =>
548+
transforms.text(({ content, text }) => {
549+
content = text.upsert(content, 'ORIGIN', {
550+
value: isExample ? `""` : `"http://localhost:5173"`,
551+
separator: true
552+
});
553+
content = text.upsert(content, 'BETTER_AUTH_SECRET', {
554+
value: isExample ? `""` : `"${crypto.randomUUID()}"`,
555+
comment: [
556+
'Better Auth',
557+
'For production use 32 characters and generated with high entropy',
558+
'https://www.better-auth.com/docs/installation'
559+
],
560+
separator: true
581561
});
582-
content = text.upsert(content, 'GITHUB_CLIENT_SECRET', { value: `""` });
583-
}
584562

585-
return content;
586-
}
563+
if (demoGithub) {
564+
content = text.upsert(content, 'GITHUB_CLIENT_ID', {
565+
value: `""`,
566+
comment: 'GitHub OAuth\n# https://www.better-auth.com/docs/authentication/github'
567+
});
568+
content = text.upsert(content, 'GITHUB_CLIENT_SECRET', { value: `""` });
569+
}
570+
571+
return content;
572+
});

0 commit comments

Comments
 (0)