Skip to content

Commit

Permalink
feat(installer): #32 add support for sveltekit
Browse files Browse the repository at this point in the history
  • Loading branch information
sdorra committed Nov 13, 2024
1 parent 237ff8b commit ff040b7
Show file tree
Hide file tree
Showing 18 changed files with 674 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/installer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"scripts": {
"build": "tsup src/index.ts --format esm --dts -d dist",
"test": "vitest",
"test": "vitest ./src --run",
"gen-content": "tsx ./scripts/gen-content.ts",
"typecheck": "tsc --noEmit"
},
Expand Down
10 changes: 9 additions & 1 deletion packages/installer/src/migration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ import { migratorQwik } from "./qwik.js";
import { migratorRemix } from "./remix.js";
import { migratorSolid } from "./solid.js";
import { migratorVite } from "./vite.js";
import { migratorSvelteKit } from "./sveltekit.js";

const migrators = [migratorNextJS, migratorRemix, migratorQwik, migratorSolid, migratorVite];
const migrators = [
migratorNextJS,
migratorRemix,
migratorQwik,
migratorSolid,
migratorSvelteKit,
migratorVite,
];

export function findMigrator(
packageJson: PackageJson,
Expand Down
163 changes: 163 additions & 0 deletions packages/installer/src/migration/sveltekit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { PackageJson } from "src/packageJson.js";
import { describe, expect, it } from "vitest";
import { migratorSvelteKit } from "./sveltekit.js";

describe("sveltekit migrator", () => {
const packageJson: PackageJson = {
name: "something",
};

describe("isResponsible", () => {
it("should be responsible for sveltekit", () => {
const responsible = migratorSvelteKit.isResponsible({
name: "something",
dependencies: {
"@sveltejs/kit": "^2.5.18",
},
});
expect(responsible).toBe(true);
});

it("should be responsible for sveltekit in dev dependencies", () => {
const responsible = migratorSvelteKit.isResponsible({
name: "something",
devDependencies: {
"@sveltejs/kit": "^2.5.18",
},
});
expect(responsible).toBe(true);
});

it("should not be responsible for next.js", () => {
const responsible = migratorSvelteKit.isResponsible({
name: "something",
dependencies: {
next: "14.3.1",
},
});
expect(responsible).toBe(false);
});
});

describe("options", () => {
it("should parse options", () => {
const options = migratorSvelteKit.options.parse({ demoContent: "none" });
expect(options).toEqual({ demoContent: "none" });
});
});

describe("migration", () => {
it("should return tasks without demo content", async () => {
const migration = await migratorSvelteKit.createMigration(
{
directory: "directory",
packageJson,
},
{
demoContent: "none",
},
);

const names = migration.map((task) => task.name);
expect(names).toEqual([
"Install dependencies",
"Modify svelte-kit configuration",
"Modify vite configuration",
"Add .content-collections to .gitignore",
"Create configuration file",
]);
});

it("should return tasks with demo content", async () => {
const migration = await migratorSvelteKit.createMigration(
{
directory: "directory",
packageJson,
},
{
demoContent: "markdown",
},
);

const names = migration.map((task) => task.name);
expect(names).toEqual([
"Install dependencies",
"Modify svelte-kit configuration",
"Modify vite configuration",
"Add .content-collections to .gitignore",
"Create configuration file",
"Create demo content",
]);
});

it("should add markdown package with markdown demo content", async () => {
const migration = await migratorSvelteKit.createMigration(
{
directory: "directory",
packageJson,
},
{
demoContent: "markdown",
},
);

const addDependenciesTask = migration.find(
(task) => task.name === "Install dependencies",
);
if (!addDependenciesTask) {
throw new Error("Task not found");
}

// @ts-expect-error - we know it's there
const dependencies = addDependenciesTask.devDependencies;
expect(dependencies).toContain("@content-collections/markdown");
});

it("should not add markdown package without demo content", async () => {
const migration = await migratorSvelteKit.createMigration(
{
directory: "directory",
packageJson,
},
{
demoContent: "none",
},
);

const addDependenciesTask = migration.find(
(task) => task.name === "Install dependencies",
);
if (!addDependenciesTask) {
throw new Error("Task not found");
}

// @ts-expect-error - we know it's there
const dependencies = addDependenciesTask.devDependencies;
expect(dependencies).not.toContain("@content-collections/markdown");
});

it("should add core and vite packages", async () => {
const migration = await migratorSvelteKit.createMigration(
{
directory: "directory",
packageJson,
},
{
demoContent: "none",
},
);

const addDependenciesTask = migration.find(
(task) => task.name === "Install dependencies",
);
if (!addDependenciesTask) {
throw new Error("Task not found");
}

// @ts-expect-error - we know it's there
const dependencies = addDependenciesTask.devDependencies;
expect(dependencies).toContain("@content-collections/core");
expect(dependencies).toContain("@content-collections/vite");
});
});
});
42 changes: 42 additions & 0 deletions packages/installer/src/migration/sveltekit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { z } from "zod";
import { defineMigrator } from "./migrator.js";
import { createConfiguration } from "./tasks/config.js";
import { createDemoContent } from "./tasks/demo.js";
import { addDependencies } from "./tasks/dependencies.js";
import { addToGitIgnore } from "./tasks/gitignore.js";
import { modifySvelteKitConfig } from "./tasks/svelteconfig.js";
import { modifyViteConfig } from "./tasks/viteconfig.js";

export const migratorSvelteKit = defineMigrator({
name: "sveltekit",
options: z.object({
demoContent: z
.enum(["none", "markdown"])
.default("markdown")
.describe("Type of demo content"),
}),
isResponsible: (packageJson) =>
Boolean(packageJson.dependencies?.["@sveltejs/kit"]) ||
Boolean(packageJson.devDependencies?.["@sveltejs/kit"]),
async createMigration({ directory, packageJson }, { demoContent }) {
const packages = ["@content-collections/core", "@content-collections/vite"];

if (demoContent === "markdown") {
packages.push("@content-collections/markdown");
}

const tasks = [
addDependencies(directory, packageJson, [], packages),
modifySvelteKitConfig(directory),
modifyViteConfig(directory, "vite"),
addToGitIgnore(directory),
createConfiguration(directory, demoContent),
];

if (demoContent !== "none") {
tasks.push(createDemoContent(directory, demoContent));
}

return tasks;
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const config = {
preprocess: vitePreprocess(),
kit: {
adapter: adapter(),
alias: {
"content-collections": "./.content-collections/generated",
}
}
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import adapter from "@sveltejs/adapter-auto";
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";

export default {
preprocess: vitePreprocess(),
kit: {
adapter: adapter(),
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const otherObject = {
alias: {
"components": "./src/components",
}
};

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://svelte.dev/docs/kit/integrations
// for more information about preprocessors
preprocess: vitePreprocess(),

kit: {
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
adapter: adapter()
}
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://svelte.dev/docs/kit/integrations
// for more information about preprocessors
preprocess: vitePreprocess(),

kit: {
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
adapter: adapter(),
}
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://svelte.dev/docs/kit/integrations
// for more information about preprocessors
preprocess: vitePreprocess(),

kit: {
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
adapter: adapter(),

alias: {
"components": "./src/components",
}
}
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const config = {
preprocess: vitePreprocess(),
kit: {
adapter: adapter(),
alias: {
"content-collections": "/some/other/path",
}
}
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const config = {
preprocess: vitePreprocess(),
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const config = {
preprocess: vitePreprocess(),
kit: []
};

export default config;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const config = {
preprocess: vitePreprocess(),
kit() {
console.log('wrong, wrong, wrong');
}
};

export default config;
2 changes: 1 addition & 1 deletion packages/installer/src/migration/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Result = {
export type Result = {
status: "changed" | "skipped" | "error";
message: string;
}
Expand Down
Loading

0 comments on commit ff040b7

Please sign in to comment.