From 18b2ce6ba539971db8b7d954ebcdf03ed96b9d9b Mon Sep 17 00:00:00 2001 From: Ben Kates Date: Thu, 25 Jul 2024 12:46:17 -0400 Subject: [PATCH] update component creation logic to handle command arguments, DRY --- bin/createComponent/createComponent.js | 103 ++++++++++++++----------- package-lock.json | 31 +------- package.json | 2 +- 3 files changed, 59 insertions(+), 77 deletions(-) diff --git a/bin/createComponent/createComponent.js b/bin/createComponent/createComponent.js index 991fde3..435443c 100644 --- a/bin/createComponent/createComponent.js +++ b/bin/createComponent/createComponent.js @@ -1,7 +1,8 @@ #!/usr/bin/env node -import inquirer from "inquirer"; +import { input } from "@inquirer/prompts"; import fs from "fs"; import path from "path"; +import process from "process"; import { createStory } from "./createStory.js"; import { createDocs } from "./createDocs.js"; @@ -10,52 +11,62 @@ function isPascalCase(str) { return /^[A-Z][A-Za-z]*$/.test(str); } +// check if name is valid +function validateName(componentName) { + if (!componentName) { + return "Please enter a component name"; + } else if (!isPascalCase(componentName)) { + return "Component name must be in PascalCase"; + } else { + return true; + } +} + +function createFiles(componentName) { + // validate file name + const validated = validateName(componentName); + if (validated !== true) { + return console.error(validated); + } + + // declare dir + const dir = path.join(process.cwd(), "src", "lib", componentName); + + // if directory exists show error, else create files + if (fs.existsSync(dir)) { + console.error("Directory already exists"); + } else { + // create directory + fs.mkdirSync(dir, { recursive: true }); + + // create Component.svelte (blank) + const svelteFilePath = path.join(dir, `${componentName}.svelte`); + fs.writeFileSync(svelteFilePath, "", "utf8"); + + // create Component.docs.md + const docsFilePath = path.join(dir, `${componentName}.docs.md`); + fs.writeFileSync(docsFilePath, createDocs(componentName), "utf8"); + + // create Component.stories.svelte + const storyFilePath = path.join(dir, `${componentName}.stories.svelte`); + fs.writeFileSync(storyFilePath, createStory(componentName), "utf8"); + + // Confirmation text + console.log(`Boilerplate files created in ${dir}`); + } +} + async function main() { - await inquirer - .prompt([ - { - type: "input", - name: "componentName", - message: "What is the name of your component? (ie: MyComponent)", - validate: (componentName) => { - // if not present - if (!componentName) { - return "Please enter a component name"; - // if not PascalCase - } else if (!isPascalCase(componentName)) { - return "Component name must be in PascalCase"; - } else { - return true; - } - } - } - ]) - .then((answers) => { - const { componentName } = answers; - const dir = path.join(process.cwd(), "src", "lib", componentName); - // if directory doesn't exist, create it - if (!fs.existsSync(dir)) { - // create directory - fs.mkdirSync(dir, { recursive: true }); - - // create Component.svelte (blank) - const svelteFilePath = path.join(dir, `${componentName}.svelte`); - fs.writeFileSync(svelteFilePath, "", "utf8"); - - // create Component.docs.md - const docsFilePath = path.join(dir, `${componentName}.docs.md`); - fs.writeFileSync(docsFilePath, createDocs(componentName), "utf8"); - - // create Component.stories.svelte - const storyFilePath = path.join(dir, `${componentName}.stories.svelte`); - fs.writeFileSync(storyFilePath, createStory(componentName), "utf8"); - - // Confirmation text - console.log(`Boilerplate files created in ${dir}`); - } else { - console.error("Directory already exists"); - } - }); + // if there's an argument in the command, use it (but no more) + if (process.argv[2] && !process.argv[3]) { + createFiles(process.argv[2]); + } else { + // show input + await input({ + message: "What is the name of your component? (ie: MyComponent)", + validate: validateName + }).then(createFiles); + } } main().catch(console.error); diff --git a/package-lock.json b/package-lock.json index cc9e194..6493ff3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,7 @@ }, "devDependencies": { "@etchteam/storybook-addon-github-link": "^1.0.1", + "@inquirer/prompts": "^5.3.2", "@storybook/addon-a11y": "^8.0.5", "@storybook/addon-essentials": "^8.0.5", "@storybook/addon-interactions": "^8.0.5", @@ -43,7 +44,6 @@ "@sveltejs/package": "^2.3.0", "concurrently": "^8.2.2", "http-server": "^14.1.1", - "inquirer": "^10.1.2", "prettier": "^3.2.5", "prettier-plugin-svelte": "^3.2.2", "publint": "^0.1.9", @@ -11429,25 +11429,6 @@ "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", "dev": true }, - "node_modules/inquirer": { - "version": "10.1.2", - "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-10.1.2.tgz", - "integrity": "sha512-rwoy4e2xFz3rru5Q5695rKXLbNlVfODRhRftBEPdxHfbGzN1T8Fxzszcb//SaRnQHsRdiwR8dOZOV+y4SoZ4KQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "@inquirer/prompts": "^5.3.2", - "@inquirer/type": "^1.5.1", - "@types/mute-stream": "^0.0.4", - "ansi-escapes": "^4.3.2", - "mute-stream": "^1.0.0", - "run-async": "^3.0.0", - "rxjs": "^7.8.1" - }, - "engines": { - "node": ">=18" - } - }, "node_modules/internal-slot": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.7.tgz", @@ -17118,16 +17099,6 @@ "fsevents": "~2.3.2" } }, - "node_modules/run-async": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/run-async/-/run-async-3.0.0.tgz", - "integrity": "sha512-540WwVDOMxA6dN6We19EcT9sc3hkXPw5mzRNGM3FkdN/vtE9NFvj5lFAPNwUDmJjXidm3v7TC1cTE7t17Ulm1Q==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.12.0" - } - }, "node_modules/run-parallel": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", diff --git a/package.json b/package.json index 45cab39..dbd6fdb 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ }, "devDependencies": { "@etchteam/storybook-addon-github-link": "^1.0.1", + "@inquirer/prompts": "^5.3.2", "@storybook/addon-a11y": "^8.0.5", "@storybook/addon-essentials": "^8.0.5", "@storybook/addon-interactions": "^8.0.5", @@ -73,7 +74,6 @@ "@sveltejs/package": "^2.3.0", "concurrently": "^8.2.2", "http-server": "^14.1.1", - "inquirer": "^10.1.2", "prettier": "^3.2.5", "prettier-plugin-svelte": "^3.2.2", "publint": "^0.1.9",