Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion community-addon-template/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default defineAddon({
sv.file('src/DemoComponent.svelte', (content) => {
if (!options.demo) return content;
const { script, generateCode } = parseSvelte(content, { typescript });
imports.addDefault(script.ast, '../addon-template-demo.txt?raw', 'demo');
imports.addDefault(script.ast, { from: '../addon-template-demo.txt?raw', as: 'demo' });
return generateCode({ script: script.generateCode(), template: '{demo}' });
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/addons/_tests/eslint/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test.concurrent.for(variants)('core - %s', async (variant, { page, ...ctx }) =>

expect(() => execSync('pnpm lint', { cwd, stdio: 'pipe' })).toThrowError();

expect(() => execSync('pnpm eslint --fix .', { cwd, stdio: 'pipe' })).not.toThrowError();
expect(() => execSync('pnpm eslint --fix .', { cwd, stdio: 'inherit' })).not.toThrowError();

expect(() => execSync('pnpm lint', { cwd, stdio: 'pipe' })).not.toThrowError();
});
6 changes: 3 additions & 3 deletions packages/addons/_tests/mdsvex/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ function addFixture(cwd: string, variant: string) {

const src = fs.readFileSync(page, 'utf8');
const { script, template, generateCode } = parseSvelte(src);
imports.addDefault(script.ast, './Demo.svx', 'Demo');
imports.addDefault(script.ast, { from: './Demo.svx', as: 'Demo' });

const div = html.div({ class: 'mdsvex' });
const div = html.createDiv({ class: 'mdsvex' });
html.appendElement(template.ast.childNodes, div);
const mdsvexNode = html.element('Demo');
const mdsvexNode = html.createElement('Demo');
html.appendElement(div.childNodes, mdsvexNode);

const content = generateCode({
Expand Down
18 changes: 9 additions & 9 deletions packages/addons/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ export function addEslintConfigPrettier(content: string): string {
}

svelteImportName ??= 'svelte';
imports.addDefault(ast, 'eslint-plugin-svelte', svelteImportName);
imports.addDefault(ast, 'eslint-config-prettier', 'prettier');
imports.addDefault(ast, { from: 'eslint-plugin-svelte', as: svelteImportName });
imports.addDefault(ast, { from: 'eslint-config-prettier', as: 'prettier' });

const fallbackConfig = common.expressionFromString('[]');
const defaultExport = exports.defaultExport(ast, fallbackConfig);
const fallbackConfig = common.parseExpression('[]');
const defaultExport = exports.createDefault(ast, { fallback: fallbackConfig });
const eslintConfig = defaultExport.value;
if (eslintConfig.type !== 'ArrayExpression' && eslintConfig.type !== 'CallExpression')
return content;

const prettier = common.expressionFromString('prettier');
const sveltePrettierConfig = common.expressionFromString(`${svelteImportName}.configs.prettier`);
const configSpread = common.createSpreadElement(sveltePrettierConfig);
const prettier = common.parseExpression('prettier');
const sveltePrettierConfig = common.parseExpression(`${svelteImportName}.configs.prettier`);
const configSpread = common.createSpread(sveltePrettierConfig);

const nodesToInsert = [];
if (!common.hasNode(eslintConfig, prettier)) nodesToInsert.push(prettier);
if (!common.hasNode(eslintConfig, configSpread)) nodesToInsert.push(configSpread);
if (!common.contains(eslintConfig, prettier)) nodesToInsert.push(prettier);
if (!common.contains(eslintConfig, configSpread)) nodesToInsert.push(configSpread);

const elements =
eslintConfig.type === 'ArrayExpression' ? eslintConfig.elements : eslintConfig.arguments;
Expand Down
19 changes: 13 additions & 6 deletions packages/addons/devtools-json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ export default defineAddon({
const { ast, generateCode } = parseScript(content);

const vitePluginName = 'devtoolsJson';
imports.addDefault(ast, 'vite-plugin-devtools-json', vitePluginName);
imports.addDefault(ast, { from: 'vite-plugin-devtools-json', as: vitePluginName });

const { value: rootObject } = exports.defaultExport(ast, functions.call('defineConfig', []));
const param1 = functions.argumentByIndex(rootObject, 0, object.createEmpty());
const { value: rootObject } = exports.createDefault(ast, {
fallback: functions.createCall({ name: 'defineConfig', args: [] })
});

const pluginsArray = object.property(param1, 'plugins', array.createEmpty());
const pluginFunctionCall = functions.call(vitePluginName, []);
array.push(pluginsArray, pluginFunctionCall);
const param1 = functions.getArgument(rootObject, {
index: 0,
fallback: object.create({})
});

const pluginsArray = object.property(param1, { name: 'plugins', fallback: array.create() });
const pluginFunctionCall = functions.createCall({ name: vitePluginName, args: [] });

array.append(pluginsArray, pluginFunctionCall);

return generateCode();
});
Expand Down
168 changes: 114 additions & 54 deletions packages/addons/drizzle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,16 @@ export default defineAddon({
sv.file(`drizzle.config.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);

imports.addNamed(ast, 'drizzle-kit', { defineConfig: 'defineConfig' });
imports.addNamed(ast, { from: 'drizzle-kit', imports: { defineConfig: 'defineConfig' } });

common.addStatement(
ast,
common.statementFromString(
ast.body.push(
common.parseStatement(
"if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');"
)
);

exports.defaultExport(
ast,
common.expressionFromString(`
exports.createDefault(ast, {
fallback: common.parseExpression(`
defineConfig({
schema: "./src/lib/server/db/schema.${typescript ? 'ts' : 'js'}",
dialect: "${options.sqlite === 'turso' ? 'turso' : options.database}",
Expand All @@ -217,7 +215,7 @@ export default defineAddon({
verbose: true,
strict: true
})`)
);
});

return generateCode();
});
Expand All @@ -227,128 +225,190 @@ export default defineAddon({

let userSchemaExpression;
if (options.database === 'sqlite') {
imports.addNamed(ast, 'drizzle-orm/sqlite-core', {
sqliteTable: 'sqliteTable',
integer: 'integer'
imports.addNamed(ast, {
from: 'drizzle-orm/sqlite-core',
imports: {
sqliteTable: 'sqliteTable',
integer: 'integer'
}
});

userSchemaExpression = common.expressionFromString(`sqliteTable('user', {
userSchemaExpression = common.parseExpression(`sqliteTable('user', {
id: integer('id').primaryKey(),
age: integer('age')
})`);
}
if (options.database === 'mysql') {
imports.addNamed(ast, 'drizzle-orm/mysql-core', {
mysqlTable: 'mysqlTable',
serial: 'serial',
int: 'int'
imports.addNamed(ast, {
from: 'drizzle-orm/mysql-core',
imports: {
mysqlTable: 'mysqlTable',
serial: 'serial',
int: 'int'
}
});

userSchemaExpression = common.expressionFromString(`mysqlTable('user', {
userSchemaExpression = common.parseExpression(`mysqlTable('user', {
id: serial('id').primaryKey(),
age: int('age'),
})`);
}
if (options.database === 'postgresql') {
imports.addNamed(ast, 'drizzle-orm/pg-core', {
pgTable: 'pgTable',
serial: 'serial',
integer: 'integer'
imports.addNamed(ast, {
from: 'drizzle-orm/pg-core',
imports: {
pgTable: 'pgTable',
serial: 'serial',
integer: 'integer'
}
});

userSchemaExpression = common.expressionFromString(`pgTable('user', {
userSchemaExpression = common.parseExpression(`pgTable('user', {
id: serial('id').primaryKey(),
age: integer('age'),
})`);
}

if (!userSchemaExpression) throw new Error('unreachable state...');
const userIdentifier = variables.declaration(ast, 'const', 'user', userSchemaExpression);
exports.namedExport(ast, 'user', userIdentifier);
const userIdentifier = variables.declaration(ast, {
kind: 'const',
name: 'user',
value: userSchemaExpression
});
exports.createNamed(ast, {
name: 'user',
fallback: userIdentifier
});

return generateCode();
});

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

imports.addNamed(ast, '$env/dynamic/private', { env: 'env' });
imports.addNamespace(ast, './schema', 'schema');
imports.addNamed(ast, {
from: '$env/dynamic/private',
imports: { env: 'env' }
});
imports.addNamespace(ast, { from: './schema', as: 'schema' });

// env var checks
const dbURLCheck = common.statementFromString(
const dbURLCheck = common.parseStatement(
"if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set');"
);
common.addStatement(ast, dbURLCheck);
ast.body.push(dbURLCheck);

let clientExpression;
// SQLite
if (options.sqlite === 'better-sqlite3') {
imports.addDefault(ast, 'better-sqlite3', 'Database');
imports.addNamed(ast, 'drizzle-orm/better-sqlite3', { drizzle: 'drizzle' });
imports.addDefault(ast, { from: 'better-sqlite3', as: 'Database' });
imports.addNamed(ast, {
from: 'drizzle-orm/better-sqlite3',
imports: { drizzle: 'drizzle' }
});

clientExpression = common.expressionFromString('new Database(env.DATABASE_URL)');
clientExpression = common.parseExpression('new Database(env.DATABASE_URL)');
}
if (options.sqlite === 'libsql' || options.sqlite === 'turso') {
imports.addNamed(ast, '@libsql/client', { createClient: 'createClient' });
imports.addNamed(ast, 'drizzle-orm/libsql', { drizzle: 'drizzle' });
imports.addNamed(ast, {
from: '@libsql/client',
imports: { createClient: 'createClient' }
});
imports.addNamed(ast, {
from: 'drizzle-orm/libsql',
imports: { drizzle: 'drizzle' }
});

if (options.sqlite === 'turso') {
imports.addNamed(ast, '$app/environment', { dev: 'dev' });
imports.addNamed(ast, {
from: '$app/environment',
imports: { dev: 'dev' }
});
// auth token check in prod
const authTokenCheck = common.statementFromString(
const authTokenCheck = common.parseStatement(
"if (!dev && !env.DATABASE_AUTH_TOKEN) throw new Error('DATABASE_AUTH_TOKEN is not set');"
);
common.addStatement(ast, authTokenCheck);
ast.body.push(authTokenCheck);

clientExpression = common.expressionFromString(
clientExpression = common.parseExpression(
'createClient({ url: env.DATABASE_URL, authToken: env.DATABASE_AUTH_TOKEN })'
);
} else {
clientExpression = common.expressionFromString('createClient({ url: env.DATABASE_URL })');
clientExpression = common.parseExpression('createClient({ url: env.DATABASE_URL })');
}
}
// MySQL
if (options.mysql === 'mysql2' || options.mysql === 'planetscale') {
imports.addDefault(ast, 'mysql2/promise', 'mysql');
imports.addNamed(ast, 'drizzle-orm/mysql2', { drizzle: 'drizzle' });
imports.addDefault(ast, { from: 'mysql2/promise', as: 'mysql' });
imports.addNamed(ast, {
from: 'drizzle-orm/mysql2',
imports: { drizzle: 'drizzle' }
});

clientExpression = common.expressionFromString('mysql.createPool(env.DATABASE_URL)');
clientExpression = common.parseExpression('mysql.createPool(env.DATABASE_URL)');
}
// PostgreSQL
if (options.postgresql === 'neon') {
imports.addNamed(ast, '@neondatabase/serverless', { neon: 'neon' });
imports.addNamed(ast, 'drizzle-orm/neon-http', { drizzle: 'drizzle' });
imports.addNamed(ast, {
from: '@neondatabase/serverless',
imports: { neon: 'neon' }
});
imports.addNamed(ast, {
from: 'drizzle-orm/neon-http',
imports: { drizzle: 'drizzle' }
});

clientExpression = common.expressionFromString('neon(env.DATABASE_URL)');
clientExpression = common.parseExpression('neon(env.DATABASE_URL)');
}
if (options.postgresql === 'postgres.js') {
imports.addDefault(ast, 'postgres', 'postgres');
imports.addNamed(ast, 'drizzle-orm/postgres-js', { drizzle: 'drizzle' });
imports.addDefault(ast, { from: 'postgres', as: 'postgres' });
imports.addNamed(ast, {
from: 'drizzle-orm/postgres-js',
imports: { drizzle: 'drizzle' }
});

clientExpression = common.expressionFromString('postgres(env.DATABASE_URL)');
clientExpression = common.parseExpression('postgres(env.DATABASE_URL)');
}

if (!clientExpression) throw new Error('unreachable state...');
const clientIdentifier = variables.declaration(ast, 'const', 'client', clientExpression);
common.addStatement(ast, clientIdentifier);
ast.body.push(
variables.declaration(ast, {
kind: 'const',
name: 'client',
value: clientExpression
})
);

// create drizzle function call
const drizzleCall = functions.callByIdentifier('drizzle', ['client']);
const drizzleCall = functions.createCall({
name: 'drizzle',
args: ['client'],
useIdentifiers: true
});

// add schema to support `db.query`
const paramObject = object.create({
schema: variables.identifier('schema')
schema: variables.createIdentifier('schema')
});
if (options.database === 'mysql') {
const mode = options.mysql === 'planetscale' ? 'planetscale' : 'default';
object.property(paramObject, 'mode', common.createLiteral(mode));
object.property(paramObject, {
name: 'mode',
fallback: common.createLiteral(mode)
});
}
drizzleCall.arguments.push(paramObject);

// create `db` export
const db = variables.declaration(ast, 'const', 'db', drizzleCall);
exports.namedExport(ast, 'db', db);
const db = variables.declaration(ast, {
kind: 'const',
name: 'db',
value: drizzleCall
});
exports.createNamed(ast, {
name: 'db',
fallback: db
});

return generateCode();
});
Expand Down
Loading