@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
102 lines (83 loc) • 3.07 kB
JavaScript
import getClosestMatchingSchemaSummary from './api/getClosestMatchingSchemaSummary.js';
import renderSimpleType from './helpers/renderSimpleType.js';
export default function attributeCommand(req, res) {
res.caption(req.command.getLongName());
const destroySpinner = res.spinner('Looking up schemas...');
const schemaSummary = getClosestMatchingSchemaSummary(
req.fdt.editorRepository.path,
req.options.schema
);
let attributeName = null;
let elementName = null;
if (req.parameters.attribute.indexOf('@') >= 0) {
elementName = req.parameters.attribute.split('@')[0];
attributeName = req.parameters.attribute.split('@')[1];
} else {
attributeName = req.parameters.attribute;
}
const allAttributeSummaries = schemaSummary.getAllAttributes();
let attributeSummaries = allAttributeSummaries.filter(
(attr) => attr.localName === attributeName
);
destroySpinner();
res.debug(`Reading from ${schemaSummary.package}`);
res.debug(
`Found ${attributeSummaries.length} attribute definitions for attribute "${attributeName}" across ${allAttributeSummaries.length} attributes.`
);
if (elementName) {
res.debug(`Filtering definitions applicable to <${elementName}>.`);
attributeSummaries = attributeSummaries.filter((attributeSummary) =>
attributeSummary
.getElements()
.some(
(elementSummary) => elementSummary.localName === elementName
)
);
res.debug(
`Found ${attributeSummaries.length} attribute definitions applicable to <${elementName}>.`
);
}
if (!attributeSummaries.length) {
throw new res.ErrorWithSolution(
`Attribute "${attributeName}" is not registered.`,
'Check your input for typos, or run `fdt attributes` to get a list of all attributes.'
);
}
attributeSummaries.forEach(function (attributeSummary, index) {
res.caption(`Definition ${index + 1}`);
res.indent();
const props = {
LocalName: attributeSummary.localName,
'Namespace URI': attributeSummary.namespaceURI,
'Type localName': attributeSummary.typeLocalName,
'Type namespace': attributeSummary.typeNamespace || '-',
Use: attributeSummary.use || '-',
'Default value': attributeSummary.defaultValue || '-',
'Allowed values': attributeSummary.allowedValues.length
? attributeSummary.allowedValues.join(', ')
: '-',
Elements: attributeSummary.getElements().length
? attributeSummary
.getElements()
.map((elementSummary) => elementSummary.localName)
.join(', ')
: '-',
};
/* istanbul ignore else */
if (schemaSummary.version[0] === 1) {
delete props['Namespace URI'];
} else if (schemaSummary.version[0] === 2) {
delete props['Type localName'];
delete props['Type namespace'];
delete props['Allowed values'];
}
res.properties(props);
const attributeSimpleType = attributeSummary.getSimpleType();
// Old schemas do not have simpleTypes, so skip this entire section if it is not used in this schema
if (attributeSimpleType) {
res.caption('Simple type');
renderSimpleType(res, attributeSimpleType);
}
res.outdent();
});
};