11import { input } from "@inquirer/prompts" ;
22import chalk from "chalk" ;
3- import { Command , program } from "commander" ;
3+ import { Command } from "commander" ;
44import { mkdir , writeFile } from "node:fs/promises" ;
55import { dirname , isAbsolute , join } from "node:path" ;
66import ora , { Ora } from "ora" ;
77
88import { createFeature , listFeatures } from "../services/features.js" ;
9- import { getConfig , getProjectPath } from "../utils /config.js" ;
10- import { handleError } from "../utils/error .js" ;
9+ import { configStore } from "../stores /config.js" ;
10+ import { handleError , MissingAppIdError } from "../utils/errors .js" ;
1111import { genDTS , genFeatureKey , KeyFormatPatterns } from "../utils/gen.js" ;
12- import { options } from "../utils/options.js" ;
13-
14- type AppIdArgs = {
15- appId : string ;
16- } ;
17-
18- type CreateFeatureArgs = AppIdArgs & {
12+ import {
13+ appIdOption ,
14+ featureKeyOption ,
15+ featureNameArgument ,
16+ typesOutOption ,
17+ } from "../utils/options.js" ;
18+
19+ type CreateFeatureArgs = {
1920 key ?: string ;
2021} ;
2122
22- type GenerateTypesArgs = AppIdArgs & {
23- out : string ;
24- } ;
25-
2623export const createFeatureAction = async (
2724 name : string | undefined ,
28- { appId , key } : CreateFeatureArgs ,
25+ { key } : CreateFeatureArgs ,
2926) => {
30- const { baseUrl } = program . opts ( ) ;
27+ const { baseUrl, appId } = configStore . getConfig ( ) ;
3128 let spinner : Ora | undefined ;
3229 let existingKeys : string [ ] = [ ] ;
30+
3331 try {
32+ if ( ! appId ) throw new MissingAppIdError ( ) ;
3433 spinner = ora (
3534 `Loading features of app ${ chalk . cyan ( appId ) } at ${ chalk . cyan ( baseUrl ) } ...` ,
3635 ) . start ( ) ;
@@ -42,6 +41,7 @@ export const createFeatureAction = async (
4241 } catch ( error ) {
4342 spinner ?. fail ( "Loading features failed" ) ;
4443 void handleError ( error , "Features Create" ) ;
44+ return ;
4545 }
4646
4747 try {
@@ -53,7 +53,7 @@ export const createFeatureAction = async (
5353 }
5454
5555 if ( ! key ) {
56- const keyFormat = getConfig ( "keyFormat" ) ?? "custom" ;
56+ const keyFormat = configStore . getConfig ( "keyFormat" ) ?? "custom" ;
5757 key = await input ( {
5858 message : "New feature key:" ,
5959 default : genFeatureKey ( name , keyFormat , existingKeys ) ,
@@ -72,31 +72,32 @@ export const createFeatureAction = async (
7272 }
7373} ;
7474
75- export const listFeaturesAction = async ( { appId } : AppIdArgs ) => {
76- const { baseUrl } = program . opts ( ) ;
77- const spinner = ora (
78- `Loading features of app ${ chalk . cyan ( appId ) } at ${ chalk . cyan ( baseUrl ) } ...` ,
79- ) . start ( ) ;
75+ export const listFeaturesAction = async ( ) => {
76+ const { baseUrl, appId } = configStore . getConfig ( ) ;
77+ let spinner : Ora | undefined ;
78+
8079 try {
80+ if ( ! appId ) throw new MissingAppIdError ( ) ;
81+ spinner = ora (
82+ `Loading features of app ${ chalk . cyan ( appId ) } at ${ chalk . cyan ( baseUrl ) } ...` ,
83+ ) . start ( ) ;
8184 const features = await listFeatures ( appId ) ;
8285 spinner . succeed (
8386 `Loaded features of app ${ chalk . cyan ( appId ) } at ${ chalk . cyan ( baseUrl ) } ` ,
8487 ) ;
8588 console . table ( features ) ;
8689 } catch ( error ) {
87- spinner . fail ( "Loading features failed" ) ;
90+ spinner ? .fail ( "Loading features failed" ) ;
8891 void handleError ( error , "Features List" ) ;
8992 }
9093} ;
9194
92- export const generateTypesAction = async ( {
93- appId,
94- out,
95- } : GenerateTypesArgs ) => {
96- const { baseUrl } = program . opts ( ) ;
95+ export const generateTypesAction = async ( ) => {
96+ const { baseUrl, appId, typesPath } = configStore . getConfig ( ) ;
9797 let spinner : Ora | undefined ;
9898 let featureKeys : string [ ] = [ ] ;
9999 try {
100+ if ( ! appId ) throw new MissingAppIdError ( ) ;
100101 spinner = ora (
101102 `Loading features of app ${ chalk . cyan ( appId ) } at ${ chalk . cyan ( baseUrl ) } ...` ,
102103 ) . start ( ) ;
@@ -107,12 +108,15 @@ export const generateTypesAction = async ({
107108 } catch ( error ) {
108109 spinner ?. fail ( "Loading features failed" ) ;
109110 void handleError ( error , "Features Types" ) ;
111+ return ;
110112 }
111113
112114 try {
113115 spinner = ora ( "Generating feature types..." ) . start ( ) ;
114116 const types = genDTS ( featureKeys ) ;
115- const outPath = isAbsolute ( out ) ? out : join ( getProjectPath ( ) , out ) ;
117+ const outPath = isAbsolute ( typesPath )
118+ ? typesPath
119+ : join ( configStore . getProjectPath ( ) , typesPath ) ;
116120 await mkdir ( dirname ( outPath ) , { recursive : true } ) ;
117121 await writeFile ( outPath , types ) ;
118122 spinner . succeed ( "Generated feature types successfully" ) ;
@@ -131,39 +135,29 @@ export function registerFeatureCommands(cli: Command) {
131135 featuresCommand
132136 . command ( "create" )
133137 . description ( "Create a new feature" )
134- . requiredOption (
135- options . appId . flags ,
136- options . appId . description ,
137- getConfig ( options . appId . configKey ) ,
138- )
139- . option ( options . featureKey . flags , options . featureKey . description )
140- . argument ( options . featureName . flags , options . featureName . description )
138+ . addOption ( appIdOption )
139+ . addOption ( featureKeyOption )
140+ . addArgument ( featureNameArgument )
141141 . action ( createFeatureAction ) ;
142142
143143 featuresCommand
144144 . command ( "list" )
145145 . description ( "List all features" )
146- . requiredOption (
147- options . appId . flags ,
148- options . appId . description ,
149- getConfig ( options . appId . configKey ) ,
150- )
146+ . addOption ( appIdOption )
151147 . action ( listFeaturesAction ) ;
152148
153149 featuresCommand
154150 . command ( "types" )
155151 . description ( "Generate feature types" )
156- . requiredOption (
157- options . appId . flags ,
158- options . appId . description ,
159- getConfig ( options . appId . configKey ) ,
160- )
161- . requiredOption (
162- options . typesOut . flags ,
163- options . typesOut . description ,
164- getConfig ( options . typesOut . configKey ) ?? options . typesOut . fallback ,
165- )
152+ . addOption ( appIdOption )
153+ . addOption ( typesOutOption )
166154 . action ( generateTypesAction ) ;
167155
156+ // Update the config with the cli override values
157+ featuresCommand . hook ( "preAction" , ( command ) => {
158+ const { appId, out } = command . opts ( ) ;
159+ configStore . setConfig ( { appId, typesPath : out } ) ;
160+ } ) ;
161+
168162 cli . addCommand ( featuresCommand ) ;
169163}
0 commit comments