@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
146 lines (119 loc) • 3.53 kB
JavaScript
import fs from 'fs';
import { createYAMLFile, log, readYAMLSync } from '@gouvfr/dsfr-forge';
let FRAMEWORK_IDS;
try {
const weave = await import('@gouvfr/dsfr-weave');
FRAMEWORK_IDS = weave.FRAMEWORK_IDS;
} catch (e) {
log.warn('Weave dependency is not available.');
FRAMEWORK_IDS = [];
}
class ComponentParser {
constructor(state, data) {
this._state = state;
this._data = data;
this._id = `fr-${data.id}`;
this._path = state.path;
this._configFile = this.state.configFile('component.yml');
}
get state () {
return this._state;
}
get src () {
return this._state.src;
}
get has () {
return this._has;
}
async read () {
this._has = false;
if (!fs.existsSync(this.src)) return;
const definitionPath = `${this.src}/definition`;
if (!fs.existsSync(definitionPath)) {
log.warn(`Component definition not found at ${definitionPath}`);
return;
}
if (this._data) {
if (this._data.hasOwnProperty('excluded')) {
this._excluded = this._data.excluded.filter(id => {
const has = FRAMEWORK_IDS.includes(id);
if (!has) {
log.warn(`Component ${this._data.id} has an unknown excluded framework ID: ${id}`);
return false;
}
return true;
});
}
}
const specPath = `${definitionPath}/component.spec.yml`;
if (!fs.existsSync(specPath)) {
log.error(`Component spec not found at ${specPath}`);
return;
}
// const spec = readYAMLSync(specPath);
this._definition = {
id: this._id,
path: this._path,
spec: specPath,
};
const descriptor = {
path: this._path,
excluded: this._excluded,
};
const inputPath = `${definitionPath}/props.input.yml`;
if (fs.existsSync(inputPath)) {
this._definition.input = inputPath;
const input = readYAMLSync(inputPath);
if (!input.hasOwnProperty('properties')) {
log.error(`Component props input at ${inputPath} must define a 'properties' property.`);
return;
}
descriptor.properties = Object.fromEntries(Object.entries(input.properties).map(([key, value]) => [key, {
type: value.type,
default: value.default,
enum: value.enum,
items: value.items,
$ref: value.$ref,
required: value.required
}]))
}
this._descriptor = {
[this._id]: descriptor
};
const templatePath = `${definitionPath}/template.ast.yml`;
if (!fs.existsSync(templatePath)) {
log.error(`Component template AST not found at ${templatePath}`);
return;
}
this._definition.template = templatePath;
this._has = true;
const inclusionPath = `${definitionPath}/inclusion.ast.yml`;
if (fs.existsSync(inclusionPath)) {
this._definition.inclusion = inclusionPath;
}
const computationPath = `${definitionPath}/props.computation.yml`;
if (fs.existsSync(computationPath)) {
this._definition.computation = computationPath;
}
const storiesPath = `${definitionPath}/stories.yml`;
if (fs.existsSync(storiesPath)) {
this._definition.stories = storiesPath;
}
}
get descriptor() {
return this._descriptor;
}
get data () {
return {
config: this._configFile
}
}
async write () {
const data = {
definition: this._definition,
excluded: this._excluded ?? []
}
createYAMLFile(this._configFile, data);
}
}
export { ComponentParser };