Skip to content

Commit 27b8fb6

Browse files
committed
feat: add download command for publishing environment variables as JSON
1 parent 02eadd7 commit 27b8fb6

4 files changed

Lines changed: 106 additions & 2 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# `appcircle publish variable group download`
2+
3+
Download publish environment variables as JSON
4+
5+
```plaintext
6+
appcircle publish variable group download [options]
7+
```
8+
9+
## Examples
10+
11+
```plaintext
12+
$ appcircle publish variable group download --publishVariableGroupId "Variable Group ID"
13+
14+
$ appcircle publish variable group download --publishVariableGroupId "Variable Group ID" --path "/path/to/save"
15+
```
16+
17+
## Options
18+
19+
```plaintext
20+
--publishVariableGroupId <uuid> Variable Group ID
21+
--path <string> [OPTIONAL] The path for JSON file to be downloaded (Defaults to the current directory)
22+
```
23+
24+
## Options inherited from parent commands
25+
26+
```plaintext
27+
--help Show help for command
28+
```

docs/publish/variable/group/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ appcircle publish profile variable group [command] [options]
1616

1717
- [`list`](list.md)
1818
- [`view`](view.md)
19+
- [`download`](download.md)
1920

src/core/command-runner.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

src/core/commands.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,28 @@ export const Commands: CommandType[] = [
14811481
required: true
14821482
}
14831483
],
1484+
},
1485+
{
1486+
command: "download",
1487+
description: 'Download publish environment variables as JSON',
1488+
longDescription: 'Download publish environment variables as JSON file',
1489+
params: [
1490+
{
1491+
name: 'publishVariableGroupId',
1492+
description: 'Variable Group ID',
1493+
type: CommandParameterTypes.SELECT,
1494+
valueType: 'uuid',
1495+
required: true
1496+
},
1497+
{
1498+
name: 'path',
1499+
description: '[OPTIONAL] The path for JSON file to be downloaded',
1500+
longDescription:'[OPTIONAL] The path for JSON file to be downloaded (Defaults to the current directory)',
1501+
type: CommandParameterTypes.STRING,
1502+
valueType: 'string',
1503+
required: false,
1504+
}
1505+
],
14841506
}
14851507
]
14861508
}

0 commit comments

Comments
 (0)