Skip to content

Commit 3a88d7b

Browse files
authored
feat: add --all flag to list and delete, --all-but-latest to delete; (#341)
* feat: add --all flag to list and delete, --all-but-latest to delete; * changeset
1 parent ff6af73 commit 3a88d7b

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

.changeset/bright-rabbits-bathe.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@rnef/tools': patch
3+
'@rnef/cli': patch
4+
---
5+
6+
feat: add --all flag to list and delete, --all-but-latest to delete

packages/cli/src/lib/plugins/remoteCache.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type Flags = {
1818
traits?: string[];
1919
name?: string;
2020
json?: boolean;
21+
all?: boolean;
22+
allButLatest?: boolean;
2123
};
2224

2325
async function remoteCache({
@@ -55,14 +57,26 @@ async function remoteCache({
5557

5658
switch (action) {
5759
case 'list': {
58-
const artifacts = await remoteBuildCache.list({ artifactName });
59-
const artifact = artifacts[0];
60-
if (artifact) {
60+
const artifacts = await remoteBuildCache.list({
61+
artifactName,
62+
limit: args.all ? undefined : 1,
63+
});
64+
if (artifacts.length > 0 && !args.all) {
65+
const artifact = artifacts[0];
6166
if (isJsonOutput) {
6267
console.log(JSON.stringify(artifact, null, 2));
6368
} else {
6469
logger.log(`- name: ${artifact.name}
65-
- url: ${artifact.url}`);
70+
- url: ${artifact.url}`);
71+
}
72+
} else if (artifacts.length > 0 && args.all) {
73+
if (isJsonOutput) {
74+
console.log(JSON.stringify(artifacts, null, 2));
75+
} else {
76+
artifacts.forEach((artifact) => {
77+
logger.log(`- name: ${artifact.name}
78+
- url: ${artifact.url}`);
79+
});
6680
}
6781
}
6882
break;
@@ -117,7 +131,11 @@ async function remoteCache({
117131
break;
118132
}
119133
case 'delete': {
120-
const deletedArtifacts = await remoteBuildCache.delete({ artifactName });
134+
const deletedArtifacts = await remoteBuildCache.delete({
135+
artifactName,
136+
limit: args.all || args.allButLatest ? undefined : 1,
137+
skipLatest: args.allButLatest,
138+
});
121139
if (isJsonOutput) {
122140
console.log(JSON.stringify(deletedArtifacts, null, 2));
123141
} else {
@@ -144,6 +162,10 @@ function validateArgs(args: Flags, action: string) {
144162
'Action is required. Available actions: list, list-all, download, upload, delete'
145163
);
146164
}
165+
if (action === 'list-all') {
166+
// return early as we don't need to validate name or platform to list all artifacts
167+
return;
168+
}
147169
if (args.name && (args.platform || args.traits)) {
148170
throw new RnefError(
149171
'Cannot use "--name" together with "--platform" or "--traits". Use either name or platform with traits'
@@ -193,6 +215,16 @@ export const remoteCachePlugin =
193215
name: '--name <string>',
194216
description: 'Full artifact name',
195217
},
218+
{
219+
name: '--all',
220+
description:
221+
'List or delete all matching artifacts. Affects "list" and "delete" actions only',
222+
},
223+
{
224+
name: '--all-but-latest',
225+
description:
226+
'Delete all but the latest matching artifact. Affects "delete" action only',
227+
},
196228
{
197229
name: '-p, --platform <string>',
198230
description: 'Select platform, e.g. ios or android',

packages/tools/src/lib/build-cache/common.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@ export interface RemoteBuildCache {
4949

5050
/**
5151
* Delete a remote artifact
52-
* @param artifact - Remote artifact to delete, as returned by `list` method
52+
* @param artifactName - Name of the artifact to delete, e.g. `rnef-android-debug-1234567890` for android in debug variant
53+
* @param limit - Optional maximum number of artifacts to delete
54+
* @param skipLatest - Optional flag to skip the latest artifact, helpful when deleting all but the latest artifact
5355
* @returns Array of deleted artifacts
5456
* @throws {Error} Throws if artifact is not found or deletion fails
5557
*/
56-
delete({ artifactName }: { artifactName: string }): Promise<RemoteArtifact[]>;
58+
delete({
59+
artifactName,
60+
limit,
61+
skipLatest,
62+
}: {
63+
artifactName: string;
64+
limit?: number;
65+
skipLatest?: boolean;
66+
}): Promise<RemoteArtifact[]>;
5767

5868
/**
5969
* Upload a local artifact stored in build cache to remote storage

packages/tools/src/lib/build-cache/github/GitHubBuildCache.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,28 @@ export class GitHubBuildCache implements RemoteBuildCache {
6767

6868
async delete({
6969
artifactName,
70+
limit,
71+
skipLatest,
7072
}: {
7173
artifactName: string;
74+
limit?: number;
75+
skipLatest?: boolean;
7276
}): Promise<RemoteArtifact[]> {
7377
const repoDetails = await this.getRepoDetails();
7478
const artifacts = await fetchGitHubArtifactsByName(
7579
artifactName,
7680
repoDetails,
77-
undefined
81+
limit
7882
);
7983
if (artifacts.length === 0) {
8084
throw new Error(`No artifact found with name "${artifactName}"`);
8185
}
82-
return await deleteGitHubArtifacts(artifacts, repoDetails, artifactName);
86+
const [, ...rest] = artifacts;
87+
return await deleteGitHubArtifacts(
88+
skipLatest ? rest : artifacts,
89+
repoDetails,
90+
artifactName
91+
);
8392
}
8493

8594
async upload(): Promise<RemoteArtifact> {

0 commit comments

Comments
 (0)