From 5da773473b06312e1864903ff49df41e421523f4 Mon Sep 17 00:00:00 2001 From: Alexander Noack Date: Fri, 7 Mar 2025 12:55:15 +0100 Subject: [PATCH 1/3] feat: add init command where preset can directly be passed --- README.md | 6 ++++++ cli.js | 7 +++++++ src/commands.js | 10 ++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 608e195..02c7707 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,12 @@ You will be prompted to choose a preset which will be copied to your templates d A config file and `.create-frontend-component` directory will be created aswell. +It is also possible to avoid the prompting and directly initialize a certain preset: + +```bash +npx create-frontend-component init:vue3 +``` + ### Configuration The `init` command creates the `.create-frontend-component/config.json` configuration file. diff --git a/cli.js b/cli.js index f108e69..df42061 100755 --- a/cli.js +++ b/cli.js @@ -57,6 +57,13 @@ program return } + if (componentName.toLowerCase().startsWith('init:')) { + const nameParts = componentName.toLowerCase().split(':') + const presetArgument = nameParts[1] + await processInitCommand(PRESET_PATH, CONFIG_DIRECTORY, CONFIG_FILE_NAME, configDefaults, presetArgument) + return + } + const { types, templatePath, componentPath, nameStyle } = loadConfig() const allowedComponentTypes = types || [] const fullTemplatePath = path.join(process.cwd(), templatePath) diff --git a/src/commands.js b/src/commands.js index d61e30e..ee33249 100644 --- a/src/commands.js +++ b/src/commands.js @@ -98,11 +98,17 @@ export function processCreateComponentCommand(env, allowedComponentTypes, fullTe * @param {string} configDirectory * @param {string} configFileName * @param {string} configDefaults + * @param {string} [presetArgument] * @return {Promise} */ -export async function processInitCommand(presetPath, configDirectory, configFileName, configDefaults) { +export async function processInitCommand(presetPath, configDirectory, configFileName, configDefaults, presetArgument) { const availablePresets = getDirectories(presetPath) - const presetName = await promptSingleSelect('Choose a preset', availablePresets) + let presetName + if (presetArgument) { + presetName = presetArgument + } else { + presetName = await promptSingleSelect('Choose a preset', availablePresets) + } return initProjectInWorkingDirectory(path.join(presetPath, presetName), configDirectory, configFileName, configDefaults) } From 28d08d412a87b8dfc8823d2561710be2f9070df7 Mon Sep 17 00:00:00 2001 From: Alexander Noack Date: Fri, 7 Mar 2025 12:55:45 +0100 Subject: [PATCH 2/3] feat: improve error handling for missing configuration file --- cli.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/cli.js b/cli.js index df42061..76beb86 100755 --- a/cli.js +++ b/cli.js @@ -9,7 +9,7 @@ import { processUpgradeCommand, } from './src/commands.js' import { getDirectories } from './src/utilities.js' -import { readFileSync } from 'fs' +import { readFileSync, existsSync } from 'fs' import path from 'path' import { fileURLToPath } from 'url' @@ -34,13 +34,25 @@ const configDefaults = { */ function loadConfig() { const filePath = path.resolve(process.cwd(), '.create-frontend-component', 'config.json') - const configFromFile = JSON.parse( - readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '') - ) - return { - ...configDefaults, - ...configFromFile + try { + if (!existsSync(filePath)) { + console.error(`Error: Configuration file not found at ${filePath}.`) + console.error('Run "npx create-frontend-component init" to generate the configuration file.') + process.exit(1) + } + + const fileContent = readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '') + const configFromFile = JSON.parse(fileContent) + + return { + ...configDefaults, + ...configFromFile + } + } catch (error) { + console.error(`Error loading configuration file: ${error.message}`) + console.error('Try running "npx create-frontend-component init" to reset the configuration.') + process.exit(1) } } From eae0020df86ceae9a49e9ebbd6187d155c7b5afe Mon Sep 17 00:00:00 2001 From: Alexander Noack Date: Fri, 7 Mar 2025 13:00:36 +0100 Subject: [PATCH 3/3] ci: add cli test workflow --- .github/workflows/cli.yml | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .github/workflows/cli.yml diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml new file mode 100644 index 0000000..0291b04 --- /dev/null +++ b/.github/workflows/cli.yml @@ -0,0 +1,56 @@ +name: CLI test + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + name: Test on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: 22 + + - name: Install Dependencies + run: npm install --save-dev create-frontend-component + + - name: Run CLI to init config + run: npx create-frontend-component init:vue3 + + - name: Run CLI to generate a component + run: npx create-frontend-component test-component --type molecules --flavour default + + - name: Verify Generated Files (Linux/macOS) + if: matrix.os != 'windows-latest' + run: | + test -d src/components/molecules/TestComponent && echo "Component directory exists" + test -f src/components/molecules/TestComponent/TestComponent.vue && echo "Vue file exists" + test -f src/components/molecules/TestComponent/TestComponent.stories.mdx && echo "MDX file exists" + + - name: Verify Generated Files (Windows) + if: matrix.os == 'windows-latest' + shell: pwsh + run: | + if (!(Test-Path "src/components/molecules/TestComponent" -PathType Container)) { exit 1 } + if (!(Test-Path "src/components/molecules/TestComponent/TestComponent.vue")) { exit 1 } + if (!(Test-Path "src/components/molecules/TestComponent/TestComponent.stories.mdx")) { exit 1 } + + - name: Cleanup (Linux/macOS) + if: matrix.os != 'windows-latest' + run: rm -rf src/components/molecules/TestComponent + + - name: Cleanup (Windows) + if: matrix.os == 'windows-latest' + shell: pwsh + run: Remove-Item -Recurse -Force src/components/molecules/TestComponent