All files / src/md-Links cli.js

45.65% Statements 21/46
62.5% Branches 15/24
18.75% Functions 3/16
48.78% Lines 20/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106    1x               1x                                 1x                               1x                           1x 1x 1x 1x 1x   1x           1x 5x 1x   5x 4x         4x           4x         4x   44x           1x              
#!/usr/bin/env node
import {mdLinks} from './mdLinks'
const chalk = require('chalk');
// import {optionValidate} from './utils1'
// const validate = require('./index');
// import {optionValidate} from './utils1'
// import {linksStats, uniqueStats, brokenStats} from './stats.js';
 
 
 
export const optionStats = (arrLinks) => {
  const linksStats = arrLinks.length;
  const uniqueStats = new Set(arrLinks.map((element) => element.href)).size;
  if (linksStats === 0) {
    return 'Is 0 Links in this path! !';
  }
  const statsTemplate = `
    Mostrar Stats:
    Total: ${linksStats}
    Unique: ${uniqueStats}
      `;
  
  return statsTemplate;
};
 
// console.log(optionStats(prueba));
 
export const statsValidate = (arrLinks) => {
  const linksStats = arrLinks.length;
  const uniqueStats = new Set(arrLinks.map((element) => element.href)).size;
  const brokenStats = arrLinks.filter(href => href.status >= 400 || href.status === 'no status').length;
  // const brokenLinks = arrLinks.filter(link => link.status >= 400).length;
  const statsValidateTemplate = `
    Mostrar Stats Validate
    Total: ${linksStats}
    Unique: ${uniqueStats}
    Broken: ${brokenStats}
      `;
  return statsValidateTemplate;
};
// console.log(statsValidate(prueba));
 
 
const warning = `
    ${chalk.magenta('************************** Valid Arguments *************************')}
    ${chalk.magenta('Option 1:')} ${chalk.greenBright('md-links <path-to-file>')}
    ${chalk.magenta('Option 2:')} ${chalk.greenBright('md-links <path-to-file> --validate || -v')}
    ${chalk.magenta('Option 3:')} ${chalk.greenBright('md-links <path-to-file> --stast || -s')}
    ${chalk.magenta('Option 4:')} ${chalk.greenBright('md-links <path-to-file> --stast --validate || -s -v')}
    ${chalk.magenta('Option 5:')} ${chalk.greenBright('md-links <path-to-file> --validate --stast || -v -s')}
    ${chalk.magenta('********************************************************************')}
    ${chalk.magenta('--validate')} ${chalk.green('Para averiguar si el link funciona o no')}
    ${chalk.magenta('--stast')} ${chalk.green('Para mostrar estadísticas básicas sobre los links')}    
    ${chalk.magenta('--stast --validate')} ${chalk.green('Para obtener estadísticas de las validaciónes')}
    `;
 
 
const route = process.argv[2]; // segundo argumento 
const validate = process.argv.indexOf('--validate');
const optValidate = process.argv.indexOf('-v');// tercer argumento 
const stats = process.argv.indexOf('--stats');
const optStats = process.argv.indexOf('-s'); // cuarto argumento
// const combinado=process.argv.indexOf('-v -s')//quinta opcion 
const helps = process.argv.indexOf('--help');
// const args = process.argv[2];
// let options= process.argv[3];
// console.log('route',args);
// console.log('options',options);
 
export const funcionCli = (route) => {
  if (helps >= 0 || route === undefined) {
    console.log(warning);
  }
  if (route) {
    Iif ((stats >= 0 || optStats >= 0) && (validate >= 0 || optValidate >= 0)) {
     return mdLinks(route, { validate: true })
        .then((links) => console.log(chalk.green(statsValidate(links))))
        .catch((error) => console.log(chalk.bgRedBright.black(error)));
    }
    Iif (validate >= 0 || optValidate >= 0) {
      return mdLinks(route, { validate: true })
        // .then((links) => console.log(optionValidate(links)))
        .then((links) =>links.forEach(link =>console.log(chalk.green(link.file, link.href, link.statusText, link.status, link.text))))
        .catch((error) => console.log(warning));
    }
    Iif (stats >= 0 || optStats >= 0) {
      return mdLinks(route, { validate: false })
        .then((links) => console.log(chalk.blueBright(optionStats(links))))
        .catch((error) => console.error(error));
    } else {
      return mdLinks(route, { validate: false })
        // .then((links) => console.log(links))
        .then((links) =>links.forEach(link =>console.log(chalk.green(link.href,link.file,link.text))))
        .catch((error) => console.error(chalk.bgRedBright.black('Error')));
    }
  }
};
 
funcionCli(route);
 
 
//   console.log(process.argv);
// console.log('probando funcionCli', funcionCli('README.md','--stats'))
// funcionCli('./prueba').then((res) => console.log('este',res));
 
// console.log(funcionCli('README.md'))