@enspirit/emb
Version:
A replacement for our Makefile-for-monorepos
49 lines (48 loc) • 1.65 kB
JavaScript
import deepmerge from '@fastify/deepmerge';
import { glob } from 'glob';
import { basename, dirname, join } from 'node:path';
import { validateEmbfile } from '../../config/index.js';
import { AbstractPlugin } from './plugin.js';
export const EmbfileLoaderPluginDefaultOptions = {
glob: '*/Embfile.{yaml,yml}',
};
export class EmbfileLoaderPlugin extends AbstractPlugin {
monorepo;
static name = 'embfiles';
constructor(cfg, monorepo) {
const config = {
...EmbfileLoaderPluginDefaultOptions,
...cfg,
};
if (!Array.isArray(config.glob)) {
config.glob = [config.glob];
}
super(config, monorepo);
this.monorepo = monorepo;
}
async extendConfig(config) {
const files = await glob(this.config.glob, {
...this.config,
cwd: this.monorepo.rootDir,
follow: true,
});
const newConfig = await files.reduce(async (pConfig, path) => {
const config = await pConfig;
const rootDir = dirname(path);
const name = basename(rootDir);
const embfile = await join(this.monorepo.rootDir, path);
const component = await validateEmbfile(embfile);
const original = config.components[name];
const newComponent = deepmerge()(original || {}, {
...component,
rootDir,
});
return config.with({
components: {
[name]: newComponent,
},
});
}, Promise.resolve(config));
return newConfig;
}
}