{"size": 1179, "ext": "js", "max_stars_repo_path": "src/main.js", "max_stars_repo_name": "abhisheksoni27/LexerJS", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/main.js", "max_issues_repo_name": "abhisheksoni27/LexerJS", "max_issues_repo_issues_event_min_datetime": "2018-01-07T18:50:26.000Z", "max_issues_repo_issues_event_max_datetime": "2018-01-09T00:13:39.000Z", "max_forks_repo_path": "src/main.js", "max_forks_repo_name": "abhisheksoni27/lexer.js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 22.6730769231, "max_line_length": 65, "alphanum_fraction": 0.6115351993}
const LCS = require("./longestCommonSequences");
const utility = require("./utility");
const tokenizer = require("./tokenizer");

const fs = require("fs-extra");
const chalk = require("chalk");

// For details, see index.js
const defaultOptions = {
  output: "json",
  saveTokens: false,
  fileName: "result"
};

function LCSFinder(files, options) {
  const TokensOfFiles = [];

  options = Object.assign({}, defaultOptions, options);

  files.forEach(file => {
    TokensOfFiles.push({
      name: file,
      tokens: []
    });
  });

  // Find tokens for each file
  TokensOfFiles.forEach((file, i) => {
    let fileString = fs.readFileSync(file.name).toString();
    console.log(
      `Processing ${chalk.red(file.name)}.js, File ${chalk.green(
        i + 1
      )} out of ${chalk.blue(TokensOfFiles.length)}`
    );
    file.tokens = tokenizer(fileString, false);

    if (options.saveTokens) {
      fs
        .writeFile(
          `${tokenDir}/${file.name.split("/").pop()}.json`,
          JSON.stringify(file.tokens)
        )
        .catch(err => console.log(err));
    }
  });

  const result = LCS(TokensOfFiles);
  return result;
}

module.exports = LCSFinder;
