{"size": 1092, "ext": "js", "max_stars_repo_path": "create-domain-files.js", "max_stars_repo_name": "Acidburn0zzz/debugger-protocol-viewer", "max_stars_repo_stars_event_min_datetime": "2016-07-06T22:30:44.000Z", "max_stars_repo_stars_event_max_datetime": "2017-04-13T17:27:18.000Z", "max_issues_repo_path": "create-domain-files.js", "max_issues_repo_name": "Acidburn0zzz/debugger-protocol-viewer", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "create-domain-files.js", "max_forks_repo_name": "Acidburn0zzz/debugger-protocol-viewer", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.6341463415, "max_line_length": 116, "alphanum_fraction": 0.7005494505}
'use strict';

/**
 * Utility command that creates HTML file in the _domains folder for each domain found in the _data/?/protocol.json.
 */

const fs = require('fs');
const DOMAINS_FOLDER = '_domains/';
const VERSIONS_FILE = '_data/versions.json';

const versionsText = fs.readFileSync(VERSIONS_FILE);
const versions = JSON.parse(versionsText);

versions.forEach(version => {
  clearFolder(`${DOMAINS_FOLDER}/${version.slug}`);
  generateDomainFiles(version);
});

function clearFolder(path) {
  fs.readdirSync(path).forEach((file, index) => fs.unlinkSync(`${path}/${file}`));
}

function generateDomainFiles(version) {
  const protocolFile = `_data/${version.slug}/protocol.json`;
  const protocolText = fs.readFileSync(protocolFile);
  const protocol = JSON.parse(protocolText);

  (protocol.domains).forEach((domain, idx) => {
    const name = domain.domain;
    const fileName = `${DOMAINS_FOLDER}/${version.slug}/${name}.html`;
    const content = `---
title: ${name}
category: ${version.slug}
version: ${version.name}
idx: ${idx}
---`;

    fs.writeFileSync(fileName, content);
  });
}
