-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: use @clack/prompts #9219
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
Merged
Merged
feat: use @clack/prompts #9219
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
'create-svelte': major | ||
--- | ||
|
||
feat: use `@clack/prompts` |
This file contains hidden or 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,187 +1,167 @@ | ||
#!/usr/bin/env node | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { bold, cyan, gray, green, yellow } from 'kleur/colors'; | ||
import prompts from 'prompts'; | ||
import * as p from '@clack/prompts'; | ||
import { bold, cyan, grey, yellow } from 'kleur/colors'; | ||
import { create } from './index.js'; | ||
import { dist } from './utils.js'; | ||
|
||
// prettier-ignore | ||
const disclaimer = ` | ||
${bold(cyan('Welcome to SvelteKit!'))} | ||
`; | ||
|
||
const { version } = JSON.parse(fs.readFileSync(new URL('package.json', import.meta.url), 'utf-8')); | ||
let cwd = process.argv[2] || '.'; | ||
|
||
async function main() { | ||
console.log(gray(`\ncreate-svelte version ${version}`)); | ||
console.log(disclaimer); | ||
console.log(` | ||
${grey(`create-svelte version ${version}`)} | ||
`); | ||
|
||
let cwd = process.argv[2] || '.'; | ||
p.intro('Welcome to SvelteKit!'); | ||
|
||
if (cwd === '.') { | ||
const opts = await prompts([ | ||
{ | ||
type: 'text', | ||
name: 'dir', | ||
message: 'Where should we create your project?\n (leave blank to use current directory)' | ||
} | ||
]); | ||
if (cwd === '.') { | ||
const dir = await p.text({ | ||
message: 'Where should we create your project?', | ||
placeholder: ' (hit Enter to use current directory)' | ||
}); | ||
|
||
if (opts.dir) { | ||
cwd = opts.dir; | ||
} | ||
} | ||
if (p.isCancel(dir)) process.exit(1); | ||
|
||
if (fs.existsSync(cwd)) { | ||
if (fs.readdirSync(cwd).length > 0) { | ||
const response = await prompts({ | ||
type: 'confirm', | ||
name: 'value', | ||
message: 'Directory not empty. Continue?', | ||
initial: false | ||
}); | ||
|
||
if (!response.value) { | ||
process.exit(1); | ||
} | ||
} | ||
if (dir) { | ||
cwd = /** @type {string} */ (dir); | ||
} | ||
} | ||
|
||
const options = /** @type {import('./types/internal').Options} */ ( | ||
await prompts( | ||
[ | ||
{ | ||
type: 'select', | ||
name: 'template', | ||
message: 'Which Svelte app template?', | ||
choices: fs.readdirSync(dist('templates')).map((dir) => { | ||
const meta_file = dist(`templates/${dir}/meta.json`); | ||
const { title, description } = JSON.parse(fs.readFileSync(meta_file, 'utf8')); | ||
|
||
return { | ||
title, | ||
description, | ||
value: dir | ||
}; | ||
}) | ||
}, | ||
{ | ||
type: 'select', | ||
name: 'types', | ||
message: 'Add type checking with TypeScript?', | ||
initial: false, | ||
choices: [ | ||
{ | ||
title: 'Yes, using JavaScript with JSDoc comments', | ||
value: 'checkjs' | ||
}, | ||
{ | ||
title: 'Yes, using TypeScript syntax', | ||
value: 'typescript' | ||
}, | ||
{ title: 'No', value: null } | ||
] | ||
}, | ||
{ | ||
type: 'toggle', | ||
name: 'eslint', | ||
message: 'Add ESLint for code linting?', | ||
initial: false, | ||
active: 'Yes', | ||
inactive: 'No' | ||
}, | ||
{ | ||
type: 'toggle', | ||
name: 'prettier', | ||
message: 'Add Prettier for code formatting?', | ||
initial: false, | ||
active: 'Yes', | ||
inactive: 'No' | ||
}, | ||
{ | ||
type: 'toggle', | ||
name: 'playwright', | ||
message: 'Add Playwright for browser testing?', | ||
initial: false, | ||
active: 'Yes', | ||
inactive: 'No' | ||
}, | ||
{ | ||
type: 'toggle', | ||
name: 'vitest', | ||
message: 'Add Vitest for unit testing?', | ||
initial: false, | ||
active: 'Yes', | ||
inactive: 'No' | ||
} | ||
], | ||
{ | ||
onCancel: () => { | ||
process.exit(1); | ||
} | ||
} | ||
) | ||
); | ||
|
||
options.name = path.basename(path.resolve(cwd)); | ||
|
||
await create(cwd, options); | ||
|
||
console.log(bold(green('\nYour project is ready!'))); | ||
|
||
if (options.types === 'typescript') { | ||
console.log(bold('✔ Typescript')); | ||
console.log(' Inside Svelte components, use <script lang="ts">'); | ||
} else if (options.types === 'checkjs') { | ||
console.log(bold('✔ Type-checked JavaScript')); | ||
console.log(' https://www.typescriptlang.org/tsconfig#checkJs'); | ||
} else if (options.template === 'skeletonlib') { | ||
const warning = yellow('▲'); | ||
console.log( | ||
`${warning} You chose to not add type checking, but TypeScript will still be installed in order to generate type definitions when building the library` | ||
); | ||
} | ||
if (fs.existsSync(cwd)) { | ||
if (fs.readdirSync(cwd).length > 0) { | ||
const force = await p.confirm({ | ||
message: 'Directory not empty. Continue?', | ||
initialValue: false | ||
}); | ||
|
||
if (options.eslint) { | ||
console.log(bold('✔ ESLint')); | ||
console.log(cyan(' https://github.com/sveltejs/eslint-plugin-svelte3')); | ||
if (force !== true) { | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
if (options.prettier) { | ||
console.log(bold('✔ Prettier')); | ||
console.log(cyan(' https://prettier.io/docs/en/options.html')); | ||
console.log(cyan(' https://github.com/sveltejs/prettier-plugin-svelte#options')); | ||
} | ||
const options = await p.group( | ||
{ | ||
template: () => | ||
p.select({ | ||
message: 'Which Svelte app template?', | ||
options: fs.readdirSync(dist('templates')).map((dir) => { | ||
const meta_file = dist(`templates/${dir}/meta.json`); | ||
const { title, description } = JSON.parse(fs.readFileSync(meta_file, 'utf8')); | ||
|
||
return { | ||
label: title, | ||
hint: description, | ||
value: dir | ||
}; | ||
}) | ||
}), | ||
|
||
types: () => | ||
p.select({ | ||
message: 'Add type checking with TypeScript?', | ||
options: [ | ||
{ | ||
label: 'Yes, using JavaScript with JSDoc comments', | ||
value: 'checkjs' | ||
}, | ||
{ | ||
label: 'Yes, using TypeScript syntax', | ||
value: 'typescript' | ||
}, | ||
{ label: 'No', value: null } | ||
] | ||
}), | ||
|
||
features: () => | ||
p.multiselect({ | ||
message: 'Select additional options', | ||
required: false, | ||
options: [ | ||
{ | ||
value: 'eslint', | ||
label: 'Add ESLint for code linting' | ||
}, | ||
{ | ||
value: 'prettier', | ||
label: 'Add Prettier for code formatting' | ||
}, | ||
{ | ||
value: 'playwright', | ||
label: 'Add Playwright for browser testing' | ||
}, | ||
{ | ||
value: 'vitest', | ||
label: 'Add Vitest for unit testing' | ||
} | ||
] | ||
}) | ||
}, | ||
{ onCancel: () => process.exit(1) } | ||
); | ||
|
||
await create(cwd, { | ||
name: path.basename(path.resolve(cwd)), | ||
template: /** @type {'default' | 'skeleton' | 'skeletonlib'} */ (options.template), | ||
types: options.types, | ||
prettier: options.features.includes('prettier'), | ||
eslint: options.features.includes('eslint'), | ||
playwright: options.features.includes('playwright'), | ||
vitest: options.features.includes('vitest') | ||
}); | ||
|
||
p.outro(`Your project is ready!`); | ||
|
||
if (options.types === 'typescript') { | ||
console.log(bold('✔ Typescript')); | ||
console.log(' Inside Svelte components, use <script lang="ts">\n'); | ||
} else if (options.types === 'checkjs') { | ||
console.log(bold('✔ Type-checked JavaScript')); | ||
console.log(cyan(' https://www.typescriptlang.org/tsconfig#checkJs\n')); | ||
} else if (options.template === 'skeletonlib') { | ||
const warning = yellow('▲'); | ||
console.log( | ||
`${warning} You chose to not add type checking, but TypeScript will still be installed in order to generate type definitions when building the library\n` | ||
); | ||
} | ||
|
||
if (options.playwright) { | ||
console.log(bold('✔ Playwright')); | ||
console.log(cyan(' https://playwright.dev')); | ||
} | ||
if (options.features.includes('eslint')) { | ||
console.log(bold('✔ ESLint')); | ||
console.log(cyan(' https://github.com/sveltejs/eslint-plugin-svelte3\n')); | ||
} | ||
|
||
if (options.vitest) { | ||
console.log(bold('✔ Vitest')); | ||
console.log(cyan(' https://vitest.dev')); | ||
} | ||
if (options.features.includes('prettier')) { | ||
console.log(bold('✔ Prettier')); | ||
console.log(cyan(' https://prettier.io/docs/en/options.html')); | ||
console.log(cyan(' https://github.com/sveltejs/prettier-plugin-svelte#options\n')); | ||
} | ||
|
||
console.log('\nInstall community-maintained integrations:'); | ||
console.log(cyan(' https://github.com/svelte-add/svelte-add')); | ||
if (options.features.includes('playwright')) { | ||
console.log(bold('✔ Playwright')); | ||
console.log(cyan(' https://playwright.dev\n')); | ||
} | ||
|
||
console.log('\nNext steps:'); | ||
let i = 1; | ||
if (options.features.includes('vitest')) { | ||
console.log(bold('✔ Vitest')); | ||
console.log(cyan(' https://vitest.dev\n')); | ||
} | ||
|
||
const relative = path.relative(process.cwd(), cwd); | ||
if (relative !== '') { | ||
console.log(` ${i++}: ${bold(cyan(`cd ${relative}`))}`); | ||
} | ||
console.log('Install community-maintained integrations:'); | ||
console.log(cyan(' https://github.com/svelte-add/svelte-add')); | ||
|
||
console.log(` ${i++}: ${bold(cyan('npm install'))} (or pnpm install, etc)`); | ||
// prettier-ignore | ||
console.log(` ${i++}: ${bold(cyan('git init && git add -A && git commit -m "Initial commit"'))} (optional)`); | ||
console.log(` ${i++}: ${bold(cyan('npm run dev -- --open'))}`); | ||
console.log('\nNext steps:'); | ||
let i = 1; | ||
|
||
console.log(`\nTo close the dev server, hit ${bold(cyan('Ctrl-C'))}`); | ||
console.log(`\nStuck? Visit us at ${cyan('https://svelte.dev/chat')}`); | ||
const relative = path.relative(process.cwd(), cwd); | ||
if (relative !== '') { | ||
console.log(` ${i++}: ${bold(cyan(`cd ${relative}`))}`); | ||
} | ||
|
||
main(); | ||
console.log(` ${i++}: ${bold(cyan('npm install'))} (or pnpm install, etc)`); | ||
// prettier-ignore | ||
console.log(` ${i++}: ${bold(cyan('git init && git add -A && git commit -m "Initial commit"'))} (optional)`); | ||
console.log(` ${i++}: ${bold(cyan('npm run dev -- --open'))}`); | ||
|
||
console.log(`\nTo close the dev server, hit ${bold(cyan('Ctrl-C'))}`); | ||
console.log(`\nStuck? Visit us at ${cyan('https://svelte.dev/chat')}`); |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.