gerar 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/commands/cnpj/index.d.ts +2 -2
- package/dist/commands/cnpj/index.js +21 -10
- package/dist/commands/cnpj/index.js.map +1 -1
- package/dist/commands/cpf/index.d.ts +2 -2
- package/dist/commands/cpf/index.js +21 -10
- package/dist/commands/cpf/index.js.map +1 -1
- package/dist/index.js +2 -10
- package/dist/index.js.map +1 -1
- package/dist/shared/utils/command-builder.d.ts +12 -0
- package/dist/shared/utils/command-builder.js +21 -0
- package/dist/shared/utils/command-builder.js.map +1 -0
- package/dist/shared/utils/command-options.d.ts +21 -0
- package/dist/shared/utils/command-options.js +19 -0
- package/dist/shared/utils/command-options.js.map +1 -0
- package/dist/shared/utils/result-logger.d.ts +15 -0
- package/dist/shared/utils/result-logger.js +22 -0
- package/dist/shared/utils/result-logger.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/cnpj/index.ts +24 -17
- package/src/commands/cpf/index.ts +24 -17
- package/src/index.ts +4 -21
- package/src/shared/utils/command-builder.ts +40 -0
- package/src/shared/utils/command-options.ts +30 -0
- package/src/shared/utils/result-logger.ts +47 -0
- package/dist/shared/utils/command-structure.d.ts +0 -6
- package/dist/shared/utils/command-structure.js +0 -3
- package/dist/shared/utils/command-structure.js.map +0 -1
- package/src/shared/utils/command-structure.ts +0 -7
package/README.md
CHANGED
|
@@ -38,8 +38,10 @@ CPF 1) Com Máscara: 123.456.789-00 - Sem Máscara: 12345678900
|
|
|
38
38
|
| ------------------------------------------------- | ----------- |
|
|
39
39
|
| CPF | ✅ Completo |
|
|
40
40
|
| CNPJ | ✅ Completo |
|
|
41
|
+
| Flag nos comandos para gerar com ou sem máscara | ✅ Completo |
|
|
41
42
|
| Cartão de crédito com base na bandeira | ⏳ Pendente |
|
|
42
43
|
| Endereço | ⏳ Pendente |
|
|
44
|
+
| Chassi e Placa | ⏳ Pendente |
|
|
43
45
|
| UUID v4 | ⏳ Pendente |
|
|
44
46
|
| UUID v7 | ⏳ Pendente |
|
|
45
47
|
| Autocomplete no Terminal | ⏳ Pendente |
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
export declare const registerCnpjCommand: (program: Command) => Command;
|
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.registerCnpjCommand = void 0;
|
|
4
|
+
const command_builder_1 = require("../../shared/utils/command-builder");
|
|
4
5
|
const utils_1 = require("./utils");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
const result_logger_1 = require("../../shared/utils/result-logger");
|
|
7
|
+
const registerCnpjCommand = (program) => {
|
|
8
|
+
return (0, command_builder_1.buildCommand)(program, {
|
|
9
|
+
name: "cnpj",
|
|
10
|
+
description: "Gera um CNPJ fictício válido.",
|
|
11
|
+
action: (options) => {
|
|
12
|
+
const generatedCnpjs = Array.from({ length: options.amount ?? 1 }, utils_1.generateCnpj);
|
|
13
|
+
generatedCnpjs.forEach((generatedCnpj, index) => {
|
|
14
|
+
(0, result_logger_1.logResult)({
|
|
15
|
+
label: "CNPJ",
|
|
16
|
+
index,
|
|
17
|
+
showIndex: true,
|
|
18
|
+
result: generatedCnpj,
|
|
19
|
+
options,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
});
|
|
14
24
|
};
|
|
25
|
+
exports.registerCnpjCommand = registerCnpjCommand;
|
|
15
26
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/cnpj/index.ts"],"names":[],"mappings":";;;AACA,mCAAuC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/cnpj/index.ts"],"names":[],"mappings":";;;AACA,wEAAkE;AAClE,mCAAuC;AAEvC,oEAA6D;AAEtD,MAAM,mBAAmB,GAAG,CAAC,OAAgB,EAAE,EAAE;IACtD,OAAO,IAAA,8BAAY,EAAC,OAAO,EAAE;QAC3B,IAAI,EAAE,MAAM;QACZ,WAAW,EAAE,+BAA+B;QAC5C,MAAM,EAAE,CAAC,OAAuB,EAAE,EAAE;YAClC,MAAM,cAAc,GAAG,KAAK,CAAC,IAAI,CAC/B,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,CAAC,EAAE,EAC/B,oBAAY,CACb,CAAC;YAEF,cAAc,CAAC,OAAO,CAAC,CAAC,aAAa,EAAE,KAAK,EAAE,EAAE;gBAC9C,IAAA,yBAAS,EAAC;oBACR,KAAK,EAAE,MAAM;oBACb,KAAK;oBACL,SAAS,EAAE,IAAI;oBACf,MAAM,EAAE,aAAa;oBACrB,OAAO;iBACR,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;AACL,CAAC,CAAC;AArBW,QAAA,mBAAmB,uBAqB9B"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
export declare const registerCpfCommand: (program: Command) => Command;
|
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.registerCpfCommand = void 0;
|
|
4
|
+
const command_builder_1 = require("../../shared/utils/command-builder");
|
|
4
5
|
const utils_1 = require("./utils");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
const result_logger_1 = require("../../shared/utils/result-logger");
|
|
7
|
+
const registerCpfCommand = (program) => {
|
|
8
|
+
return (0, command_builder_1.buildCommand)(program, {
|
|
9
|
+
name: "cpf",
|
|
10
|
+
description: "Gera um CPF fictício válido.",
|
|
11
|
+
action: (options) => {
|
|
12
|
+
const generatedCpfs = Array.from({ length: options.amount ?? 1 }, utils_1.generateCpf);
|
|
13
|
+
generatedCpfs.forEach((generatedCpf, index) => {
|
|
14
|
+
(0, result_logger_1.logResult)({
|
|
15
|
+
label: "CPF",
|
|
16
|
+
index,
|
|
17
|
+
showIndex: true,
|
|
18
|
+
result: generatedCpf,
|
|
19
|
+
options,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
},
|
|
23
|
+
});
|
|
14
24
|
};
|
|
25
|
+
exports.registerCpfCommand = registerCpfCommand;
|
|
15
26
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/cpf/index.ts"],"names":[],"mappings":";;;AACA,mCAAsC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/cpf/index.ts"],"names":[],"mappings":";;;AACA,wEAAkE;AAClE,mCAAsC;AAEtC,oEAA6D;AAEtD,MAAM,kBAAkB,GAAG,CAAC,OAAgB,EAAE,EAAE;IACrD,OAAO,IAAA,8BAAY,EAAC,OAAO,EAAE;QAC3B,IAAI,EAAE,KAAK;QACX,WAAW,EAAE,8BAA8B;QAC3C,MAAM,EAAE,CAAC,OAAuB,EAAE,EAAE;YAClC,MAAM,aAAa,GAAG,KAAK,CAAC,IAAI,CAC9B,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,CAAC,EAAE,EAC/B,mBAAW,CACZ,CAAC;YAEF,aAAa,CAAC,OAAO,CAAC,CAAC,YAAY,EAAE,KAAK,EAAE,EAAE;gBAC5C,IAAA,yBAAS,EAAC;oBACR,KAAK,EAAE,KAAK;oBACZ,KAAK;oBACL,SAAS,EAAE,IAAI;oBACf,MAAM,EAAE,YAAY;oBACpB,OAAO;iBACR,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;AACL,CAAC,CAAC;AArBW,QAAA,kBAAkB,sBAqB7B"}
|
package/dist/index.js
CHANGED
|
@@ -11,15 +11,7 @@ program
|
|
|
11
11
|
.version("0.0.1");
|
|
12
12
|
program.helpCommand("help", "Exibe informações de ajuda do CLI.");
|
|
13
13
|
// TODO: Create automatic registration for commands.
|
|
14
|
-
program
|
|
15
|
-
|
|
16
|
-
.description(cpf_1.cpfCommand.description)
|
|
17
|
-
.option("-a, --amount <amount>", "Quantidade a ser gerado.", (value) => Number(value))
|
|
18
|
-
.action(cpf_1.cpfCommand.action);
|
|
19
|
-
program
|
|
20
|
-
.command(cnpj_1.cnpjCommand.name)
|
|
21
|
-
.description(cnpj_1.cnpjCommand.description)
|
|
22
|
-
.option("-a, --amount <amount>", "Quantidade a ser gerado.", (value) => Number(value))
|
|
23
|
-
.action(cnpj_1.cnpjCommand.action);
|
|
14
|
+
(0, cpf_1.registerCpfCommand)(program);
|
|
15
|
+
(0, cnpj_1.registerCnpjCommand)(program);
|
|
24
16
|
program.parse();
|
|
25
17
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AACA,yCAAoC;AACpC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AACA,yCAAoC;AACpC,wCAAoD;AACpD,0CAAsD;AAEtD,MAAM,OAAO,GAAG,IAAI,mBAAO,EAAE,CAAC;AAE9B,OAAO;KACJ,IAAI,CAAC,OAAO,CAAC;KACb,WAAW,CAAC,wDAAwD,CAAC;KACrE,OAAO,CAAC,OAAO,CAAC,CAAC;AAEpB,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,oCAAoC,CAAC,CAAC;AAElE,oDAAoD;AACpD,IAAA,wBAAkB,EAAC,OAAO,CAAC,CAAC;AAC5B,IAAA,0BAAmB,EAAC,OAAO,CAAC,CAAC;AAE7B,OAAO,CAAC,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { CommandBaseOptions } from "./command-options";
|
|
3
|
+
export type CommandStructure = {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
action: (this: Command, ...args: any[]) => void | Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
type CommandBuilderOptions = {
|
|
9
|
+
omitOptions?: CommandBaseOptions[];
|
|
10
|
+
};
|
|
11
|
+
export declare const buildCommand: (program: Command, command: CommandStructure, options?: CommandBuilderOptions) => Command;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildCommand = void 0;
|
|
4
|
+
const command_options_1 = require("./command-options");
|
|
5
|
+
const buildCommand = (program, command, options = {}) => {
|
|
6
|
+
const commandInstance = program
|
|
7
|
+
.command(command.name)
|
|
8
|
+
.description(command.description);
|
|
9
|
+
const baseOptionsKeys = Object.keys(command_options_1.commandBaseOptions).filter((option) => !options.omitOptions?.includes(option));
|
|
10
|
+
const baseOptions = baseOptionsKeys.map((option) => command_options_1.commandBaseOptions[option]);
|
|
11
|
+
baseOptions.forEach((option) => {
|
|
12
|
+
if ("validator" in option) {
|
|
13
|
+
commandInstance.option(option.flag, option.description, option.validator);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
commandInstance.option(option.flag, option.description);
|
|
17
|
+
});
|
|
18
|
+
return commandInstance.action(command.action);
|
|
19
|
+
};
|
|
20
|
+
exports.buildCommand = buildCommand;
|
|
21
|
+
//# sourceMappingURL=command-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-builder.js","sourceRoot":"","sources":["../../../src/shared/utils/command-builder.ts"],"names":[],"mappings":";;;AACA,uDAA2E;AAYpE,MAAM,YAAY,GAAG,CAC1B,OAAgB,EAChB,OAAyB,EACzB,UAAiC,EAAE,EACnC,EAAE;IACF,MAAM,eAAe,GAAG,OAAO;SAC5B,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC;SACrB,WAAW,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IAEpC,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,oCAAkB,CAAC,CAAC,MAAM,CAC5D,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,MAA4B,CAAC,CACzE,CAAC;IACF,MAAM,WAAW,GAAG,eAAe,CAAC,GAAG,CACrC,CAAC,MAAM,EAAE,EAAE,CAAC,oCAAkB,CAAC,MAA4B,CAAC,CAC7D,CAAC;IAEF,WAAW,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE;QAC7B,IAAI,WAAW,IAAI,MAAM,EAAE,CAAC;YAC1B,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC;YAC1E,OAAO;QACT,CAAC;QAED,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAC1D,CAAC,CAAC,CAAC;IAEH,OAAO,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;AAChD,CAAC,CAAC;AA1BW,QAAA,YAAY,gBA0BvB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const commandBaseOptions: {
|
|
2
|
+
readonly amount: {
|
|
3
|
+
readonly flag: "-a, --amount <amount>";
|
|
4
|
+
readonly description: "Quantidade a ser gerado.";
|
|
5
|
+
readonly validator: (value: string) => number;
|
|
6
|
+
};
|
|
7
|
+
readonly masked: {
|
|
8
|
+
readonly flag: "-m, --masked";
|
|
9
|
+
readonly description: "Gera o resultado com máscara.";
|
|
10
|
+
};
|
|
11
|
+
readonly unmasked: {
|
|
12
|
+
readonly flag: "-u, --unmasked";
|
|
13
|
+
readonly description: "Gera o resultado sem máscara.";
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export type CommandBaseOptions = keyof typeof commandBaseOptions;
|
|
17
|
+
export type CommandOptions = Partial<{
|
|
18
|
+
[Option in CommandBaseOptions]: (typeof commandBaseOptions)[Option] extends {
|
|
19
|
+
validator: (value: string) => infer ValidatorReturnType;
|
|
20
|
+
} ? ValidatorReturnType : boolean;
|
|
21
|
+
}>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.commandBaseOptions = void 0;
|
|
4
|
+
exports.commandBaseOptions = {
|
|
5
|
+
amount: {
|
|
6
|
+
flag: "-a, --amount <amount>",
|
|
7
|
+
description: "Quantidade a ser gerado.",
|
|
8
|
+
validator: (value) => Number(value),
|
|
9
|
+
},
|
|
10
|
+
masked: {
|
|
11
|
+
flag: "-m, --masked",
|
|
12
|
+
description: "Gera o resultado com máscara.",
|
|
13
|
+
},
|
|
14
|
+
unmasked: {
|
|
15
|
+
flag: "-u, --unmasked",
|
|
16
|
+
description: "Gera o resultado sem máscara.",
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=command-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-options.js","sourceRoot":"","sources":["../../../src/shared/utils/command-options.ts"],"names":[],"mappings":";;;AAMa,QAAA,kBAAkB,GAAG;IAChC,MAAM,EAAE;QACN,IAAI,EAAE,uBAAuB;QAC7B,WAAW,EAAE,0BAA0B;QACvC,SAAS,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC;KAC5C;IACD,MAAM,EAAE;QACN,IAAI,EAAE,cAAc;QACpB,WAAW,EAAE,+BAA+B;KAC7C;IACD,QAAQ,EAAE;QACR,IAAI,EAAE,gBAAgB;QACtB,WAAW,EAAE,+BAA+B;KAC7C;CAC0D,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CommandOptions } from "./command-options";
|
|
2
|
+
type ResultWithMask = {
|
|
3
|
+
withMask: string;
|
|
4
|
+
withoutMask: string;
|
|
5
|
+
};
|
|
6
|
+
type ResultWithoutMask = string;
|
|
7
|
+
type LogResultOptions = {
|
|
8
|
+
label: string;
|
|
9
|
+
index?: number;
|
|
10
|
+
showIndex?: boolean;
|
|
11
|
+
result: ResultWithMask | ResultWithoutMask;
|
|
12
|
+
options: CommandOptions;
|
|
13
|
+
};
|
|
14
|
+
export declare const logResult: ({ label, index, showIndex, result, options, }: LogResultOptions) => void;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.logResult = void 0;
|
|
4
|
+
const logResult = ({ label, index = 0, showIndex = false, result, options, }) => {
|
|
5
|
+
const indexText = showIndex ? ` ${index + 1})` : ":";
|
|
6
|
+
if (typeof result === "string") {
|
|
7
|
+
console.log(`${label}${indexText} ${result}`);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const { masked, unmasked } = options;
|
|
11
|
+
if (unmasked) {
|
|
12
|
+
console.log(`${label}${indexText} Sem Máscara: ${result.withoutMask}`);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (masked) {
|
|
16
|
+
console.log(`${label}${indexText} Com Máscara: ${result.withMask}`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
console.log(`${label}${indexText} Com Máscara: ${result.withMask} - Sem Máscara: ${result.withoutMask}`);
|
|
20
|
+
};
|
|
21
|
+
exports.logResult = logResult;
|
|
22
|
+
//# sourceMappingURL=result-logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result-logger.js","sourceRoot":"","sources":["../../../src/shared/utils/result-logger.ts"],"names":[],"mappings":";;;AAiBO,MAAM,SAAS,GAAG,CAAC,EACxB,KAAK,EACL,KAAK,GAAG,CAAC,EACT,SAAS,GAAG,KAAK,EACjB,MAAM,EACN,OAAO,GACU,EAAQ,EAAE;IAC3B,MAAM,SAAS,GAAG,SAAS,CAAC,CAAC,CAAC,IAAI,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;IAErD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,GAAG,SAAS,IAAI,MAAM,EAAE,CAAC,CAAC;QAC9C,OAAO;IACT,CAAC;IAED,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAErC,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,GAAG,SAAS,iBAAiB,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC;QACvE,OAAO;IACT,CAAC;IAED,IAAI,MAAM,EAAE,CAAC;QACX,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,GAAG,SAAS,iBAAiB,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;QACpE,OAAO;IACT,CAAC;IAED,OAAO,CAAC,GAAG,CACT,GAAG,KAAK,GAAG,SAAS,iBAAiB,MAAM,CAAC,QAAQ,mBAAmB,MAAM,CAAC,WAAW,EAAE,CAC5F,CAAC;AACJ,CAAC,CAAC;AA7BW,QAAA,SAAS,aA6BpB"}
|
package/package.json
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { buildCommand } from "../../shared/utils/command-builder";
|
|
2
3
|
import { generateCnpj } from "./utils";
|
|
4
|
+
import { CommandOptions } from "../../shared/utils/command-options";
|
|
5
|
+
import { logResult } from "../../shared/utils/result-logger";
|
|
3
6
|
|
|
4
|
-
export const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
generatedCnpjs.forEach((generatedCnpj, index) => {
|
|
14
|
-
console.log(
|
|
15
|
-
`CNPJ ${index + 1}) Com Máscara: ${
|
|
16
|
-
generatedCnpj.withMask
|
|
17
|
-
} - Sem Máscara: ${generatedCnpj.withoutMask}`
|
|
7
|
+
export const registerCnpjCommand = (program: Command) => {
|
|
8
|
+
return buildCommand(program, {
|
|
9
|
+
name: "cnpj",
|
|
10
|
+
description: "Gera um CNPJ fictício válido.",
|
|
11
|
+
action: (options: CommandOptions) => {
|
|
12
|
+
const generatedCnpjs = Array.from(
|
|
13
|
+
{ length: options.amount ?? 1 },
|
|
14
|
+
generateCnpj
|
|
18
15
|
);
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
|
|
17
|
+
generatedCnpjs.forEach((generatedCnpj, index) => {
|
|
18
|
+
logResult({
|
|
19
|
+
label: "CNPJ",
|
|
20
|
+
index,
|
|
21
|
+
showIndex: true,
|
|
22
|
+
result: generatedCnpj,
|
|
23
|
+
options,
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
});
|
|
21
28
|
};
|
|
@@ -1,21 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { buildCommand } from "../../shared/utils/command-builder";
|
|
2
3
|
import { generateCpf } from "./utils";
|
|
4
|
+
import { CommandOptions } from "../../shared/utils/command-options";
|
|
5
|
+
import { logResult } from "../../shared/utils/result-logger";
|
|
3
6
|
|
|
4
|
-
export const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
generatedCpfs.forEach((generatedCpf, index) => {
|
|
14
|
-
console.log(
|
|
15
|
-
`CPF ${index + 1}) Com Máscara: ${
|
|
16
|
-
generatedCpf.withMask
|
|
17
|
-
} - Sem Máscara: ${generatedCpf.withoutMask}`
|
|
7
|
+
export const registerCpfCommand = (program: Command) => {
|
|
8
|
+
return buildCommand(program, {
|
|
9
|
+
name: "cpf",
|
|
10
|
+
description: "Gera um CPF fictício válido.",
|
|
11
|
+
action: (options: CommandOptions) => {
|
|
12
|
+
const generatedCpfs = Array.from(
|
|
13
|
+
{ length: options.amount ?? 1 },
|
|
14
|
+
generateCpf
|
|
18
15
|
);
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
|
|
17
|
+
generatedCpfs.forEach((generatedCpf, index) => {
|
|
18
|
+
logResult({
|
|
19
|
+
label: "CPF",
|
|
20
|
+
index,
|
|
21
|
+
showIndex: true,
|
|
22
|
+
result: generatedCpf,
|
|
23
|
+
options,
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
});
|
|
21
28
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command } from "commander";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { registerCpfCommand } from "./commands/cpf";
|
|
4
|
+
import { registerCnpjCommand } from "./commands/cnpj";
|
|
5
5
|
|
|
6
6
|
const program = new Command();
|
|
7
7
|
|
|
@@ -13,24 +13,7 @@ program
|
|
|
13
13
|
program.helpCommand("help", "Exibe informações de ajuda do CLI.");
|
|
14
14
|
|
|
15
15
|
// TODO: Create automatic registration for commands.
|
|
16
|
-
program
|
|
17
|
-
|
|
18
|
-
.description(cpfCommand.description)
|
|
19
|
-
.option(
|
|
20
|
-
"-a, --amount <amount>",
|
|
21
|
-
"Quantidade a ser gerado.",
|
|
22
|
-
(value: string) => Number(value)
|
|
23
|
-
)
|
|
24
|
-
.action(cpfCommand.action);
|
|
25
|
-
|
|
26
|
-
program
|
|
27
|
-
.command(cnpjCommand.name)
|
|
28
|
-
.description(cnpjCommand.description)
|
|
29
|
-
.option(
|
|
30
|
-
"-a, --amount <amount>",
|
|
31
|
-
"Quantidade a ser gerado.",
|
|
32
|
-
(value: string) => Number(value)
|
|
33
|
-
)
|
|
34
|
-
.action(cnpjCommand.action);
|
|
16
|
+
registerCpfCommand(program);
|
|
17
|
+
registerCnpjCommand(program);
|
|
35
18
|
|
|
36
19
|
program.parse();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { CommandBaseOptions, commandBaseOptions } from "./command-options";
|
|
3
|
+
|
|
4
|
+
export type CommandStructure = {
|
|
5
|
+
name: string;
|
|
6
|
+
description: string;
|
|
7
|
+
action: (this: Command, ...args: any[]) => void | Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type CommandBuilderOptions = {
|
|
11
|
+
omitOptions?: CommandBaseOptions[];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const buildCommand = (
|
|
15
|
+
program: Command,
|
|
16
|
+
command: CommandStructure,
|
|
17
|
+
options: CommandBuilderOptions = {}
|
|
18
|
+
) => {
|
|
19
|
+
const commandInstance = program
|
|
20
|
+
.command(command.name)
|
|
21
|
+
.description(command.description);
|
|
22
|
+
|
|
23
|
+
const baseOptionsKeys = Object.keys(commandBaseOptions).filter(
|
|
24
|
+
(option) => !options.omitOptions?.includes(option as CommandBaseOptions)
|
|
25
|
+
);
|
|
26
|
+
const baseOptions = baseOptionsKeys.map(
|
|
27
|
+
(option) => commandBaseOptions[option as CommandBaseOptions]
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
baseOptions.forEach((option) => {
|
|
31
|
+
if ("validator" in option) {
|
|
32
|
+
commandInstance.option(option.flag, option.description, option.validator);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
commandInstance.option(option.flag, option.description);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return commandInstance.action(command.action);
|
|
40
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type CommandBaseOptionsObject = {
|
|
2
|
+
flag: string;
|
|
3
|
+
description: string;
|
|
4
|
+
validator?: (value: string) => any;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const commandBaseOptions = {
|
|
8
|
+
amount: {
|
|
9
|
+
flag: "-a, --amount <amount>",
|
|
10
|
+
description: "Quantidade a ser gerado.",
|
|
11
|
+
validator: (value: string) => Number(value),
|
|
12
|
+
},
|
|
13
|
+
masked: {
|
|
14
|
+
flag: "-m, --masked",
|
|
15
|
+
description: "Gera o resultado com máscara.",
|
|
16
|
+
},
|
|
17
|
+
unmasked: {
|
|
18
|
+
flag: "-u, --unmasked",
|
|
19
|
+
description: "Gera o resultado sem máscara.",
|
|
20
|
+
},
|
|
21
|
+
} as const satisfies Record<string, CommandBaseOptionsObject>;
|
|
22
|
+
|
|
23
|
+
export type CommandBaseOptions = keyof typeof commandBaseOptions;
|
|
24
|
+
export type CommandOptions = Partial<{
|
|
25
|
+
[Option in CommandBaseOptions]: (typeof commandBaseOptions)[Option] extends {
|
|
26
|
+
validator: (value: string) => infer ValidatorReturnType;
|
|
27
|
+
}
|
|
28
|
+
? ValidatorReturnType
|
|
29
|
+
: boolean;
|
|
30
|
+
}>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CommandOptions } from "./command-options";
|
|
2
|
+
|
|
3
|
+
type ResultWithMask = {
|
|
4
|
+
withMask: string;
|
|
5
|
+
withoutMask: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type ResultWithoutMask = string;
|
|
9
|
+
|
|
10
|
+
type LogResultOptions = {
|
|
11
|
+
label: string;
|
|
12
|
+
index?: number;
|
|
13
|
+
showIndex?: boolean;
|
|
14
|
+
result: ResultWithMask | ResultWithoutMask;
|
|
15
|
+
options: CommandOptions;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const logResult = ({
|
|
19
|
+
label,
|
|
20
|
+
index = 0,
|
|
21
|
+
showIndex = false,
|
|
22
|
+
result,
|
|
23
|
+
options,
|
|
24
|
+
}: LogResultOptions): void => {
|
|
25
|
+
const indexText = showIndex ? ` ${index + 1})` : ":";
|
|
26
|
+
|
|
27
|
+
if (typeof result === "string") {
|
|
28
|
+
console.log(`${label}${indexText} ${result}`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const { masked, unmasked } = options;
|
|
33
|
+
|
|
34
|
+
if (unmasked) {
|
|
35
|
+
console.log(`${label}${indexText} Sem Máscara: ${result.withoutMask}`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (masked) {
|
|
40
|
+
console.log(`${label}${indexText} Com Máscara: ${result.withMask}`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
console.log(
|
|
45
|
+
`${label}${indexText} Com Máscara: ${result.withMask} - Sem Máscara: ${result.withoutMask}`
|
|
46
|
+
);
|
|
47
|
+
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"command-structure.js","sourceRoot":"","sources":["../../../src/shared/utils/command-structure.ts"],"names":[],"mappings":""}
|