import { Framework , Files } from "../utils" ; import { FileContent , SMConfigPath } from "./paths" ; export interface Manifest { apiEndpoint : string ; storybook ? : string ; libraries ? : string [ ] ; framework ? : Framework . FrameworkEnum ; chromaticAppId ? : string ; _latest ? : string ; } export function createManifest ( cwd , manifest ) { const manifestPath = SMConfigPath ( cwd ) ; Files . write ( manifestPath , JSON . stringify ( manifest , null , "\t" ) , { recursive : false , } ) ; } export function retrieveManifest ( cwd ) { const manifestPath = SMConfigPath ( cwd ) ; if ( ! Files . exists ( manifestPath ) ) { return { exists : false , content : null , } ; } const content = Files . safeReadJson ( manifestPath ) as Manifest | null ; return { exists : true , content , } ; } export function maybeRepoNameFromSMFile ( cwd , base ) { try { const baseUrl = new URL ( base ) ; const maybeSMFile = retrieveManifest ( cwd ) ; if ( maybeSMFile . exists === false ) return null ; if ( ! maybeSMFile . content ?. apiEndpoint ) return null ; const repoUrl = new URL ( maybeSMFile . content . apiEndpoint ) ; const correctBase = repoUrl . hostname . includes ( baseUrl . hostname ) ; if ( correctBase === false ) return null ; return repoUrl . hostname . split ( "." ) [ 0 ] ; } catch { return null ; } } export function patchManifest ( cwd , data ) { const manifest = retrieveManifest ( cwd ) ; if ( ! manifest . exists || ! manifest . content ) return false ; const updatedManifest = { ... manifest . content , ... data , } ; Files . write ( SMConfigPath ( cwd ) , updatedManifest ) ; return true ; } export function updateManifestSMVersion ( cwd , version ) { const manifest = retrieveManifest ( cwd ) ; if ( manifest . content ?. _latest ) return false ; return patchManifest ( cwd , { _latest : version } ) ; }