@gouvfr/dsfr-nexus
Version:
Le module `dsfr-nexus` est l'interface de ligne de commande (CLI) centrale du Système de Design de l’État - DSFR. Il offre des outils pour gérer et compiler les ressources du DSFR
54 lines (40 loc) • 1.36 kB
JavaScript
import path from 'path';
import { pathToFileURL } from 'url';
import { createFile, readYAMLSync } from '@gouvfr/dsfr-forge';
class PartGenerator {
constructor (state) {
this._state = state;
this._canWrite = false;
}
get state () {
return this._state;
}
async read () {
this._data = readYAMLSync(this._state.configFile('data.yml'));
if (!this._data.generation) return;
const generation = readYAMLSync(this._data.generation.config);
const baseDir = path.dirname('./');
this._generators = [];
for (const generatorData of generation.generators) {
const resolvedPath = path.resolve(baseDir, generatorData.path);
const moduleUrl = pathToFileURL(resolvedPath).href;
const module = await import(moduleUrl);
const GeneratorConstructor =
module.default ?? module.Generator ?? Object.values(module)[0];
const generator = new GeneratorConstructor(generatorData.dest);
this._generators.push(generator);
}
this._canWrite = this._generators.length > 0;
}
async write () {
if (!this._canWrite) return;
const outputs = [];
for (const generator of this._generators) {
outputs.push(... await generator.output());
}
for (const output of outputs) {
createFile(output.filepath, output.content);
}
}
}
export { PartGenerator };