Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajuste, testes e es6 #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
[*.js]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,.travis.yml}]
[{package.json,*.yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
19 changes: 0 additions & 19 deletions .eslintrc

This file was deleted.

4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test
.editor*
.git*
.travis*
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sudo: false
language: node_js
node_js:
- '4.0.0'
branches:
only:
- ups
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# dev-util-cli
Command line to dev-util.
# dev-util-cli [![Build Status](https://travis-ci.org/lagden/dev-util-cli.svg?branch=ups)](https://travis-ci.org/lagden/dev-util-cli)

> Command line to dev-util.

## Installation

Expand Down Expand Up @@ -47,3 +48,13 @@ Generate data with default mask.

### -C, --clipboard:
Copy all generated data to clipboard.

## Contributors

- [Luís Fernando Vendrame](https://github.com/lvendrame)
- [Thiago Lagden](https://github.com/lagden)


## License

MIT © [Luís Fernando Vendrame](https://github.com/lvendrame)
15 changes: 0 additions & 15 deletions bin/cli.js

This file was deleted.

72 changes: 0 additions & 72 deletions bin/index.js

This file was deleted.

106 changes: 106 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env node

'use strict';

const cpf = require('dev-util/docs/pt-br/cpf');
const cnpj = require('dev-util/docs/pt-br/cnpj');
const creditCard = require('dev-util/docs/creditCard');
const clipboard = require('to-clipboard');
const chalk = require('chalk');
const cli = require('./lib/commander');

const red = chalk.bold.red;
const green = chalk.bold.green;
const blue = chalk.bold.blue;

let cliParam;

function fail(v) {
process.stdout.write(`${red('\u2716')} ${v}\n`);
process.exit(1);
}

function success(v) {
process.stdout.write(`${green('\u2713')} ${v}\n`);
process.exit();
}

function info(v) {
process.stdout.write(`${blue('\u2794')} ${v}\n`);
}

function getQuantity() {
if (cli.quantity) {
return cli.quantity;
}
return 1;
}

function getDocFunction() {
if (cli.cpf) {
return cpf;
} else if (cli.cnpj) {
return cnpj;
} else if (cli.creditCard) {
cliParam = cli.creditCard;
return creditCard;
}
}

function validateDoc(func) {
const c = cli.validate.replace(/\D/g, '').replace(/^0+/, '');
if (func.validate(c)) {
success('Valid');
} else {
fail('Invalid');
}
}

function generateDoc(func) {
const str = [];
let tmp;
for (let i = 0, len = getQuantity(); i < len; i++) {
tmp = cli.mask ? func.generateWithMask(cliParam) : func.generate(cliParam);
info(tmp);
str.push(tmp);
}

if (cli.clipboard) {
clipboard(str.join('\r\n'), err => {
if (err) {
fail('Fail when copy text to the clipboard');
} else {
success('All text was copied to the clipboard');
}
});
} else {
process.exit();
}
}

function validate() {
validateDoc(getDocFunction());
}

function generate() {
const docFunc = getDocFunction();
if (docFunc) {
generateDoc(docFunc);
} else {
fail('Select an document type');
}
}

function execute() {
if (cli.validate) {
validate();
} else {
generate();
}
}

if (cli.rawArgs.length < 3) {
cli.help();
} else {
execute();
}
18 changes: 18 additions & 0 deletions lib/commander.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const program = require('commander');
const pkg = require('../package.json');

program
.description('Generate random documents, numbers, names, address, etc.')
.version(pkg.version)
.option('-f, --cpf', 'Select CPF')
.option('-j, --cnpj', 'Select CNPJ')
.option('-c, --creditCard <type>', 'Select Credit Card. Types: maestro, dinersclub, laser, jcb, unionpay, discover, mastercard, amex, visa')
.option('-q, --quantity <number>', 'Quantity of generate data', parseInt)
.option('-m, --mask', 'Add mask to')
.option('-v, --validate <document>', 'Validate document')
.option('-C, --clipboard', 'Copy text to clipboard')
.parse(process.argv);

module.exports = program;
34 changes: 24 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
{
"name": "dev-util-cli",
"version": "0.0.5",
"version": "1.0.0",
"description": "Command line interface to dev-util",
"main": "index.js",
"scripts": {
"lint": "eslint .",
"test": "npm run lint"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lvendrame/dev-util-cli.git"
},
"preferGlobal": true,
"bin": {
"dev-util": "./bin/index.js"
"dev-util": "./cli.js"
},
"keywords": [
"cli",
Expand All @@ -28,12 +23,31 @@
"url": "https://github.com/lvendrame/dev-util-cli/issues"
},
"homepage": "https://github.com/lvendrame/dev-util-cli#readme",
"engines": {
"node": ">=4.0.0"
},
"scripts": {
"pretest": "xo",
"test": "mocha --reporter spec --require should",
"start": "watch xo -d -u",
"prepublish": "npm test"
},
"xo": {
"envs": [
"node"
],
"esnext": true
},
"dependencies": {
"chalk": "^1.1.1",
"commander": "^2.8.1",
"copy-paste": "^1.1.3",
"dev-util": "0.0.3"
"dev-util": "0.0.3",
"to-clipboard": "^0.2.0"
},
"devDependencies": {
"eslint": "^1.3.1"
"mocha": "*",
"should": "*",
"watch": "^0.16.0",
"xo": "^0.9.0"
}
}
Loading