-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
get-deployment-by-id-or-url.ts
89 lines (78 loc) · 2.44 KB
/
get-deployment-by-id-or-url.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import chalk from 'chalk';
import getDeployment from '../get-deployment';
import getTeamById from '../teams/get-team-by-id';
import { isValidName } from '../is-valid-name';
import type Client from '../client';
import type { Deployment, Team } from '@vercel-internals/types';
/**
* Renders feedback while retrieving a deployment, then validates the
* deployment belongs to the current team.
*
* @param client - The CLI client instance.
* @param contextName - The context/team name.
* @param deployIdOrUrl - The deployment id or URL.
* @returns The deployment info.
*/
export async function getDeploymentByIdOrURL({
client,
contextName,
deployIdOrUrl,
}: {
client: Client;
contextName: string;
deployIdOrUrl: string;
}): Promise<Deployment> {
const { config, output } = client;
if (!isValidName(deployIdOrUrl)) {
throw new Error(
`The provided argument "${deployIdOrUrl}" is not a valid deployment ID or URL`
);
}
let deployment: Deployment;
let team: Team | undefined;
try {
output.spinner(
`Fetching deployment "${deployIdOrUrl}" in ${chalk.bold(contextName)}…`
);
const [teamResult, deploymentResult] = await Promise.allSettled([
config.currentTeam ? getTeamById(client, config.currentTeam) : undefined,
getDeployment(client, contextName, deployIdOrUrl),
]);
if (teamResult.status === 'rejected') {
throw new Error(
`Failed to retrieve team information: ${teamResult.reason}`
);
}
if (deploymentResult.status === 'rejected') {
throw new Error(deploymentResult.reason.message);
}
team = teamResult.value;
deployment = deploymentResult.value;
// re-render the spinner text because it goes so fast
output.log(
`Fetching deployment "${deployIdOrUrl}" in ${chalk.bold(contextName)}…`
);
} finally {
output.stopSpinner();
}
if (deployment.team?.id) {
if (!team || deployment.team.id !== team.id) {
const err: NodeJS.ErrnoException = new Error(
team
? `Deployment doesn't belong to current team ${chalk.bold(
contextName
)}`
: `Deployment belongs to a different team`
);
err.code = 'ERR_INVALID_TEAM';
throw err;
}
} else if (team) {
const err: NodeJS.ErrnoException = new Error(
`Deployment doesn't belong to current team ${chalk.bold(contextName)}`
);
err.code = 'ERR_INVALID_TEAM';
throw err;
}
return deployment;
}