77 processInitCommand ,
88 processPromptCommand ,
99 processUpgradeCommand ,
10+ type CommandEnv ,
1011} from './src/commands.js'
1112import { getDirectories } from './src/utilities.js'
1213import { readFileSync , existsSync } from 'fs'
@@ -16,24 +17,27 @@ import { fileURLToPath } from 'url'
1617const __filename = fileURLToPath ( import . meta. url )
1718const __dirname = path . dirname ( __filename )
1819
20+ interface AppConfig {
21+ types: string [ ] | null
22+ templatePath: string
23+ componentPath: string
24+ nameStyle: string
25+ }
1926
2027const CONFIG_DIRECTORY = '.create-frontend-component'
2128const CONFIG_FILE_NAME = 'config.json'
2229const PRESET_DIR = 'presets'
2330const 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+
5974program
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