import { Builder as BuilderInterface, RawCard, CompiledCard, Saved, Unsaved } from '@cardstack/core/src/interfaces'; import { Compiler } from '@cardstack/core/src/compiler'; import { inject } from '@cardstack/di'; import logger from '@cardstack/logger'; import { NotFound } from '@cardstack/core/src/utils/errors'; const log = logger('hub/card-builder'); export default class CardBuilder implements BuilderInterface { realmManager = inject('realm-manager', { as: 'realmManager' }); cache = inject('file-cache', { as: 'cache' }); cards = inject('card-service', { as: 'cards' }); index = inject('searchIndex', { as: 'index' }); async getRawCard(url: string): Promise { log.trace('getRawCard: %s', url); return await this.realmManager.read(this.realmManager.parseCardURL(url.replace(/\/$/, ''))); } async getCompiledCard(url: string): Promise { log.trace('getCompiledCard: %s', url); let { compiled } = await this.index.getCard(url); if (!compiled) { throw new NotFound(`CardBuilder could not find ${url}`); } return compiled; } compileCardFromRaw(cardSource: RawCard): Compiler { return new Compiler({ builder: this, cardSource }); } } declare module '@cardstack/di' { interface KnownServices { 'card-builder': CardBuilder; } }