Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] create-svelte: Add prompt for choosing adapter #2479

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/wicked-colts-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Add option to select adapter
27 changes: 27 additions & 0 deletions packages/create-svelte/adapters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default [
{
name: 'adapter-node',
npm: '@sveltejs/adapter-node',
url: 'https://github.com/sveltejs/kit/tree/master/packages/adapter-node'
},
{
name: 'adapter-static',
npm: '@sveltejs/adapter-static',
url: 'https://github.com/sveltejs/kit/tree/master/packages/adapter-static'
},
{
name: 'adapter-cloudflare-workers',
npm: '@sveltejs/adapter-cloudflare-workers',
url: 'https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare-workers'
},
{
name: 'adapter-netlify',
npm: '@sveltejs/adapter-netlify',
url: 'https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify'
},
{
name: 'adapter-vercel',
npm: '@sveltejs/adapter-vercel',
url: 'https://github.com/sveltejs/kit/tree/master/packages/adapter-vercel'
}
];
32 changes: 32 additions & 0 deletions packages/create-svelte/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from 'url';
import { bold, cyan, gray, green, red } from 'kleur/colors';
import prompts from 'prompts';
import { mkdirp, copy } from './utils.js';
import adapters from './adapters.js';

// prettier-ignore
const disclaimer = `
Expand Down Expand Up @@ -79,6 +80,20 @@ async function main() {
initial: false,
active: 'Yes',
inactive: 'No'
},
{
type: 'select',
name: 'adapter',
message: 'Add an adapter',
choices: [
{
title: "I'll add it later or use a community-provided adapter",
description:
'Community provided adapters: https://sveltesociety.dev/components#adapters',
value: null
},
...adapters.map((adapter) => ({ title: adapter.name, value: adapter }))
]
}
])
);
Expand Down Expand Up @@ -124,6 +139,12 @@ async function main() {
);
}

if (options.adapter) {
console.log(
bold(green(`✔ Added ${options.adapter.name}.\n` + `Documentation: ${options.adapter.url}`))
);
}

console.log(
'\nWant to add other parts to your code base? ' +
'Visit https://github.com/svelte-add/svelte-adders, a community project of commands ' +
Expand Down Expand Up @@ -196,12 +217,23 @@ function write_common_files(cwd, options, name) {
const new_pkg = JSON.parse(file.contents);
merge(pkg, new_pkg);
} else {
if (file.name === 'svelte.config.js') {
file.contents = file.contents
.replace(
'// insert:adapter-import\n',
options.adapter ? `import adapter from '${options.adapter.npm}';\n` : ''
)
.replace(/\t+\/\/ insert:adapter/, options.adapter ? '\t\tadapter: adapter(),' : '');
}
const dest = path.join(cwd, file.name);
mkdirp(path.dirname(dest));
fs.writeFileSync(dest, file.contents);
}
});

if (options.adapter) {
pkg.devDependencies[options.adapter.npm] = '^1.0.0-next.1';
}
pkg.dependencies = sort_keys(pkg.dependencies);
pkg.devDependencies = sort_keys(pkg.devDependencies);
pkg.name = toValidPackageName(name);
Expand Down
2 changes: 2 additions & 0 deletions packages/create-svelte/shared/+typescript/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import preprocess from 'svelte-preprocess';
// insert:adapter-import

/** @type {import('@sveltejs/kit').Config} */
const config = {
Expand All @@ -7,6 +8,7 @@ const config = {
preprocess: preprocess(),

kit: {
// insert:adapter
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
}
Expand Down
2 changes: 2 additions & 0 deletions packages/create-svelte/shared/-typescript/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// insert:adapter-import
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// insert:adapter
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
}
Expand Down
1 change: 1 addition & 0 deletions packages/create-svelte/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type Options = {
typescript: boolean;
prettier: boolean;
eslint: boolean;
adapter: { name: string; npm: string; url: string };
};

export type File = {
Expand Down