@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
110 lines (92 loc) • 2.89 kB
JavaScript
import { I18nParser } from './i18n/i18n-parser.js';
import { VersionParser } from './version/version-parser.js';
import { createFile, getPackagePath } from '@gouvfr/dsfr-forge';
import { State } from '../../../common/state/state.js';
import yaml from 'yaml';
import { ResourceParser } from './resource/resource-parser.js';
import { fragments } from '@gouvfr/dsfr-lore'
import { CONFIG_FOLDER, NEXUS, GITHUB_EDIT } from '../../../common/constants.js';
import path from 'path';
import fs from 'fs';
const STATIC_SRC = `${getPackagePath('@gouvfr/dsfr-lore')}src`;
const getGithubEdit = (major) => {
switch (major) {
case 1:
return GITHUB_EDIT;
default:
return `${GITHUB_EDIT}dsfr/`;
}
};
class StateParser extends State {
constructor () {
super();
this._isStatic = false;
}
async load () {
const pkgFile = fs.readFileSync(`${NEXUS}/package.json`, 'utf8');
const pkg = JSON.parse(pkgFile);
this._docVersion = pkg.version;
this._i18n = new I18nParser();
await this._i18n.load(this._src);
this._version = new VersionParser();
await this._version.load(this._root);
this._resource = new ResourceParser();
await this._resource.load(`${STATIC_SRC}/resource`, this._version.core, this._i18n.locales);
this._config = `${CONFIG_FOLDER}/${this._version.feature}`
this._apply();
}
_clone () {
const state = super._clone();
state._resource = this._resource;
state._isStatic = this._isStatic;
state._docVersion = this._docVersion;
return state;
}
statify (id) {
const state = this._clone();
state._src = `${STATIC_SRC}/parts/${id}`;
state._isStatic = true;
Object.freeze(state);
return state;
}
get editUrl () {
if (this._isStatic || !this._version.isCurrent) return null;
const match = /(?<src>src.*)$/.exec(this._src);
if (!match) return null;
const editUrl = new URL(match.groups.src, getGithubEdit(this.version.major));
return editUrl.href;
}
setAsCurrent () {
const clone = this._clone();
clone._version = this._version.setAsCurrent();
Object.freeze(clone);
return clone;
}
getResource () {
return this._resource.getResource(this._i18n.current);
}
get versionSegment () {
if (this._version.isCurrent) return fragments.getFragment(this._i18n.current.code, 'current.segment');
return this.version.text;
}
get docVersion () {
return this._docVersion;
}
get versionLabel () {
if (this._version.isCurrent) return 'current';
return this.version.text;
}
async write (map, partIds) {
const data = {
root: this._root,
i18n: this._i18n.data,
version: this._version.data,
map: {
[this.version.text]: map
},
partIds: partIds
}
createFile(`${this.config}/state.yml`, yaml.stringify(data));
}
}
export { StateParser };