Skip to content

Commit d56326b

Browse files
authored
Merge pull request #25 from diva-e/feature/ci-cross-platform-tests
CI: cross platform CLI tests
2 parents 2d97d08 + eae0020 commit d56326b

File tree

4 files changed

+96
-9
lines changed

4 files changed

+96
-9
lines changed

.github/workflows/cli.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: CLI test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
name: Test on ${{ matrix.os }}
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
matrix:
15+
os: [ubuntu-latest, macos-latest, windows-latest]
16+
steps:
17+
- name: Checkout Repository
18+
uses: actions/checkout@v3
19+
20+
- name: Set up Node.js
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: 22
24+
25+
- name: Install Dependencies
26+
run: npm install --save-dev create-frontend-component
27+
28+
- name: Run CLI to init config
29+
run: npx create-frontend-component init:vue3
30+
31+
- name: Run CLI to generate a component
32+
run: npx create-frontend-component test-component --type molecules --flavour default
33+
34+
- name: Verify Generated Files (Linux/macOS)
35+
if: matrix.os != 'windows-latest'
36+
run: |
37+
test -d src/components/molecules/TestComponent && echo "Component directory exists"
38+
test -f src/components/molecules/TestComponent/TestComponent.vue && echo "Vue file exists"
39+
test -f src/components/molecules/TestComponent/TestComponent.stories.mdx && echo "MDX file exists"
40+
41+
- name: Verify Generated Files (Windows)
42+
if: matrix.os == 'windows-latest'
43+
shell: pwsh
44+
run: |
45+
if (!(Test-Path "src/components/molecules/TestComponent" -PathType Container)) { exit 1 }
46+
if (!(Test-Path "src/components/molecules/TestComponent/TestComponent.vue")) { exit 1 }
47+
if (!(Test-Path "src/components/molecules/TestComponent/TestComponent.stories.mdx")) { exit 1 }
48+
49+
- name: Cleanup (Linux/macOS)
50+
if: matrix.os != 'windows-latest'
51+
run: rm -rf src/components/molecules/TestComponent
52+
53+
- name: Cleanup (Windows)
54+
if: matrix.os == 'windows-latest'
55+
shell: pwsh
56+
run: Remove-Item -Recurse -Force src/components/molecules/TestComponent

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ You will be prompted to choose a preset which will be copied to your templates d
2222

2323
A config file and `.create-frontend-component` directory will be created aswell.
2424

25+
It is also possible to avoid the prompting and directly initialize a certain preset:
26+
27+
```bash
28+
npx create-frontend-component init:vue3
29+
```
30+
2531
### Configuration
2632

2733
The `init` command creates the `.create-frontend-component/config.json` configuration file.

cli.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
processUpgradeCommand,
1010
} from './src/commands.js'
1111
import { getDirectories } from './src/utilities.js'
12-
import { readFileSync } from 'fs'
12+
import { readFileSync, existsSync } from 'fs'
1313
import path from 'path'
1414
import { fileURLToPath } from 'url'
1515

@@ -34,13 +34,25 @@ const configDefaults = {
3434
*/
3535
function loadConfig() {
3636
const filePath = path.resolve(process.cwd(), '.create-frontend-component', 'config.json')
37-
const configFromFile = JSON.parse(
38-
readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '')
39-
)
4037

41-
return {
42-
...configDefaults,
43-
...configFromFile
38+
try {
39+
if (!existsSync(filePath)) {
40+
console.error(`Error: Configuration file not found at ${filePath}.`)
41+
console.error('Run "npx create-frontend-component init" to generate the configuration file.')
42+
process.exit(1)
43+
}
44+
45+
const fileContent = readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '')
46+
const configFromFile = JSON.parse(fileContent)
47+
48+
return {
49+
...configDefaults,
50+
...configFromFile
51+
}
52+
} catch (error) {
53+
console.error(`Error loading configuration file: ${error.message}`)
54+
console.error('Try running "npx create-frontend-component init" to reset the configuration.')
55+
process.exit(1)
4456
}
4557
}
4658

@@ -57,6 +69,13 @@ program
5769
return
5870
}
5971

72+
if (componentName.toLowerCase().startsWith('init:')) {
73+
const nameParts = componentName.toLowerCase().split(':')
74+
const presetArgument = nameParts[1]
75+
await processInitCommand(PRESET_PATH, CONFIG_DIRECTORY, CONFIG_FILE_NAME, configDefaults, presetArgument)
76+
return
77+
}
78+
6079
const { types, templatePath, componentPath, nameStyle } = loadConfig()
6180
const allowedComponentTypes = types || []
6281
const fullTemplatePath = path.join(process.cwd(), templatePath)

src/commands.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,17 @@ export function processCreateComponentCommand(env, allowedComponentTypes, fullTe
9898
* @param {string} configDirectory
9999
* @param {string} configFileName
100100
* @param {string} configDefaults
101+
* @param {string} [presetArgument]
101102
* @return {Promise<void>}
102103
*/
103-
export async function processInitCommand(presetPath, configDirectory, configFileName, configDefaults) {
104+
export async function processInitCommand(presetPath, configDirectory, configFileName, configDefaults, presetArgument) {
104105
const availablePresets = getDirectories(presetPath)
105-
const presetName = await promptSingleSelect('Choose a preset', availablePresets)
106+
let presetName
107+
if (presetArgument) {
108+
presetName = presetArgument
109+
} else {
110+
presetName = await promptSingleSelect('Choose a preset', availablePresets)
111+
}
106112
return initProjectInWorkingDirectory(path.join(presetPath, presetName), configDirectory, configFileName, configDefaults)
107113
}
108114

0 commit comments

Comments
 (0)