-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): add a getGeneration helper function and apply it across …
…the CLI
- Loading branch information
1 parent
51ad3ef
commit 6c50633
Showing
17 changed files
with
607 additions
and
556 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,63 @@ | ||
import {APIClient} from '@heroku-cli/command' | ||
import {App} from '../types/fir' | ||
|
||
async function getApp(appOrName: App | string, herokuApi?: APIClient): Promise<App> { | ||
if (typeof appOrName === 'string') { | ||
if (herokuApi === undefined) | ||
throw new Error('herokuApi parameter is required when passing an app name') | ||
|
||
const {body: app} = await herokuApi.get<App>( | ||
`/apps/${appOrName}`, { | ||
headers: {Accept: 'application/vnd.heroku+json; version=3.sdk'}, | ||
}) | ||
return app | ||
import {App, Space, DynoSize, TeamApp, Pipeline, Generation, AppGeneration, DynoSizeGeneration, PipelineGeneration} from '../types/fir' | ||
import Dyno from '../run/dyno' | ||
|
||
export type GenerationKind = 'fir' | 'cedar'; | ||
// web.1 web-1234abcde-123ab | ||
export type GenerationLike = Generation | AppGeneration | DynoSizeGeneration | PipelineGeneration | Dyno | ||
export type GenerationCapable = App | Space | DynoSize | TeamApp | Pipeline | ||
|
||
function getGenerationFromGenerationLike(generation: string | GenerationLike | undefined): GenerationKind | undefined { | ||
let maybeGeneration = '' | ||
|
||
if (typeof generation === 'string') { | ||
maybeGeneration = generation | ||
} else if (generation && 'name' in generation) { | ||
maybeGeneration = generation.name ?? '' | ||
} | ||
|
||
return appOrName | ||
if (/(fir|cedar)/.test(maybeGeneration)) { | ||
return generation as GenerationKind | ||
} | ||
|
||
// web-1234abcde44-123ab etc. fir | ||
if (/^web-[0-9a-z]+-[0-9a-z]{5}$/.test(maybeGeneration)) { | ||
return 'fir' | ||
} | ||
|
||
// web.n cedar | ||
if (/^web\.[0-9]+$/.test(maybeGeneration)) { | ||
return 'cedar' | ||
} | ||
|
||
return undefined | ||
} | ||
|
||
export async function isFirApp(appOrName: App | string, herokuApi?: APIClient) { | ||
const app = await getApp(appOrName, herokuApi) | ||
return app.generation.name === 'fir' | ||
/** | ||
* Get the generation of an object | ||
* | ||
* @param source The object to get the generation from | ||
* @returns The generation of the object | ||
*/ | ||
export function getGeneration(source: GenerationLike | GenerationCapable | string): GenerationKind | undefined { | ||
if (typeof source === 'object' && 'generation' in source) { | ||
return getGenerationFromGenerationLike(source.generation) | ||
} | ||
|
||
return getGenerationFromGenerationLike(source) | ||
} | ||
|
||
export async function isCedarApp(appOrName: App | string, herokuApi?: APIClient) { | ||
const app = await getApp(appOrName, herokuApi) | ||
return app.generation.name === 'cedar' | ||
/** | ||
* Get the generation of an app by id or name | ||
* | ||
* @param appIdOrName The id or name of the app to get the generation for | ||
* @param herokuApi The Heroku API client to use | ||
* @returns The generation of the app | ||
*/ | ||
export async function getGenerationByAppId(appIdOrName: string, herokuApi: APIClient) { | ||
const {body: app} = await herokuApi.get<App>( | ||
`/apps/${appIdOrName}`, { | ||
headers: {Accept: 'application/vnd.heroku+json; version=3.sdk'}, | ||
}) | ||
return getGeneration(app) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.