11import { Command } from "@commander-js/extra-typings" ;
2- import { readProjectConfig } from "./lib/config.js" ;
3- import chalk from "chalk" ;
4- import { bigBrainAPI } from "./lib/utils/utils.js" ;
5- import { oneoffContext } from "../bundler/context.js" ;
6- import { logError , logMessage , logOutput } from "../bundler/log.js" ;
7-
8- type Deployment = {
9- id : number ;
10- name : string ;
11- create_time : number ;
12- deployment_type : "dev" | "prod" ;
13- } ;
2+ import { oneoffContext , Context } from "../bundler/context.js" ;
3+ import { logMessage } from "../bundler/log.js" ;
4+ import {
5+ getDeploymentSelection ,
6+ DeploymentSelection ,
7+ } from "./lib/deploymentSelection.js" ;
8+ import { fetchTeamAndProject } from "./lib/api.js" ;
9+
10+ // This is a debugging command: it's output is not stable, don't write scripts
11+ // that depend on its output.
12+
13+ // TODO: for the deployments command to list all deployments in a project
14+ // we need a stable endpoint for listing projects (check) and a way to
15+ // get a project ID in all cases to use it. We have an endpoint that lists
16+ // deployments by team/project slug today but it's not in use and we'll
17+ // be able to deprecate it if we avoid using it.
1418
1519export const deployments = new Command ( "deployments" )
1620 . description ( "List deployments associated with a project" )
@@ -21,18 +25,78 @@ export const deployments = new Command("deployments")
2125 adminKey : undefined ,
2226 envFile : undefined ,
2327 } ) ;
24- const { projectConfig : config } = await readProjectConfig ( ctx ) ;
25-
26- const url = `teams/${ config . team } /projects/${ config . project } /deployments` ;
27-
28- logMessage ( `Deployments for project ${ config . team } /${ config . project } ` ) ;
29- const deployments = ( await bigBrainAPI ( {
30- ctx,
31- method : "GET" ,
32- url,
33- } ) ) as Deployment [ ] ;
34- logOutput ( deployments ) ;
35- if ( deployments . length === 0 ) {
36- logError ( chalk . yellow ( `No deployments exist for project` ) ) ;
37- }
28+
29+ const deploymentSelection = await getDeploymentSelection ( ctx , {
30+ url : undefined ,
31+ adminKey : undefined ,
32+ envFile : undefined ,
33+ } ) ;
34+
35+ await displayCurrentDeploymentInfo ( ctx , deploymentSelection ) ;
3836 } ) ;
37+
38+ async function displayCurrentDeploymentInfo (
39+ ctx : Context ,
40+ selection : DeploymentSelection ,
41+ ) {
42+ logMessage ( "Currently configured deployment:" ) ;
43+
44+ switch ( selection . kind ) {
45+ case "existingDeployment" : {
46+ const { deploymentToActOn } = selection ;
47+ logMessage ( ` URL: ${ deploymentToActOn . url } ` ) ;
48+
49+ if ( deploymentToActOn . deploymentFields ) {
50+ const fields = deploymentToActOn . deploymentFields ;
51+ logMessage (
52+ ` Deployment: ${ fields . deploymentName } (${ fields . deploymentType } )` ,
53+ ) ;
54+ logMessage ( ` Team: ${ fields . teamSlug } ` ) ;
55+ logMessage ( ` Project: ${ fields . projectSlug } ` ) ;
56+ } else {
57+ logMessage ( ` Type: ${ deploymentToActOn . source } ` ) ;
58+ }
59+ break ;
60+ }
61+ case "deploymentWithinProject" : {
62+ const { targetProject } = selection ;
63+ if ( targetProject . kind === "teamAndProjectSlugs" ) {
64+ logMessage ( ` Team: ${ targetProject . teamSlug } ` ) ;
65+ logMessage ( ` Project: ${ targetProject . projectSlug } ` ) ;
66+ } else if ( targetProject . kind === "deploymentName" ) {
67+ const slugs = await fetchTeamAndProject (
68+ ctx ,
69+ targetProject . deploymentName ,
70+ ) ;
71+ logMessage ( ` Team: ${ slugs . team } ` ) ;
72+ logMessage ( ` Project: ${ slugs . project } ` ) ;
73+ logMessage ( ` Deployment: ${ targetProject . deploymentName } ` ) ;
74+ if ( targetProject . deploymentType ) {
75+ logMessage ( ` Type: ${ targetProject . deploymentType } ` ) ;
76+ }
77+ } else {
78+ logMessage ( ` Project deploy key configured` ) ;
79+ }
80+ break ;
81+ }
82+ case "preview" : {
83+ logMessage ( ` Preview deployment (deploy key configured)` ) ;
84+ break ;
85+ }
86+ case "anonymous" : {
87+ if ( selection . deploymentName ) {
88+ logMessage ( ` Anonymous deployment: ${ selection . deploymentName } ` ) ;
89+ } else {
90+ logMessage ( ` Anonymous development (no deployment selected)` ) ;
91+ }
92+ break ;
93+ }
94+ case "chooseProject" : {
95+ logMessage ( ` No project configured - will prompt interactively` ) ;
96+ break ;
97+ }
98+ default : {
99+ logMessage ( ` Unknown deployment configuration` ) ;
100+ }
101+ }
102+ }
0 commit comments