Skip to content

Instantly share code, notes, and snippets.

@GarthDB
Last active September 18, 2019 22:45
Show Gist options
  • Save GarthDB/cee30cc595585de3abfdd3c2194224df to your computer and use it in GitHub Desktop.
Save GarthDB/cee30cc595585de3abfdd3c2194224df to your computer and use it in GitHub Desktop.
Rework Metadata
const path = require('path');
const fs = require('fs');
const yaml = require('js-yaml');
const order = [
'id',
'name',
'dnaStatus',
'status',
'ignoreDNA',
'details',
'description',
'demoClassName',
'markup',
'examples'
];
function sortKeys(a, b) {
if (!order.includes(a)) console.log(a);
if (!order.includes(b)) console.log(b);
return order.indexOf(a) - order.indexOf(b);
}
function processDoc(doc, component) {
if (doc.hasOwnProperty('examples') || doc.hasOwnProperty('components')) {
if (doc.hasOwnProperty('examples')) {
doc.components = doc.examples;
delete doc.examples;
}
doc.examples = [];
Object.entries(doc.components).forEach(compAr => {
if(!compAr[1].hasOwnProperty('id')) compAr[1].id = compAr[0];
if(compAr[1].name == doc.name) compAr[1].name = 'Standard';
if (compAr[1].hasOwnProperty('ignoreDNA')) delete compAr[1].ignoreDNA;
if (!compAr[1].hasOwnProperty('name')) console.log(component);
doc.examples.push(compAr[1]);
});
delete doc.components;
}
else {
doc.examples = [{
name: 'Standard',
markup: doc.markup
}];
delete doc.markup;
}
// console.log(component);
if (doc.hasOwnProperty('ignoreDNA')) delete doc.ignoreDNA;
const docYaml = yaml.safeDump(doc, {
lineWidth: 10000000,
sortKeys: sortKeys,
condenseFlow: true
});
return docYaml;
}
const components = fs.readdirSync('components');
components.forEach(component => {
const componentPath = path.join('components', component);
const stat = fs.statSync(componentPath);
if (stat.isDirectory()) {
if (fs.existsSync(path.join(componentPath, 'docs.yml'))) {
const doc = yaml.safeLoad(fs.readFileSync(path.join(componentPath, 'docs.yml'), 'utf8'));
const docYaml = processDoc(doc, component);
fs.writeFileSync(path.join(componentPath, 'metadata.yml'), docYaml);
fs.writeFileSync(path.join('..', '..', 'spectrum-css', 'data', 'yml', `${component}.yml`), docYaml);
fs.unlinkSync(path.join(componentPath, 'docs.yml'));
}
else {
const docsDirPath = path.join(componentPath, 'docs');
if (fs.existsSync(docsDirPath)) {
const docs = fs.readdirSync(docsDirPath);
const metadataDir = path.join(componentPath, 'metadata');
if(!fs.existsSync(metadataDir)) fs.mkdirSync(metadataDir);
docs.forEach(docFileName => {
const doc = yaml.safeLoad(fs.readFileSync(path.join(docsDirPath, docFileName), 'utf8'));
const docYaml = processDoc(doc, component);
fs.writeFileSync(path.join(metadataDir, docFileName), docYaml);
fs.writeFileSync(path.join('..', '..', 'spectrum-css', 'data', 'yml', docFileName), docYaml);
fs.unlinkSync(path.join(docsDirPath, docFileName));
});
fs.rmdirSync(docsDirPath, {recursive: true});
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment