UNPKG

@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

135 lines (110 loc) 4.18 kB
import path from 'path'; import { createFile, log, readYAMLSync } from '@gouvfr/dsfr-forge'; let getComposers, Definition; try { const weave = await import('@gouvfr/dsfr-weave'); getComposers = weave.getComposers; Definition = weave.Definition; } catch (e) { log // La dépendance n'est pas disponible getComposers = null; Definition = null; } class PartComposer { constructor (state, frameworkIds, description) { this._state = state; this._requestedFrameworkIds = frameworkIds; this._description = description; this._canWrite = false; } get id () { return this._data.id; } get src () { return this._state.src; } async read () { this._data = readYAMLSync(this._state.configFile('data.yml')); if (!this._data.component) return; const component = readYAMLSync(this._data.component.config); this._frameworkIds = this._requestedFrameworkIds.filter(id => { if (component.excluded && component.excluded.includes(id)) { log.info(`Component ${this._data.id} excludes framework ${id}`); return false; } return true; }); if (!this._frameworkIds || this._frameworkIds.length === 0) { log.warn(`Part ${this._data.id} has no frameworks defined.`); return; } const spec = readYAMLSync(component.definition.spec); const input = readYAMLSync(component.definition.input); const template = readYAMLSync(component.definition.template); if (!spec || !input || !template) { log.warn(`Part ${this._data.id} is missing required definitions: ${ [spec || 'spec', input || 'input', template || 'template'].filter(d => typeof d === 'string').join(', ') }`); return; } this._definition = new Definition(component.definition.id, component.definition.path); this._definition.addDescription(this._description); this._definition.addSpec(spec); this._definition.addInput(input); this._definition.addTemplate(template); if (component.definition.inclusion) { const inclusion = readYAMLSync(component.definition.inclusion); this._definition.addInclusion(inclusion); } if (component.definition.computation) { const computation = readYAMLSync(component.definition.computation); this._definition.addComputation(computation); } if (component.definition.stories) { const stories = readYAMLSync(component.definition.stories); this._definition.addStories(stories); } this._canWrite = true; } async write () { if (!this._canWrite) return; if (!getComposers) { log.warn('Weave dependency is not available. Composer cannot be used.'); return; } for (const frameworkId of this._frameworkIds) { const composers = getComposers(frameworkId, this._definition); if (composers.length === 0) { log.warn(`No composer found for framework ${frameworkId} in part ${this._data.id}`); continue; } for (const composer of composers) { const renderers = composer.renderers; if (!renderers || renderers.length === 0) { log.warn(`No renderers found for framework ${frameworkId} in part ${this._data.id}`); continue; } for (const renderer of renderers) { let outputs; try { outputs = (await renderer.output()).filter(Boolean); } catch (err) { log.error(`Error while rendering with renderer ${renderer.constructor.name} for framework ${frameworkId} in part ${this._data.id}`); console.log(err); } if (!outputs) continue; if (outputs.length === 0) continue; for (const output of outputs) { if (!output || !output.filePath || !output.content) { log.warn(`Invalid output from renderer ${renderer.constructor.name} for framework ${frameworkId} in part ${this._data.id}`); continue; } const filePath = path.join(this._state.dest, composer.path, output.filePath); createFile(filePath, output.content); } } } } } } export { PartComposer };