Skip to content

Commit

Permalink
feat: create /demo page (#171)
Browse files Browse the repository at this point in the history
* feat: create `/demo` page

* simplify

---------

Co-authored-by: AdrianGonz97 <[email protected]>
  • Loading branch information
manuel3108 and AdrianGonz97 authored Oct 19, 2024
1 parent 8037f7c commit ab39dca
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/blue-pandas-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

feat: create `/demo` page
19 changes: 18 additions & 1 deletion packages/adders/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { imports, exports, common } from '@sveltejs/cli-core/js';
import { type Question, type FileEditor } from '@sveltejs/cli-core';
import { parseScript } from '@sveltejs/cli-core/parsers';
import { parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';

export function addEslintConfigPrettier({ content }: FileEditor<Record<string, Question>>): string {
const { ast, generateCode } = parseScript(content);
Expand Down Expand Up @@ -64,3 +64,20 @@ export function addEslintConfigPrettier({ content }: FileEditor<Record<string, Q

return generateCode();
}

export function addToDemoPage(
{ content }: FileEditor<Record<string, Question>>,
path: string
): string {
const { template, generateCode } = parseSvelte(content);

for (const node of template.ast.childNodes) {
if (node.type === 'tag' && node.attribs['href'] === `/demo/${path}`) {
return content;
}
}

const newLine = template.source ? '\n' : '';
const src = template.source + `${newLine}<a href="/demo/${path}">${path}</a>`;
return generateCode({ template: src });
}
6 changes: 6 additions & 0 deletions packages/adders/lucia/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { common, exports, imports, variables, object, functions, kit } from '@sv
// eslint-disable-next-line no-duplicate-imports
import type { AstTypes } from '@sveltejs/cli-core/js';
import { parseScript } from '@sveltejs/cli-core/parsers';
import { addToDemoPage } from '../common.ts';

const TABLE_TYPE = {
mysql: 'mysqlTable',
Expand Down Expand Up @@ -345,6 +346,11 @@ export default defineAdder({
},
// DEMO
// login/register
{
name: ({ kit }) => `${kit?.routesDirectory}/demo/+page.svelte`,
condition: ({ options }) => options.demo,
content: (editor) => addToDemoPage(editor, 'lucia')
},
{
name: ({ kit, typescript }) =>
`${kit!.routesDirectory}/demo/lucia/login/+page.server.${typescript ? 'ts' : 'js'}`,
Expand Down
6 changes: 6 additions & 0 deletions packages/adders/paraglide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@sveltejs/cli-core/js';
import * as html from '@sveltejs/cli-core/html';
import { parseHtml, parseJson, parseScript, parseSvelte } from '@sveltejs/cli-core/parsers';
import { addToDemoPage } from '../common.ts';

const DEFAULT_INLANG_PROJECT = {
$schema: 'https://inlang.com/schema/project-settings',
Expand Down Expand Up @@ -232,6 +233,11 @@ export default defineAdder({
return generateCode();
}
},
{
name: ({ kit }) => `${kit?.routesDirectory}/demo/+page.svelte`,
condition: ({ options }) => options.demo,
content: (editor) => addToDemoPage(editor, 'paraglide')
},
{
// add usage example
name: ({ kit }) => `${kit?.routesDirectory}/demo/paraglide/+page.svelte`,
Expand Down

0 comments on commit ab39dca

Please sign in to comment.