Skip to content

CI: cross platform CLI tests #25

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 3 commits into from
Mar 7, 2025
Merged
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
56 changes: 56 additions & 0 deletions .github/workflows/cli.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 26 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)
}
}

Expand All @@ -57,6 +69,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)
Expand Down
10 changes: 8 additions & 2 deletions src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,17 @@ export function processCreateComponentCommand(env, allowedComponentTypes, fullTe
* @param {string} configDirectory
* @param {string} configFileName
* @param {string} configDefaults
* @param {string} [presetArgument]
* @return {Promise<void>}
*/
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)
}