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

create-svelte: Skip prompts by passing args to CLI #6982

Closed
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/tidy-icons-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

Make it possible to skip some or all prompts by passing arguments directly to the CLI
14 changes: 14 additions & 0 deletions packages/create-svelte/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ npm create svelte@latest

...and follow the prompts.

## Arguments

Some or all of the prompts can be skipped by supplying arguments directly to the CLI:

```bash
npm create svelte@latest my-app \
--name=my-new-app \
--template=default|skeleton|skeletonlib \
--types=checkjs|typescript|none \
--prettier|--no-prettier \
--eslint|--no-eslint \
--playwright|--no-playwright
```

## API

You can also use `create-svelte` programmatically:
Expand Down
35 changes: 19 additions & 16 deletions packages/create-svelte/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { bold, cyan, gray, green, red } from 'kleur/colors';
import prompts from 'prompts';
import { create } from './index.js';
import { dist } from './utils.js';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

// prettier-ignore
const disclaimer = `
Expand All @@ -21,32 +23,33 @@ async function main() {
console.log(gray(`\ncreate-svelte version ${version}`));
console.log(disclaimer);

let cwd = process.argv[2] || '.';

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 (opts.dir) {
cwd = opts.dir;
const args = await yargs(hideBin(process.argv)).coerce('types', (arg) =>
arg === 'none' ? null : arg
).argv;
Comment on lines +26 to +28
Copy link
Author

@JReinhold JReinhold Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes it so that --types=none ends up as { types: null } internally.

Alternatively we could make the arg match one-to-one either by making the arg null instead of "none", or changing the internal null to be "none" everywhere.

prompts.override({
...args,
dir: args._[0]?.toString()
});

const { dir } = await prompts([
{
type: 'text',
name: 'dir',
message: 'Where should we create your project?\n (leave blank to use current directory)'
}
}
]);
const cwd = dir || '.';

if (fs.existsSync(cwd)) {
if (fs.readdirSync(cwd).length > 0) {
const response = await prompts({
type: 'confirm',
name: 'value',
name: 'overwrite',
message: 'Directory not empty. Continue?',
initial: false
});

if (!response.value) {
if (!response.overwrite) {
process.exit(1);
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/create-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
"@types/gitignore-parser": "^0.0.0",
"@types/prettier": "^2.6.3",
"@types/prompts": "^2.0.14",
"@types/yargs": "^17.0.12",
"gitignore-parser": "^0.0.2",
"prettier": "^2.6.2",
"prettier-plugin-svelte": "^2.7.0",
"sucrase": "^3.21.0",
"svelte": "^3.48.0",
"svelte-preprocess": "^4.10.6",
"tiny-glob": "^0.2.9",
"uvu": "^0.5.3"
"uvu": "^0.5.3",
"yargs": "^17.5.1"
},
"scripts": {
"build": "node scripts/build-templates",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.