import { BundleNotFoundError , EntrypointMismatchedAppIdError , } from "../common/errors" ; import Usecase from "../common/Usecase" ; import { splitNameTagCombination } from "../entities/Bundle" ; import CreateApp from "./CreateApp" ; import CreateEntrypoint from "./CreateEntrypoint" ; import UpdateEntrypoint from "./UpdateEntrypoint" ; export default class DeployBundle extends Usecase { async exec ( options ) { const { bundleNameTagCombination , appName , entrypointUrlMatcher } = options ; const [ bundleName , bundleTag ] = splitNameTagCombination ( bundleNameTagCombination ) ; const [ bundle , existingApp , existingEntrypoint ] = await Promise . all ( [ this . storages . bundles . findLatestByNameAndTag ( bundleName , bundleTag ) , this . storages . apps . findOneByName ( appName ) , this . storages . entrypoints . findOneByUrlMatcher ( entrypointUrlMatcher ) , ] ) ; if ( ! bundle ) { throw new BundleNotFoundError ( bundleNameTagCombination , "name:tag combination" ) ; } if ( existingEntrypoint && ( ! existingApp || existingApp . id !== existingEntrypoint . appId ) ) { throw new EntrypointMismatchedAppIdError ( entrypointUrlMatcher , appName ) ; } let app = existingApp ; if ( ! app ) { app = await this . makeUsecase ( CreateApp ) . exec ( { name : appName } ) ; } if ( ! existingEntrypoint ) { await this . makeUsecase ( CreateEntrypoint ) . exec ( { appId : app . id , bundleId : bundle . id , urlMatcher : entrypointUrlMatcher , } ) ; } else { await this . makeUsecase ( UpdateEntrypoint ) . exec ( existingEntrypoint . id , { bundleId : bundle . id } ) ; } } }