Skip to content

Commit a038037

Browse files
authored
Merge pull request #27 from diva-e/feature/move-to-typescript
Migrate to typescript
2 parents 8305245 + 14935ff commit a038037

22 files changed

Lines changed: 1801 additions & 859 deletions

.eslintignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.eslintrc.json

Lines changed: 0 additions & 43 deletions
This file was deleted.

.github/workflows/cli.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ name: CLI test
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
87

98
jobs:
109
test:
@@ -15,10 +14,10 @@ jobs:
1514
os: [ubuntu-latest, macos-latest, windows-latest]
1615
steps:
1716
- name: Checkout Repository
18-
uses: actions/checkout@v3
17+
uses: actions/checkout@v4
1918

2019
- name: Set up Node.js
21-
uses: actions/setup-node@v3
20+
uses: actions/setup-node@v4
2221
with:
2322
node-version: 22
2423

.github/workflows/node.js.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ name: Node.js CI
55

66
on:
77
push:
8-
branches: [ main ]
8+
branches: [main]
99
pull_request:
10-
branches: [ main ]
1110

1211
jobs:
1312
build:
@@ -16,13 +15,13 @@ jobs:
1615

1716
strategy:
1817
matrix:
19-
node-version: [18.x, 20.x, 22.x]
18+
node-version: [20.x, 22.x]
2019
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2120

2221
steps:
23-
- uses: actions/checkout@v2
22+
- uses: actions/checkout@v4
2423
- name: Use Node.js ${{ matrix.node-version }}
25-
uses: actions/setup-node@v2
24+
uses: actions/setup-node@v4
2625
with:
2726
node-version: ${{ matrix.node-version }}
2827
- run: npm ci

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ playground/src
55
.idea/
66

77
*.iml
8-
*.iws
8+
*.iws
9+
10+
dist/

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
- Make sure to run `npm test` and `npm run lint:fix` after changes have been finished
2020
- Keep the project lightweight and avoid adding more dependencies
21-
- Make sure to update `src/serve-mocks.spec.js` with tests for new features, it is basically a kind of e2e test which covers most of logic
21+
- Make sure to update existing unit tests or create new ones for new features
2222

2323
## Scripts
2424

cli.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
export interface AppConfig {
3+
types: string[] | null;
4+
templatePath: string;
5+
componentPath: string;
6+
nameStyle: string;
7+
}
8+
//# sourceMappingURL=cli.d.ts.map

cli.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli.js renamed to cli.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
processInitCommand,
88
processPromptCommand,
99
processUpgradeCommand,
10+
type CommandEnv,
1011
} from './src/commands.js'
1112
import { getDirectories } from './src/utilities.js'
1213
import { readFileSync, existsSync } from 'fs'
@@ -16,24 +17,27 @@ import { fileURLToPath } from 'url'
1617
const __filename = fileURLToPath(import.meta.url)
1718
const __dirname = path.dirname(__filename)
1819

20+
interface AppConfig {
21+
types: string[] | null
22+
templatePath: string
23+
componentPath: string
24+
nameStyle: string
25+
}
1926

2027
const CONFIG_DIRECTORY = '.create-frontend-component'
2128
const CONFIG_FILE_NAME = 'config.json'
2229
const PRESET_DIR = 'presets'
2330
const PRESET_PATH = path.join(__dirname, PRESET_DIR)
2431

25-
const configDefaults = {
32+
const configDefaults: AppConfig = {
2633
types: ['atoms', 'molecules', 'organisms'],
27-
templatePath: CONFIG_DIRECTORY + '/templates',
34+
templatePath: `${CONFIG_DIRECTORY}/templates`,
2835
componentPath: 'src/components',
2936
nameStyle: 'pascalCase'
3037
}
3138

32-
/**
33-
* @return {object}
34-
*/
35-
function loadConfig() {
36-
const filePath = path.resolve(process.cwd(), '.create-frontend-component', 'config.json')
39+
function loadConfig(): AppConfig {
40+
const filePath = path.resolve(process.cwd(), CONFIG_DIRECTORY, CONFIG_FILE_NAME)
3741

3842
try {
3943
if (!existsSync(filePath)) {
@@ -46,22 +50,32 @@ function loadConfig() {
4650
const configFromFile = JSON.parse(fileContent)
4751

4852
return {
49-
...configDefaults,
53+
types: configDefaults.types ?? null,
54+
templatePath: configDefaults.templatePath,
55+
componentPath: configDefaults.componentPath,
56+
nameStyle: configDefaults.nameStyle,
5057
...configFromFile
5158
}
5259
} catch (error) {
53-
console.error(`Error loading configuration file: ${error.message}`)
60+
console.error(`Error loading configuration file: ${(error as Error).message}`)
5461
console.error('Try running "npx create-frontend-component init" to reset the configuration.')
5562
process.exit(1)
5663
}
5764
}
5865

66+
const packageJsonPath = path.join(__dirname, '..', 'package.json')
67+
const { version } = JSON.parse(readFileSync(packageJsonPath, { encoding: 'utf-8' }))
68+
69+
program
70+
.name('create-frontend-component')
71+
.description('Frontend Component Generator')
72+
.version(version)
73+
5974
program
60-
.version('2.1.0')
61-
.command('create-frontend-component [component-name]') // Define the command
62-
.option( '-t, --type <type>', 'Component type, default: atoms')
63-
.option( '-f, --flavour <flavour>', 'Component flavour')
64-
.action( async function(componentNameArg, env) {
75+
.command('create-frontend-component [component-name]')
76+
.option('-t, --type <type>', 'Component type, default: atoms')
77+
.option('-f, --flavour <flavour>', 'Component flavour')
78+
.action(async function(componentNameArg: string | undefined, env: CommandEnv) {
6579
const componentName = componentNameArg || ''
6680

6781
if (componentName.toLowerCase() === 'init') {
@@ -81,6 +95,12 @@ program
8195
const fullTemplatePath = path.join(process.cwd(), templatePath)
8296
const availableFlavours = getDirectories(fullTemplatePath)
8397

98+
if (availableFlavours.length === 0) {
99+
console.error('Error: No flavours found in template directory.')
100+
console.error(`Please ensure templates exist in ${fullTemplatePath}`)
101+
process.exit(1)
102+
}
103+
84104
if (componentName.toLowerCase() === 'prompt' || !componentName.trim()) {
85105
await processPromptCommand(allowedComponentTypes, availableFlavours, fullTemplatePath, componentPath, nameStyle)
86106
} else if (componentName.toLowerCase() === 'upgrade') {
@@ -89,4 +109,5 @@ program
89109
processCreateComponentCommand(env, allowedComponentTypes, fullTemplatePath, componentPath, componentName, availableFlavours, nameStyle)
90110
}
91111
})
92-
.parse(process.argv)
112+
113+
program.parse(process.argv)

0 commit comments

Comments
 (0)