-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(installer): #32 add support for sveltekit
- Loading branch information
Showing
18 changed files
with
674 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}); |
14 changes: 14 additions & 0 deletions
14
...installer/src/migration/tasks/__tests__/svelte-config/already-configured/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
9 changes: 9 additions & 0 deletions
9
...ages/installer/src/migration/tasks/__tests__/svelte-config/inline-object/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
}; |
24 changes: 24 additions & 0 deletions
24
...ages/installer/src/migration/tasks/__tests__/svelte-config/second-object/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
18 changes: 18 additions & 0 deletions
18
packages/installer/src/migration/tasks/__tests__/svelte-config/simple/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
22 changes: 22 additions & 0 deletions
22
.../installer/src/migration/tasks/__tests__/svelte-config/with-alias-object/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
14 changes: 14 additions & 0 deletions
14
...staller/src/migration/tasks/__tests__/svelte-config/with-different-alias/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
7 changes: 7 additions & 0 deletions
7
...installer/src/migration/tasks/__tests__/svelte-config/without-kit-object/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
8 changes: 8 additions & 0 deletions
8
...r/src/migration/tasks/__tests__/svelte-config/wrong-type-of-kit-property/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
10 changes: 10 additions & 0 deletions
10
.../installer/src/migration/tasks/__tests__/svelte-config/wrong-type-of-kit/svelte.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
Oops, something went wrong.