()
| 100 | } |
| 101 | |
| 102 | async function selectProProject() { |
| 103 | try { |
| 104 | const token = (await login()) as string |
| 105 | const spinner = ora('Fetching account...').start() |
| 106 | const response = await fetch(`${APP_URL}/api/account`, { |
| 107 | headers: { |
| 108 | Authorization: `Bearer ${token}`, |
| 109 | 'Content-Type': 'application/json', |
| 110 | Accept: 'application/json', |
| 111 | }, |
| 112 | }) |
| 113 | const data = await response.json() |
| 114 | spinner.stop() |
| 115 | |
| 116 | const res = await prompts({ |
| 117 | type: 'select', |
| 118 | name: 'team', |
| 119 | message: 'Select a team:', |
| 120 | choices: data.teams |
| 121 | .map( |
| 122 | (team: { |
| 123 | id: number |
| 124 | name: string |
| 125 | projects: Array<{ name: string; api_key: string }> |
| 126 | }) => ({ |
| 127 | title: team.name, |
| 128 | value: team, |
| 129 | }) |
| 130 | ) |
| 131 | .concat([{ title: 'Create a new team', value: 'new' }]), |
| 132 | }) |
| 133 | |
| 134 | if (res.team === 'new') { |
| 135 | const team = await createTeam(token) |
| 136 | const project = await createProject(token, team.id) |
| 137 | return project.api_key |
| 138 | } else { |
| 139 | let { project } = await prompts({ |
| 140 | type: 'select', |
| 141 | name: 'project', |
| 142 | message: 'Select a project:', |
| 143 | choices: res.team.projects |
| 144 | .map((team: { id: number; name: string }) => ({ |
| 145 | title: team.name, |
| 146 | value: team, |
| 147 | })) |
| 148 | .concat([{ title: 'Create a new project', value: 'new' }]), |
| 149 | }) |
| 150 | |
| 151 | if (project === 'new') { |
| 152 | project = await createProject(token, res.team.id) |
| 153 | } |
| 154 | return project.api_key |
| 155 | } |
| 156 | } catch (err) { |
| 157 | console.log(err) |
| 158 | error('Login failed.') |
| 159 | } |
no test coverage detected