UNPKG

@enspirit/emb

Version:

A replacement for our Makefile-for-monorepos

61 lines (60 loc) 1.82 kB
import jsonpatch from 'fast-json-patch'; import { join } from 'node:path'; import { toIdentifedHash, } from './index.js'; export class Component { name; config; monorepo; _rootDir; tasks; resources; flavors; constructor(name, config, monorepo) { this.name = name; this.config = config; this.monorepo = monorepo; this._rootDir = config.rootDir; this.tasks = toIdentifedHash(config.tasks || {}, this.name); this.resources = toIdentifedHash( // Due to the schema.json -> typescript conversion weirdness config.resources || {}, this.name); this.flavors = toIdentifedHash(config.flavors || {}, this.name); } get rootDir() { return this._rootDir || this.name; } flavor(name, mustExist = true) { const flavor = this.flavors[name]; if (!flavor && mustExist) { throw new Error(`Unknown flavor: ${name}`); } return flavor; } cloneWith(config) { return new Component(this.name, { ...this.config, ...config, }, this.monorepo); } toJSON() { return structuredClone(this.config); } withFlavor(name) { const original = this.toJSON(); const patches = this.flavor(name).patches || []; const errors = jsonpatch.validate(patches, original); if (errors) { throw errors; } const patched = patches.reduce((doc, patch, index) => { return jsonpatch.applyReducer(doc, patch, index); }, original); return new Component(this.name, patched, this.monorepo); } join(path) { return this.monorepo.join(join(this.rootDir, path)); } relative(path) { return join(this.rootDir, path); } }