import { IAssetWithContent , IBaseBundle , IBundle , IBundlesStorage , IBundleWithoutAssetsContent , } from "@staticdeploy/core" ; import { filter , find , last , map , pick , sortBy , uniq } from "lodash" ; import cloneMethodsIO from "./common/cloneMethodsIO" ; import convertErrors from "./common/convertErrors" ; import { ICollection } from "./common/ICollection" ; @ cloneMethodsIO @ convertErrors export default class BundlesStorage implements IBundlesStorage { constructor ( private bundles ) { } async findOne ( id ) { const bundle = this . bundles [ id ] ; return bundle ? this . removeAssetsContent ( bundle ) : null ; } async findLatestByNameAndTag ( name , tag ) { const bundles = filter ( this . bundles , { name , tag } ) ; const latestBundle = last ( sortBy ( bundles , "createdAt" ) ) ; return latestBundle ? this . removeAssetsContent ( latestBundle ) : null ; } async getBundleAssetContent ( bundleId , assetPath ) { const bundle = this . bundles [ bundleId ] ; if ( ! bundle ) { return null ; } const matchingAsset = find ( bundle . assets , { path : assetPath } ) ; return matchingAsset ? matchingAsset . content ! : null ; } async findMany ( ) { return map ( this . bundles , ( bundle ) => pick ( bundle , [ "id" , "name" , "tag" , "createdAt" ] ) ) ; } async findManyByNameAndTag ( name , tag ) { return map ( filter ( this . bundles , { name , tag } ) , this . removeAssetsContent ) ; } async findManyNames ( ) { return uniq ( map ( this . bundles , "name" ) ) ; } async findManyTagsByName ( name ) { return uniq ( map ( filter ( this . bundles , { name } ) , "tag" ) ) ; } async oneExistsWithId ( id ) { return ! ! this . bundles [ id ] ; } async createOne ( toBeCreatedBundle ) { this . bundles [ toBeCreatedBundle . id ] = toBeCreatedBundle ; return this . removeAssetsContent ( toBeCreatedBundle ) ; } async deleteMany ( ids ) { for ( const id of ids ) { delete this . bundles [ id ] ; } } private removeAssetsContent ( bundle ) { return { ... bundle , assets : bundle . assets . map ( ( asset ) => ( { ... asset , content : undefined , } ) ) , } ; } }