Skip to content

Commit

Permalink
fix: remove method with proper error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
weilinzung committed Dec 3, 2024
1 parent 0015d59 commit 76185cb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

singleQuote: false
trailingComma: none
11 changes: 9 additions & 2 deletions bin/action.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -93102,14 +93102,21 @@ async function removePreviews({
}));
}
}
function removeChannel(gacFilename, deployConfig, channelId) {
async function removeChannel(gacFilename, deployConfig, channelId) {
const {
projectId,
target,
firebaseToolsVersion
} = deployConfig;
return execWithCredentials(["hosting:channel:delete", channelId, "--force"], projectId, gacFilename, {
const deleteChannelText = await execWithCredentials(["hosting:channel:delete", channelId, ...(target ? ["--site", target] : []), "--force"], projectId, gacFilename, {
firebaseToolsVersion
});
const channelResults = JSON.parse(deleteChannelText.trim());
if (channelResults.status === "error") {
throw Error(channelResults.error);
} else {
return channelResults.status || "success";
}
}
async function deployPreview(gacFilename, deployConfig) {
const {
Expand Down
43 changes: 31 additions & 12 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export type ErrorResult = {
error: string;
};

export type ChannelDeleteSuccessResult = {
status: "success";
};

export type ChannelTotalSuccessResult = {
status: "success";
channels: Channel[];
Expand Down Expand Up @@ -126,7 +130,7 @@ export function interpretChannelDeployResult(
return {
expireTime,
expire_time_formatted,
urls,
urls
};
}

Expand All @@ -148,19 +152,19 @@ async function execWithCredentials(
...(projectId ? ["--project", projectId] : []),
debug
? "--debug" // gives a more thorough error message
: "--json", // allows us to easily parse the output
: "--json" // allows us to easily parse the output
],
{
listeners: {
stdout(data: Buffer) {
deployOutputBuf.push(data);
},
}
},
env: {
...process.env,
FIREBASE_DEPLOY_AGENT: "action-hosting-deploy",
GOOGLE_APPLICATION_CREDENTIALS: gacFilename, // the CLI will automatically authenticate with this env variable set
},
GOOGLE_APPLICATION_CREDENTIALS: gacFilename // the CLI will automatically authenticate with this env variable set
}
}
);
} catch (e) {
Expand All @@ -173,7 +177,7 @@ async function execWithCredentials(
);
await execWithCredentials(args, projectId, gacFilename, {
debug: true,
firebaseToolsVersion,
firebaseToolsVersion
});
} else {
throw e;
Expand Down Expand Up @@ -280,7 +284,7 @@ function getPreviewChannelToRemove(
export async function removePreviews({
channels,
gacFilename,
deployConfig,
deployConfig
}: {
channels: Channel[];
gacFilename: string;
Expand Down Expand Up @@ -311,19 +315,34 @@ export async function removePreviews({
}
}

export function removeChannel(
export async function removeChannel(
gacFilename: string,
deployConfig: Omit<ChannelDeployConfig, "expires" | "channelId">,
channelId: string
): Promise<string> {
const { projectId, firebaseToolsVersion } = deployConfig;
const { projectId, target, firebaseToolsVersion } = deployConfig;

return execWithCredentials(
["hosting:channel:delete", channelId, "--force"],
const deleteChannelText = await execWithCredentials(
[
"hosting:channel:delete",
channelId,
...(target ? ["--site", target] : []),
"--force"
],
projectId,
gacFilename,
{ firebaseToolsVersion }
);

const channelResults = JSON.parse(deleteChannelText.trim()) as
| ChannelDeleteSuccessResult
| ErrorResult;

if (channelResults.status === "error") {
throw Error((channelResults as ErrorResult).error);
} else {
return channelResults.status || "success";
}
}

export async function deployPreview(
Expand All @@ -338,7 +357,7 @@ export async function deployPreview(
"hosting:channel:deploy",
channelId,
...(target ? ["--only", target] : []),
...(expires ? ["--expires", expires] : []),
...(expires ? ["--expires", expires] : [])
],
projectId,
gacFilename,
Expand Down

0 comments on commit 76185cb

Please sign in to comment.