From 80c6acd7b2d644257404e4e01e2468b2319622aa Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 26 May 2020 07:08:59 -0500 Subject: [PATCH] Core function ready --- .gitignore | 2 + cat-names/cat-names.json | 102 +++++++++++++++++++++++++++++++++++++++ cat-names/cli.js | 21 ++++++++ cat-names/index.js | 6 +++ cat-names/package.json | 39 +++++++++++++++ cli.js | 15 ++++++ index.js | 92 +++++++++++++++++++++++++++++++++++ package.json | 30 ++++++++++++ readmeprueba.md | 13 +++++ test/test.js | 10 ++++ 10 files changed, 330 insertions(+) create mode 100644 .gitignore create mode 100644 cat-names/cat-names.json create mode 100644 cat-names/cli.js create mode 100644 cat-names/index.js create mode 100644 cat-names/package.json create mode 100755 cli.js create mode 100644 index.js create mode 100644 package.json create mode 100644 readmeprueba.md create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..504afef81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +package-lock.json diff --git a/cat-names/cat-names.json b/cat-names/cat-names.json new file mode 100644 index 000000000..7d74330bf --- /dev/null +++ b/cat-names/cat-names.json @@ -0,0 +1,102 @@ +[ + "Abby", + "Angel", + "Annie", + "Baby", + "Bailey", + "Bandit", + "Bear", + "Bella", + "Bob", + "Boo", + "Boots", + "Bubba", + "Buddy", + "Buster", + "Cali", + "Callie", + "Casper", + "Charlie", + "Chester", + "Chloe", + "Cleo", + "Coco", + "Cookie", + "Cuddles", + "Daisy", + "Dusty", + "Felix", + "Fluffy", + "Garfield", + "George", + "Ginger", + "Gizmo", + "Gracie", + "Harley", + "Jack", + "Jasmine", + "Jasper", + "Kiki", + "Kitty", + "Leo", + "Lilly", + "Lily", + "Loki", + "Lola", + "Lucky", + "Lucy", + "Luna", + "Maggie", + "Max", + "Mia", + "Midnight", + "Milo", + "Mimi", + "Miss kitty", + "Missy", + "Misty", + "Mittens", + "Molly", + "Muffin", + "Nala", + "Oliver", + "Oreo", + "Oscar", + "Patches", + "Peanut", + "Pepper", + "Precious", + "Princess", + "Pumpkin", + "Rascal", + "Rocky", + "Sadie", + "Salem", + "Sam", + "Samantha", + "Sammy", + "Sasha", + "Sassy", + "Scooter", + "Shadow", + "Sheba", + "Simba", + "Simon", + "Smokey", + "Snickers", + "Snowball", + "Snuggles", + "Socks", + "Sophie", + "Spooky", + "Sugar", + "Tiger", + "Tigger", + "Tinkerbell", + "Toby", + "Trouble", + "Whiskers", + "Willow", + "Zoe", + "Zoey" +] \ No newline at end of file diff --git a/cat-names/cli.js b/cat-names/cli.js new file mode 100644 index 000000000..bd983564b --- /dev/null +++ b/cat-names/cli.js @@ -0,0 +1,21 @@ +#!/usr/bin/env node +'use strict'; + +var meow = require('meow'); +var catNames = require('./'); + +var cli = meow([ + 'Examples', + ' $ cat-names', + ' Max', + '', + ' $ cat-names --all', + ' Abby', + ' Angel', + ' ...', + '', + 'Options', + ' --all Get all names instead of a random name' +]); + +console.log(cli.flags.all ? catNames.all.join('\n') : catNames.random()); \ No newline at end of file diff --git a/cat-names/index.js b/cat-names/index.js new file mode 100644 index 000000000..ee0b97aa2 --- /dev/null +++ b/cat-names/index.js @@ -0,0 +1,6 @@ +'use strict'; +var uniqueRandomArray = require('unique-random-array'); +var catNames = require('./cat-names.json'); + +exports.all = catNames; +exports.random = uniqueRandomArray(catNames); \ No newline at end of file diff --git a/cat-names/package.json b/cat-names/package.json new file mode 100644 index 000000000..68fd63c7c --- /dev/null +++ b/cat-names/package.json @@ -0,0 +1,39 @@ +{ + "name": "cat-names", + "version": "1.0.0", + "description": "Get popular cat names", + "license": "MIT", + "repository": "sindresorhus/cat-names", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com" + }, + "bin": "cli.js", + "engines": { + "node": ">=0.10.0" + }, + "scripts": {}, + "files": [ + "index.js", + "cli.js", + "cat-names.json" + ], + "keywords": [ + "cli-app", + "cli", + "bin", + "array", + "random", + "rand", + "animal", + "cats", + "kitten", + "names" + ], + "dependencies": { + "chalk": "^4.0.0", + "meow": "^3.6.0", + "unique-random-array": "^1.0.0" + }, + "devDependencies": {} +} \ No newline at end of file diff --git a/cli.js b/cli.js new file mode 100755 index 000000000..d86cfbe52 --- /dev/null +++ b/cli.js @@ -0,0 +1,15 @@ +#!/usr/bin/env node +'use strict'; + +const meow = require('meow'); +var mdLinks = require('./index.js'); + +var mdLinks = meow([ + 'Arguments', + ' --file Write the path of the file you would like to check', + "Options", + "--validate Tells you the status of the founded link", + "--stats Gives you some basic statistics about the foundes links, as how many broken, working and total links." +]); + +// console.log(cli.flags.all ? catNames.all.join('\n') : catNames.random()); \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 000000000..a7405153c --- /dev/null +++ b/index.js @@ -0,0 +1,92 @@ +'use strict'; +//Dependencias +const fs = require('fs'); +const path = require("path"); +const fetch = require('node-fetch'); +const chalk = require('chalk'); +const log = console.log; + +let uri = ""; +let content = ""; + +function getPath() { + let index = process.argv.indexOf("--file") + if (index < 0) { return log(chalk.bgRed("Por favor ingresa el argumento --file seguido de la ruta del archivo Markdown.")) } + let uri = process.argv[index + 1] + let extension = path.extname(uri) + // log(chalk.black.bgYellowBright("extension:", extension)) + if (extension != '.md') { + log(chalk.bgRed("Por favor ingresa la ruta de un archivo Markdown (extensión .md)")); + } else { + return uri + } +} + +uri = getPath(); + +function readFile(uri) { + let content = fs.readFileSync(uri, "utf8") + return content +} + +// Falta promesa en readfile, + +content = readFile(uri) +//log(chalk.bgCyan("text content: ", content)); + +function searchLinks(content) { + let regEx = (/https?:\S+\w/g); + let links = content.match(regEx) + return links +} + +let links = searchLinks(content) + +log(chalk.cyan("Founded links :", links)); +let totalLinks = links.length; +log(chalk.black.bgCyan("total links:", totalLinks)); + +let arrayOfBrokenLinks = []; +let arrayOfCorrectLinks = []; + +function verifyLinks(links) { + // let indexVal = process.argv.indexOf("--validate") + // if (indexVal < 0) { + links.forEach(link => { + fetch(link) + .then(res => { + let status = res.status + if (status === 200) { + arrayOfCorrectLinks.push(link) + log(chalk.bgGreen(link, "Status OK: ", status)) + } + else { + arrayOfBrokenLinks.push(link) + log(chalk.bgRed(link, "Broken link: ", status)) + } + } + ) + + }) +} +// } + +verifyLinks(links) + +log(chalk.cyan("Total links:", totalLinks)); +log(chalk.bgGreen("Working links: ", arrayOfCorrectLinks)); +log(chalk.bgRedBright("Broken links: ", arrayOfBrokenLinks)); + + +// log("status: ", verifyLinks(links)) + + +// function statistics(links) { + +// let total = links.lenght +// log(chalk.cyan("Total links:", total)); +// log(chalk.bgGreen("Working links: ", arrayOfCorrectLinks.lenght)); +// log(chalk.bgRedBright("Broken links: ", arrayOfBrokenLinks.lenght)); +// } + +// statistics(links) \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 000000000..78d8fd854 --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "markdown-links", + "version": "1.0.0", + "description": "Get links from a markdown file and verify their status.", + "main": "index.js", + "scripts": { + "start": "node index.js", + "test": "ava" + }, + "keywords": [ + "cli-app", + "cli", + "links", + "markdown", + "verify", + "stats" + ], + "dependencies": { + "chalk": "^4.0.0", + "fs": ".0.1-secu0rity", + "meow": "^7.0.1", + "node-fetch": "^2.6.0", + "path": "^0.12.7", + "yargs": "^15.3.1" + }, + "devDependencies": { + "ava": "^3.8.2", + "jest": "^26.0.1" + } +} diff --git a/readmeprueba.md b/readmeprueba.md new file mode 100644 index 000000000..6e2ed66af --- /dev/null +++ b/readmeprueba.md @@ -0,0 +1,13 @@ + +Tópicos: [Node.js](https://nodejs.org/en/), +[módulos (CommonJS)](https://nodejs.org/docs/latest-v0.10.x/api/modules.html), +[file system](https://nodejs.org/api/fs.html), +[path](https://nodejs.org/api/path.html), +[http.get](https://nodejs.org/api/http.html#http_http_get_options_callback), +parsing, +[markdown](https://daringfireball.net/projects/markdown/syntax), CLI, +[npm-scripts](https://docs.npmjs.com/misc/scripts), +[semver](https://semver.org/), ... + +[link roto](https://this-is-a-broken-link) +[Fake link](https://this-a-fake-link.com/) diff --git a/test/test.js b/test/test.js new file mode 100644 index 000000000..e49d7fa27 --- /dev/null +++ b/test/test.js @@ -0,0 +1,10 @@ +const test = require('ava'); + +test('foo', t => { + t.pass(); +}); + +test('bar', async t => { + const bar = Promise.resolve('bar'); + t.is(await bar, 'bar'); +}); \ No newline at end of file