{"size": 4287, "ext": "js", "max_stars_repo_path": "lib/index.js", "max_stars_repo_name": "shakenetwork/specificity-graph", "max_stars_repo_stars_event_min_datetime": "2015-01-08T19:09:26.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-27T21:51:19.000Z", "max_issues_repo_path": "lib/index.js", "max_issues_repo_name": "jaosorio1013/specificity-graph", "max_issues_repo_issues_event_min_datetime": "2015-01-04T09:55:44.000Z", "max_issues_repo_issues_event_max_datetime": "2018-07-14T12:09:41.000Z", "max_forks_repo_path": "lib/index.js", "max_forks_repo_name": "jaosorio1013/specificity-graph", "max_forks_repo_forks_event_min_datetime": "2015-01-05T06:41:56.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-06T18:17:18.000Z"}
{"max_stars_count": 407, "max_issues_count": 11, "max_forks_count": 41, "avg_line_length": 28.0196078431, "max_line_length": 103, "alphanum_fraction": 0.6330767436}
'use strict';
/*
 * Node module wrapper for specificity-graph
 */

var fs     = require('fs'),
    path   = require('path'),
    mkdirp = require('mkdirp'),
    generateCssData = require('./generateCssData'),
    Promise = require('es6-promise').Promise;

/**
 * Generates CSS specificity data in JSON format,
 * as well as a html chart.
 * Creates a directory if the directory specified does not already exist
 * @param  {String}   directory Directoru
 * @param  {OBject}   data      Data to write
 * @param  {Function} cb        Success callback
 * @return {void}
 */
module.exports = function(directory, css, cb) {
  directory = directory || './specificity-graph';
  cb = cb || function(){};
  var json = JSON.stringify(generateCssData(css.toString()));

  // chainable promises
  createDirectory({
    'json': json,
    'directory':directory
  }).then(writeSpecificityJson, errorHandler)
    .then(addExampleFiles, errorHandler)
    .then(cb, errorHandler);

  function errorHandler( err ) {
    throw err;
  }
};

/**
 * Create directory to hold files.
 * @param  {Object}  options            Options data
 * @param  {String}  options.directory  Directory
 * @param  {Object}  options.json       JSON data.
 * @return {void}
 */
function createDirectory(options) {
  return new Promise(function(resolve, reject) {
    mkdirp( options.directory, function (err) {
      if(err && err.code !== 'EEXIST') {
        reject(err);
      } else {
        resolve(options);
      }
    });
  });
}

/**
 * Write the specificity JSON to file
 * @param  {Object}  options            Options data
 * @param  {String}  options.directory  Directory
 * @param  {Object}  options.json       JSON data.
 * @return {void}
 */
function writeSpecificityJson(options) {
  return new Promise(function(resolve, reject) {
    var dest = path.join(options.directory, 'specificity.json');
    var out = fs.createWriteStream(dest);
    out.write(new Buffer(options.json));
    out.end();
    resolve(options);
  });
}

/**
 * This method copy the required js/css files and also
 * inject json into html.
 * @param  {Object}  options            Options data
 * @param  {String}  options.directory  Directory
 * @param  {Object}  options.json       JSON data.
 * @return {void}
 */

function addExampleFiles(options) {


  return new Promise(function(resolve, reject) {
    var templateFile = path.join(__dirname, '../template/cli.html');
    var tasks = [];

    tasks.push(writeHTMLFile(options));
    // add necessary JS files
    tasks.push(copyFileToDir('specificity-graph-standalone.js', options.directory))
    tasks.push(copyFileToDir('example.js', options.directory));

    // make sure all these aync tasks are done.
    Promise.all(tasks).then(resolve,reject);
  });
}

/**
 * This method creates index.html with injected data.
 *
 * @param  {Object}  options            Options data
 * @param  {String}  options.directory  Directory
 * @param  {Object}  options.json       JSON data.
 * @return {void}
 */

function writeHTMLFile(options) {
  return new Promise(function(resolve, reject) {
    var templateFile = path.join(__dirname, '../template/cli.html');
     // read our template file
     fs.readFile(templateFile, 'utf8', function (err,data) {
      if (err) {
        reject(err);
      }
      // insert the generated json data
      var result = data.replace('{{ insertJSONHere }}', 'var embeddedJsonData = '+ options.json + ';');

      // write file to directory
      fs.writeFile(path.join(options.directory, 'index.html'), result, 'utf8', function (err) {
         if (err) {
          reject(err);
         }
         resolve(options.directory);
      });
    });
  });
}


/**
 * This function copy file from root directory
 * to provided directory
 * @param  {[type]} file      [description]
 * @param  {[type]} directory [description]
 * @return {[type]}           [description]
 */
function copyFileToDir(file, directory) {
  return new Promise(function(resolve, reject) {
      if(file.split(path.sep).length > 1) {
      mkdirp.sync(path.dirname(path.join(directory, file)));
    }

    fs
      .createReadStream(path.join(__dirname, '../' + file))
      .pipe(fs.createWriteStream(path.join(directory, file)))
      .on('finish', resolve)
      .on('error', reject);
  });
}
