@@ -453,7 +453,60 @@ const handlePublishCommand = async (command: ProgramCommand, params: any) => {
453453 fullCommandName : command . fullCommandName ,
454454 data : variables . variables ,
455455 } ) ;
456- } else if ( command . fullCommandName === `${ PROGRAM_NAME } -publish-profile-version-list` ) {
456+ } else if ( command . fullCommandName === `${ PROGRAM_NAME } -publish-variable-group-download` ) {
457+ const spinner = createOra ( 'Downloading publish environment variables...' ) . start ( ) ;
458+ try {
459+ const variableGroups = await getPublishVariableGroups ( ) ;
460+ const variableGroup = variableGroups . find ( ( group : any ) => group . id === params . publishVariableGroupId ) ;
461+
462+ if ( ! variableGroup ) {
463+ spinner . fail ( `Variable group with ID ${ params . publishVariableGroupId } not found` ) ;
464+ throw new Error ( `Variable group not found` ) ;
465+ }
466+
467+ const variables = await getPublishVariableListByGroupId ( params ) ;
468+
469+ let formattedVariables = variables . variables . map ( ( variable : any ) => ( {
470+ key : variable . key ,
471+ value : variable . value ,
472+ isSecret : variable . isSecret ,
473+ isFile : variable . isFile || false ,
474+ id : variable . key
475+ } ) ) ;
476+
477+ formattedVariables . sort ( ( a : any , b : any ) => {
478+ const aKey = a . key ;
479+ const bKey = b . key ;
480+ return bKey . localeCompare ( aKey ) ;
481+ } ) ;
482+
483+ const timestamp = Date . now ( ) ;
484+ const fileName = `${ variableGroup . name } _${ timestamp } .json` ;
485+
486+ let filePath = params . path || process . cwd ( ) ;
487+
488+ if ( filePath . includes ( '~' ) ) {
489+ filePath = filePath . replace ( / ~ / g, os . homedir ( ) ) ;
490+ }
491+
492+ filePath = path . resolve ( filePath ) ;
493+
494+ if ( ! fs . existsSync ( filePath ) ) {
495+ fs . mkdirSync ( filePath , { recursive : true } ) ;
496+ }
497+
498+ if ( fs . statSync ( filePath ) . isDirectory ( ) ) {
499+ filePath = path . join ( filePath , fileName ) ;
500+ }
501+
502+ fs . writeFileSync ( filePath , JSON . stringify ( formattedVariables ) ) ;
503+
504+ spinner . succeed ( `Publish environment variables downloaded successfully to ${ filePath } ` ) ;
505+ } catch ( e ) {
506+ spinner . fail ( 'Failed to download publish environment variables' ) ;
507+ throw e ;
508+ }
509+ } else if ( command . fullCommandName === `${ PROGRAM_NAME } -publish-profile-version-list` ) {
457510 const spinner = createOra ( 'Fetching...' ) . start ( ) ;
458511 const appVersions = await getAppVersions ( params ) ;
459512 spinner . succeed ( ) ;
@@ -532,7 +585,7 @@ const handleBuildCommand = async (command: ProgramCommand, params:any) => {
532585 fullCommandName : command . fullCommandName ,
533586 data : responseData ,
534587 } ) ;
535- } else if ( command . fullCommandName === `${ PROGRAM_NAME } -build-profile-branch-list` ) {
588+ } else if ( command . fullCommandName === `${ PROGRAM_NAME } -build-profile-branch-list` ) {
536589 const spinner = createOra ( 'Fetching...' ) . start ( ) ;
537590 const responseData = await getBranches ( params ) ;
538591 spinner . succeed ( ) ;
@@ -624,6 +677,57 @@ const handleBuildCommand = async (command: ProgramCommand, params:any) => {
624677 fullCommandName : command . fullCommandName ,
625678 data : responseData ,
626679 } ) ;
680+ } else if ( command . fullCommandName === `${ PROGRAM_NAME } -build-variable-download` ) {
681+ const spinner = createOra ( 'Downloading environment variables...' ) . start ( ) ;
682+ try {
683+ const variableGroups = await getEnvironmentVariableGroups ( ) ;
684+ const variableGroup = variableGroups . find ( ( group : any ) => group . id === params . variableGroupId ) ;
685+
686+ if ( ! variableGroup ) {
687+ spinner . fail ( `Variable group with ID ${ params . variableGroupId } not found` ) ;
688+ throw new Error ( `Variable group not found` ) ;
689+ }
690+
691+ const responseData = await getEnvironmentVariables ( params ) ;
692+
693+ let formattedVariables = responseData . map ( ( variable : any ) => ( {
694+ key : variable . key ,
695+ value : variable . value ,
696+ isSecret : variable . isSecret ,
697+ isFile : variable . isFile || false ,
698+ id : variable . key
699+ } ) ) ;
700+
701+ formattedVariables . sort ( ( a : any , b : any ) => {
702+ const aKey = a . key ;
703+ const bKey = b . key ;
704+ return bKey . localeCompare ( aKey ) ;
705+ } ) ;
706+
707+ const timestamp = Date . now ( ) ;
708+ const fileName = `${ variableGroup . name } _${ timestamp } .json` ;
709+ let filePath = params . path || process . cwd ( ) ;
710+
711+ if ( filePath . includes ( '~' ) ) {
712+ filePath = filePath . replace ( / ~ / g, os . homedir ( ) ) ;
713+ }
714+
715+ filePath = path . resolve ( filePath ) ;
716+
717+ if ( ! fs . existsSync ( filePath ) ) {
718+ fs . mkdirSync ( filePath , { recursive : true } ) ;
719+ }
720+
721+ if ( fs . statSync ( filePath ) . isDirectory ( ) ) {
722+ filePath = path . join ( filePath , fileName ) ;
723+ }
724+
725+ fs . writeFileSync ( filePath , JSON . stringify ( formattedVariables ) ) ;
726+ spinner . succeed ( `Environment variables downloaded successfully to ${ filePath } ` ) ;
727+ } catch ( e ) {
728+ spinner . fail ( 'Failed to download environment variables' ) ;
729+ throw e ;
730+ }
627731 } else if ( command . fullCommandName === `${ PROGRAM_NAME } -build-variable-create` ) {
628732 const spinner = createOra ( 'Creating environment variable' ) . start ( ) ;
629733 try {
0 commit comments