From c69a6d03d0625c8e50b540caf98eecd9048c7a8a Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 22 Sep 2022 21:59:20 -0400 Subject: [PATCH 1/5] parse args with yargs --- packages/create-svelte/bin.js | 35 +++++++++++++++-------------- packages/create-svelte/package.json | 4 +++- pnpm-lock.yaml | 14 ++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/packages/create-svelte/bin.js b/packages/create-svelte/bin.js index 94c3dc529d9e..a91fdfedcb3d 100755 --- a/packages/create-svelte/bin.js +++ b/packages/create-svelte/bin.js @@ -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 = ` @@ -20,33 +22,32 @@ const { version } = JSON.parse(fs.readFileSync(new URL('package.json', import.me 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)).argv; + prompts.override({ + ...args, + dir: args._[0]?.toString() + }); + console.log(args); + + 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); } } diff --git a/packages/create-svelte/package.json b/packages/create-svelte/package.json index b850fc800cdf..9d04b9ea7192 100644 --- a/packages/create-svelte/package.json +++ b/packages/create-svelte/package.json @@ -20,6 +20,7 @@ "@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", @@ -27,7 +28,8 @@ "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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f1a309a23c29..2f332e0f2500 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -210,6 +210,7 @@ importers: '@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 kleur: ^4.1.4 prettier: ^2.6.2 @@ -220,6 +221,7 @@ importers: svelte-preprocess: ^4.10.6 tiny-glob: ^0.2.9 uvu: ^0.5.3 + yargs: ^17.5.1 dependencies: kleur: 4.1.5 prompts: 2.4.2 @@ -229,6 +231,7 @@ importers: '@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.7.1 prettier-plugin-svelte: 2.7.0_nakrehnrzdf7fdea5k3a4dfy4m @@ -237,6 +240,7 @@ importers: svelte-preprocess: 4.10.7_svelte@3.48.0 tiny-glob: 0.2.9 uvu: 0.5.4 + yargs: 17.5.1 packages/create-svelte/templates/default: specifiers: @@ -1277,6 +1281,16 @@ packages: '@types/node': 16.11.42 dev: true + /@types/yargs-parser/21.0.0: + resolution: {integrity: sha512-iO9ZQHkZxHn4mSakYV0vFHAVDyEOIJQrV2uZ06HxEPcx+mt8swXoZHIbaaJ2crJYFfErySgktuTZ3BeLz+XmFA==} + dev: true + + /@types/yargs/17.0.12: + resolution: {integrity: sha512-Nz4MPhecOFArtm81gFQvQqdV7XYCrWKx5uUt6GNHredFHn1i2mtWqXTON7EPXMtNi1qjtjEM/VCHDhcHsAMLXQ==} + dependencies: + '@types/yargs-parser': 21.0.0 + dev: true + /@typescript/twoslash/3.1.0: resolution: {integrity: sha512-kTwMUQ8xtAZaC4wb2XuLkPqFVBj2dNBueMQ89NWEuw87k2nLBbuafeG5cob/QEr6YduxIdTVUjix0MtC7mPlmg==} dependencies: From 4beb54d5e7fe7d9f76fe3a48a789dd4eabf34ebf Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 22 Sep 2022 22:08:50 -0400 Subject: [PATCH 2/5] document args in readme --- packages/create-svelte/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/create-svelte/README.md b/packages/create-svelte/README.md index 99cf8e729452..d54480bf85e9 100644 --- a/packages/create-svelte/README.md +++ b/packages/create-svelte/README.md @@ -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-directory \ + --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: From 6e8f7eb944e5a78675d5a74be09ceeb3b287d0d8 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 22 Sep 2022 22:19:05 -0400 Subject: [PATCH 3/5] handle types arg correctly --- packages/create-svelte/bin.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/create-svelte/bin.js b/packages/create-svelte/bin.js index a91fdfedcb3d..add6b37d3390 100755 --- a/packages/create-svelte/bin.js +++ b/packages/create-svelte/bin.js @@ -22,12 +22,14 @@ const { version } = JSON.parse(fs.readFileSync(new URL('package.json', import.me async function main() { console.log(gray(`\ncreate-svelte version ${version}`)); console.log(disclaimer); - const args = await yargs(hideBin(process.argv)).argv; + + const args = await yargs(hideBin(process.argv)).coerce('types', (arg) => + arg === 'none' ? null : arg + ).argv; prompts.override({ ...args, dir: args._[0]?.toString() }); - console.log(args); const { dir } = await prompts([ { From 1f76e34f2ce7880b67548c7cc5e65a23c7dcb220 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 22 Sep 2022 22:22:34 -0400 Subject: [PATCH 4/5] consistent docs in readme --- packages/create-svelte/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create-svelte/README.md b/packages/create-svelte/README.md index d54480bf85e9..059bb527f50e 100644 --- a/packages/create-svelte/README.md +++ b/packages/create-svelte/README.md @@ -13,7 +13,7 @@ npm create svelte@latest Some or all of the prompts can be skipped by supplying arguments directly to the CLI: ```bash -npm create svelte@latest my-directory \ +npm create svelte@latest my-app \ --name=my-new-app \ --template=default|skeleton|skeletonlib \ --types=checkjs|typescript|none \ From 48b1e4585286334fbf5431c9c5b535ad4cb5dfc9 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 22 Sep 2022 22:32:47 -0400 Subject: [PATCH 5/5] add changeset --- .changeset/tidy-icons-hug.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tidy-icons-hug.md diff --git a/.changeset/tidy-icons-hug.md b/.changeset/tidy-icons-hug.md new file mode 100644 index 000000000000..7f53dca780b9 --- /dev/null +++ b/.changeset/tidy-icons-hug.md @@ -0,0 +1,5 @@ +--- +'create-svelte': patch +--- + +Make it possible to skip some or all prompts by passing arguments directly to the CLI