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

54 lines (45 loc) 1.43 kB
import fs from 'fs'; import path from 'path'; import { ImageRefiner } from './image-refiner.js'; import { readYAMLSync } from '@gouvfr/dsfr-forge'; class PartCurator { constructor (name, state) { this._name = name; this._state = state; } get name () { return this._name; } async read () { const dataPath = path.join(this._state.config, this._name, 'data.yml'); if (!fs.existsSync(dataPath)) return; this._data = readYAMLSync(dataPath); this._images = await this.readImages(this._state); } async readImages (state) { const images = []; const entries = fs.readdirSync(state.src, { withFileTypes: true }); if (entries.length === 0) return images; for (const entry of entries) { if (entry.isDirectory()) { const childState = state.descend(entry.name); images.push(...await this.readImages(childState)); } else { if (!entry.name.match(/\.(svg|png|jpg|jpeg)/i)) continue; const imgState = state.setData({ src: path.join(state.src, entry.name), dest: `${this._data.src}/_part/doc/_asset/${path.relative(this._state.src, state.src)}/${entry.name}` }); const image = new ImageRefiner(entry.name, imgState); images.push(image); } } return images; } async write () { for (const image of this._images) { await image.write(); } } } export { PartCurator };