@@ -442,7 +442,60 @@ const handlePublishCommand = async (command: ProgramCommand, params: any) => {
442442 fullCommandName : command . fullCommandName ,
443443 data : variables . variables ,
444444 } ) ;
445- } else if ( command . fullCommandName === `${ PROGRAM_NAME } -publish-profile-version-list` ) {
445+ } else if ( command . fullCommandName === `${ PROGRAM_NAME } -publish-variable-group-download` ) {
446+ const spinner = createOra ( 'Downloading publish environment variables...' ) . start ( ) ;
447+ try {
448+ const variableGroups = await getPublishVariableGroups ( ) ;
449+ const variableGroup = variableGroups . find ( ( group : any ) => group . id === params . publishVariableGroupId ) ;
450+
451+ if ( ! variableGroup ) {
452+ spinner . fail ( `Variable group with ID ${ params . publishVariableGroupId } not found` ) ;
453+ throw new Error ( `Variable group not found` ) ;
454+ }
455+
456+ const variables = await getPublishVariableListByGroupId ( params ) ;
457+
458+ let formattedVariables = variables . variables . map ( ( variable : any ) => ( {
459+ key : variable . key ,
460+ value : variable . value ,
461+ isSecret : variable . isSecret ,
462+ isFile : variable . isFile || false ,
463+ id : variable . key
464+ } ) ) ;
465+
466+ formattedVariables . sort ( ( a : any , b : any ) => {
467+ const aKey = a . key ;
468+ const bKey = b . key ;
469+ return bKey . localeCompare ( aKey ) ;
470+ } ) ;
471+
472+ const timestamp = Date . now ( ) ;
473+ const fileName = `${ variableGroup . name } _${ timestamp } .json` ;
474+
475+ let filePath = params . path || process . cwd ( ) ;
476+
477+ if ( filePath . includes ( '~' ) ) {
478+ filePath = filePath . replace ( / ~ / g, os . homedir ( ) ) ;
479+ }
480+
481+ filePath = path . resolve ( filePath ) ;
482+
483+ if ( ! fs . existsSync ( filePath ) ) {
484+ fs . mkdirSync ( filePath , { recursive : true } ) ;
485+ }
486+
487+ if ( fs . statSync ( filePath ) . isDirectory ( ) ) {
488+ filePath = path . join ( filePath , fileName ) ;
489+ }
490+
491+ fs . writeFileSync ( filePath , JSON . stringify ( formattedVariables ) ) ;
492+
493+ spinner . succeed ( `Publish environment variables downloaded successfully to ${ filePath } ` ) ;
494+ } catch ( e ) {
495+ spinner . fail ( 'Failed to download publish environment variables' ) ;
496+ throw e ;
497+ }
498+ } else if ( command . fullCommandName === `${ PROGRAM_NAME } -publish-profile-version-list` ) {
446499 const spinner = createOra ( 'Fetching...' ) . start ( ) ;
447500 const appVersions = await getAppVersions ( params ) ;
448501 spinner . succeed ( ) ;
@@ -521,7 +574,7 @@ const handleBuildCommand = async (command: ProgramCommand, params:any) => {
521574 fullCommandName : command . fullCommandName ,
522575 data : responseData ,
523576 } ) ;
524- } else if ( command . fullCommandName === `${ PROGRAM_NAME } -build-profile-branch-list` ) {
577+ } else if ( command . fullCommandName === `${ PROGRAM_NAME } -build-profile-branch-list` ) {
525578 const spinner = createOra ( 'Fetching...' ) . start ( ) ;
526579 const responseData = await getBranches ( params ) ;
527580 spinner . succeed ( ) ;
0 commit comments