<commit_before>//!Defines the commands used in cli.

/*
 * c
 * https://github.com/rumpl/c
 *
 * Copyright (c) 2012 <NAME>
 * Licensed under the MIT license.
 */

"use strict";

// Dependencies
const pack = require("../package.json");
const helpers = require("./helpers");
const storage = require("./storage");
const path = require("path");
const fs = require("fs");
const colors = require("colors/safe");
const { trueCasePathSync } = require("true-case-path");

var commands = module.exports;

/**Lists all `.comment` files available within `.comments`.
 * @param {String} dir the relative filepath to a directory, the content of which will be listed.
 */
commands.list = function (dir) {
  //Checks if the path is invalid OR a directory - returns if so.
  if (!fs.existsSync(dir) || fs.statSync(dir).isFile()) {
    console.error("Please specify a valid directory.");
    return;
  }

  var comments, files;

  //If there is not a '.comments', pass in an empty array
  if (storage.exists(dir)) {
    comments = storage.loadComments(dir);
    files = storage.loadFiles(dir);
  } else {
    comments = [];
    files = storage.loadFiles(dir);
  }

  //Prints the files and their comments.
  helpers.printFileComments(files, comments, dir);
};

/** Lists only files with related `.comment` files.
 * @param {File} dir the current directory.
 */
commands.filteredList = function (dir) {
  if (!fs.existsSync(dir) || fs.statSync(dir).isFile()) {
    console.error("Please specify a valid directory.");
    return;
  }

  var comments, files;

  if (!storage.exists(dir)) {
    comments = [];
    files = storage.loadFiles(dir);
  } else {
    files = storage.loadFiles(dir);
    comments = storage.loadComments(dir);
  }

  helpers.printOnlyComments(files, comments);
};

/** Adds or overwrites a comment to a file.
 * @param {String} file The name of the file to add a relevant `.comment`.
 * @param {String} comment The comment to be written.
 */
commands.set = function (file, comment) {
  //Checks if the file is invalid
  if (!fs.existsSync(file)) {
    console.error("Please specify a valid directory or file.");
    return;
  }

  if (file != "./" && file != "../" && file != "." && file != "..") {
    const pathUpTo = path.resolve("./");
    const trueFile = trueCasePathSync(file, pathUpTo);
    file = trueFile.replace(pathUpTo, "").replace("/", "");
  }

  storage.set(file, comment);
  console.log(
    '"' +
      colors.cyan(comment) +
      '" was applied to "' +
      colors.cyan(file) +
      '" successfully.'
  );<commit_msg>Resolved a Windows path issues<commit_after>//!Defines the commands used in cli.

/*
 * c
 * https://github.com/rumpl/c
 *
 * Copyright (c) 2012 <NAME>
 * Licensed under the MIT license.
 */

"use strict";

// Dependencies
const pack = require("../package.json");
const helpers = require("./helpers");
const storage = require("./storage");
const path = require("path");
const fs = require("fs");
const colors = require("colors/safe");
const { trueCasePathSync } = require("true-case-path");

var commands = module.exports;

/**Lists all `.comment` files available within `.comments`.
 * @param {String} dir the relative filepath to a directory, the content of which will be listed.
 */
commands.list = function (dir) {
  //Checks if the path is invalid OR a directory - returns if so.
  if (!fs.existsSync(dir) || fs.statSync(dir).isFile()) {
    console.error("Please specify a valid directory.");
    return;
  }

  var comments, files;

  //If there is not a '.comments', pass in an empty array
  if (storage.exists(dir)) {
    comments = storage.loadComments(dir);
    files = storage.loadFiles(dir);
  } else {
    comments = [];
    files = storage.loadFiles(dir);
  }

  //Prints the files and their comments.
  helpers.printFileComments(files, comments, dir);
};

/** Lists only files with related `.comment` files.
 * @param {File} dir the current directory.
 */
commands.filteredList = function (dir) {
  if (!fs.existsSync(dir) || fs.statSync(dir).isFile()) {
    console.error("Please specify a valid directory.");
    return;
  }

  var comments, files;

  if (!storage.exists(dir)) {
    comments = [];
    files = storage.loadFiles(dir);
  } else {
    files = storage.loadFiles(dir);
    comments = storage.loadComments(dir);
  }

  helpers.printOnlyComments(files, comments);
};

/** Adds or overwrites a comment to a file.
 * @param {String} file The name of the file to add a relevant `.comment`.
 * @param {String} comment The comment to be written.
 */
commands.set = function (file, comment) {
  //Checks if the file is invalid
  if (!fs.existsSync(file)) {
    console.error("Please specify a valid directory or file.");
    return;
  }

  if (file != "./" && file != "../" && file != "." && file != "..") {
    const pathUpTo = path.resolve("./");
    const trueFile = trueCasePathSync(file, pathUpTo);
    file = trueFile.replace(pathUpTo, "").replace("/", "");
    file = trueFile.replace(pathUpTo, "").replace("\\", "");
  }

  storage.set(file, comment);
  console.log(
    '"' +
      colors.cyan(comment) +
      '" was applied to "' +
      colors.cyan(file) +
      '" successfully.'
  );
{'fraction_non_alphanumeric': 0.1611778380472685, 'fraction_numerical': 0.0015497869043006587, 'mean_word_length': 3.25990099009901, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6563282', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1572, 'n_words': 481}