@@ -613,6 +613,57 @@ const handleBuildCommand = async (command: ProgramCommand, params:any) => {
613613 fullCommandName : command . fullCommandName ,
614614 data : responseData ,
615615 } ) ;
616+ } else if ( command . fullCommandName === `${ PROGRAM_NAME } -build-variable-download` ) {
617+ const spinner = createOra ( 'Downloading environment variables...' ) . start ( ) ;
618+ try {
619+ const variableGroups = await getEnvironmentVariableGroups ( ) ;
620+ const variableGroup = variableGroups . find ( ( group : any ) => group . id === params . variableGroupId ) ;
621+
622+ if ( ! variableGroup ) {
623+ spinner . fail ( `Variable group with ID ${ params . variableGroupId } not found` ) ;
624+ throw new Error ( `Variable group not found` ) ;
625+ }
626+
627+ const responseData = await getEnvironmentVariables ( params ) ;
628+
629+ let formattedVariables = responseData . map ( ( variable : any ) => ( {
630+ key : variable . key ,
631+ value : variable . value ,
632+ isSecret : variable . isSecret ,
633+ isFile : variable . isFile || false ,
634+ id : variable . key
635+ } ) ) ;
636+
637+ formattedVariables . sort ( ( a : any , b : any ) => {
638+ const aKey = a . key ;
639+ const bKey = b . key ;
640+ return bKey . localeCompare ( aKey ) ;
641+ } ) ;
642+
643+ const timestamp = Date . now ( ) ;
644+ const fileName = `${ variableGroup . name } _${ timestamp } .json` ;
645+ let filePath = params . path || process . cwd ( ) ;
646+
647+ if ( filePath . includes ( '~' ) ) {
648+ filePath = filePath . replace ( / ~ / g, os . homedir ( ) ) ;
649+ }
650+
651+ filePath = path . resolve ( filePath ) ;
652+
653+ if ( ! fs . existsSync ( filePath ) ) {
654+ fs . mkdirSync ( filePath , { recursive : true } ) ;
655+ }
656+
657+ if ( fs . statSync ( filePath ) . isDirectory ( ) ) {
658+ filePath = path . join ( filePath , fileName ) ;
659+ }
660+
661+ fs . writeFileSync ( filePath , JSON . stringify ( formattedVariables ) ) ;
662+ spinner . succeed ( `Environment variables downloaded successfully to ${ filePath } ` ) ;
663+ } catch ( e ) {
664+ spinner . fail ( 'Failed to download environment variables' ) ;
665+ throw e ;
666+ }
616667 } else if ( command . fullCommandName === `${ PROGRAM_NAME } -build-variable-create` ) {
617668 const spinner = createOra ( 'Creating environment variable' ) . start ( ) ;
618669 try {
0 commit comments