🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@br-validators/cli

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@br-validators/cli - npm Package Compare versions

Comparing version
0.12.0-alpha.1
to
0.12.0-alpha.2
+9
dist/run-captured.d.ts
type CliRunResult = {
exitCode: number;
stdout: string;
stderr: string;
};
/** Run CLI with argv; capture stdout/stderr and exit code (browser-safe — no commander/process.exit). */
declare function runCaptured(argv: string[]): CliRunResult;
export { type CliRunResult, runCaptured };
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
// src/constants.ts
var SUPPORTED_TYPES = ["cnpj", "cpf", "cep", "telefone", "cnh", "renavam", "titulo-eleitor", "nfe-chave", "brcode", "placa", "pis-pasep", "pix", "boleto", "cartao", "ie"];
var EXIT = {
OK: 0,
INVALID: 1,
USAGE: 2
};
// src/commands/brcode.ts
import {
BRCODE_OFFICIAL_SOURCE_URL,
parseBrCode,
validateBrCode
} from "@br-validators/core";
function resolveInput(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function printBrCodeResult(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? {
ok: true,
value: result.value,
format: result.format,
merchantName: result.merchantName,
merchantCity: result.merchantCity,
...result.amount ? { amount: result.amount } : {},
...result.txid ? { txid: result.txid } : {},
...result.pixKey ? { pixKey: result.pixKey, pixKeyType: result.pixKeyType } : {},
...result.pixInitiationUrl ? { pixInitiationUrl: result.pixInitiationUrl } : {},
...options.source ? { source: options.source } : {}
} : { ok: false, code: result.code, message: result.message },
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push("valid: yes");
io.stdout.push(`merchantName: ${result.merchantName}`);
io.stdout.push(`merchantCity: ${result.merchantCity}`);
if (result.amount) {
io.stdout.push(`amount: ${result.amount}`);
}
if (result.txid) {
io.stdout.push(`txid: ${result.txid}`);
}
if (result.pixKey) {
io.stdout.push(`pixKey: ${result.pixKey}`);
io.stdout.push(`pixKeyType: ${result.pixKeyType}`);
}
if (result.pixInitiationUrl) {
io.stdout.push(`pixInitiationUrl: ${result.pixInitiationUrl}`);
}
if (options.source) {
io.stdout.push(`source: ${options.source}`);
}
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
return EXIT.INVALID;
}
function runBrCodeCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? BRCODE_OFFICIAL_SOURCE_URL : void 0;
const result = action === "validate" ? validateBrCode(input) : parseBrCode(input);
return printBrCodeResult(result, { json: options.json, quiet: options.quiet, source }, io);
}
function runBrCode(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput(value, options.file);
if (input === null) {
io.stderr.push("Missing BR Code payload. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runBrCodeCommand(action, input, options, io);
}
// src/commands/cep.ts
import {
CEP_OFFICIAL_SOURCE_URL,
formatCep,
stripCep,
validateCep
} from "@br-validators/core";
// src/output.ts
function printValidation(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? {
ok: true,
value: result.value,
format: result.format,
...options.source ? { source: options.source } : {}
} : { ok: false, code: result.code, message: result.message },
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(`valid: yes (${result.format})`);
io.stdout.push(`value: ${result.value}`);
if (options.source) {
io.stdout.push(`source: ${options.source}`);
}
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
return EXIT.INVALID;
}
function printNfeChaveValidation(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? {
ok: true,
value: result.value,
format: result.format,
parsed: result.parsed,
...result.uf ? { uf: result.uf } : {},
...options.source ? { source: options.source } : {}
} : { ok: false, code: result.code, message: result.message },
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(`valid: yes (${result.format})`);
io.stdout.push(`value: ${result.value}`);
io.stdout.push(`cUF: ${result.parsed.cUF}`);
io.stdout.push(`cnpj: ${result.parsed.cnpj}`);
io.stdout.push(`mod: ${result.parsed.mod}`);
if (result.uf) {
io.stdout.push(`uf: ${result.uf}`);
}
if (options.source) {
io.stdout.push(`source: ${options.source}`);
}
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
return EXIT.INVALID;
}
function printFormat(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(JSON.stringify(result, null, 2));
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(result.formatted);
return EXIT.OK;
}
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
return EXIT.INVALID;
}
function printStrip(value, options, io = { stdout: [] }) {
if (options.json) {
io.stdout.push(JSON.stringify({ stripped: value }, null, 2));
} else {
io.stdout.push(value);
}
return EXIT.OK;
}
// src/commands/cep.ts
function resolveInput2(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runCepCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? CEP_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validateCep(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatCep(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripCep(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runCep(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput2(value, options.file);
if (input === null) {
io.stderr.push("Missing CEP value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runCepCommand(action, input, options, io);
}
// src/commands/telefone.ts
import {
TELEFONE_OFFICIAL_SOURCE_URL,
formatTelefone,
stripTelefone,
validateTelefone
} from "@br-validators/core";
function resolveInput3(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function printTelefoneValidation(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? {
ok: true,
value: result.value,
tipo: result.tipo,
format: result.format,
...options.source ? { source: options.source } : {}
} : { ok: false, code: result.code, message: result.message },
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(`valid: yes (${result.tipo})`);
io.stdout.push(`value: ${result.value}`);
io.stdout.push(`format: ${result.format}`);
if (options.source) {
io.stdout.push(`source: ${options.source}`);
}
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
return EXIT.INVALID;
}
function runTelefoneCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? TELEFONE_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printTelefoneValidation(validateTelefone(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatTelefone(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripTelefone(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runTelefone(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput3(value, options.file);
if (input === null) {
io.stderr.push("Missing telephone value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runTelefoneCommand(action, input, options, io);
}
// src/commands/cnh.ts
import {
CNH_OFFICIAL_SOURCE_URL,
formatCnh,
stripCnh,
validateCnh
} from "@br-validators/core";
function resolveInput4(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runCnhCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? CNH_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validateCnh(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatCnh(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripCnh(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runCnh(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput4(value, options.file);
if (input === null) {
io.stderr.push("Missing CNH value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runCnhCommand(action, input, options, io);
}
// src/commands/renavam.ts
import {
RENAVAM_OFFICIAL_SOURCE_URL,
formatRenavam,
stripRenavam,
validateRenavam
} from "@br-validators/core";
function resolveInput5(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runRenavamCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? RENAVAM_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validateRenavam(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatRenavam(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripRenavam(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runRenavam(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput5(value, options.file);
if (input === null) {
io.stderr.push("Missing RENAVAM value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runRenavamCommand(action, input, options, io);
}
// src/commands/titulo-eleitor.ts
import {
TITULO_ELEITOR_OFFICIAL_SOURCE_URL,
formatTituloEleitor,
stripTituloEleitor,
validateTituloEleitor
} from "@br-validators/core";
function resolveInput6(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runTituloEleitorCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? TITULO_ELEITOR_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validateTituloEleitor(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatTituloEleitor(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripTituloEleitor(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runTituloEleitor(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput6(value, options.file);
if (input === null) {
io.stderr.push("Missing T\xEDtulo de Eleitor value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runTituloEleitorCommand(action, input, options, io);
}
// src/commands/nfe-chave.ts
import {
NFE_CHAVE_OFFICIAL_SOURCE_URL,
formatNfeChave,
parseNfeChave,
stripNfeChave,
validateNfeChave
} from "@br-validators/core";
function resolveInput7(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runNfeChaveCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? NFE_CHAVE_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validateNfeChave(input), { json: options.json, quiet: options.quiet, source }, io);
case "parse":
return printNfeChaveValidation(parseNfeChave(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatNfeChave(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripNfeChave(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runNfeChave(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput7(value, options.file);
if (input === null) {
io.stderr.push("Missing NF-e chave de acesso value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runNfeChaveCommand(action, input, options, io);
}
// src/commands/cnpj.ts
import {
CNPJ_OFFICIAL_SOURCE_URL,
formatCnpj,
stripCnpj,
validateCnpj
} from "@br-validators/core";
function resolveInput8(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runCnpjCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? CNPJ_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validateCnpj(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatCnpj(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripCnpj(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runCnpj(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput8(value, options.file);
if (input === null) {
io.stderr.push("Missing CNPJ value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runCnpjCommand(action, input, options, io);
}
// src/commands/cpf.ts
import {
CPF_OFFICIAL_SOURCE_URL,
formatCpf,
stripCpf,
validateCpf
} from "@br-validators/core";
function resolveInput9(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runCpfCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? CPF_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validateCpf(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatCpf(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripCpf(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runCpf(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput9(value, options.file);
if (input === null) {
io.stderr.push("Missing CPF value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runCpfCommand(action, input, options, io);
}
// src/commands/placa.ts
import {
PLACA_OFFICIAL_SOURCE_URL,
convertPlacaToMercosul,
formatPlaca,
stripPlaca,
validatePlaca
} from "@br-validators/core";
function resolveInput10(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runPlacaCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? PLACA_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validatePlaca(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatPlaca(input), { json: options.json, quiet: options.quiet }, io);
case "convert":
return printFormat(convertPlacaToMercosul(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripPlaca(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runPlaca(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput10(value, options.file);
if (input === null) {
io.stderr.push("Missing placa value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runPlacaCommand(action, input, options, io);
}
// src/commands/pis-pasep.ts
import {
PIS_PASEP_OFFICIAL_SOURCE_URL,
formatPisPasep,
stripPisPasep,
validatePisPasep
} from "@br-validators/core";
function resolveInput11(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function runPisPasepCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? PIS_PASEP_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printValidation(validatePisPasep(input), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatPisPasep(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripPisPasep(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runPisPasep(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput11(value, options.file);
if (input === null) {
io.stderr.push("Missing PIS/PASEP value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runPisPasepCommand(action, input, options, io);
}
// src/commands/pix.ts
import {
PIX_OFFICIAL_SOURCE_URL,
detectPixKeyType,
formatPixKey,
validatePixKey
} from "@br-validators/core";
function resolveInput12(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function printPixValidation(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? {
ok: true,
value: result.value,
keyType: result.keyType,
format: result.format,
...options.source ? { source: options.source } : {}
} : {
ok: false,
code: result.code,
message: result.message,
...result.keyType ? { keyType: result.keyType } : {}
},
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(`valid: yes (${result.keyType})`);
io.stdout.push(`value: ${result.value}`);
io.stdout.push(`format: ${result.format}`);
if (options.source) {
io.stdout.push(`source: ${options.source}`);
}
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
if (result.keyType) {
io.stderr.push(`keyType: ${result.keyType}`);
}
return EXIT.INVALID;
}
function printPixDetect(keyType, options, io = { stdout: [] }) {
if (options.json) {
io.stdout.push(JSON.stringify({ keyType }, null, 2));
} else if (!options.quiet) {
io.stdout.push(keyType);
}
return EXIT.OK;
}
function runPixCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? PIX_OFFICIAL_SOURCE_URL : void 0;
const validateOptions = options.type ? { type: options.type } : void 0;
switch (action) {
case "validate":
return printPixValidation(validatePixKey(input, validateOptions), { json: options.json, quiet: options.quiet, source }, io);
case "format":
return printFormat(formatPixKey(input, validateOptions), { json: options.json, quiet: options.quiet }, io);
case "detect":
return printPixDetect(detectPixKeyType(input), { json: options.json, quiet: options.quiet }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runPix(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput12(value, options.file);
if (input === null) {
io.stderr.push("Missing PIX key value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runPixCommand(action, input, options, io);
}
// src/commands/boleto.ts
import {
BOLETO_OFFICIAL_SOURCE_URL,
convertCodigoBarrasToLinhaDigitavel,
convertLinhaToCodigoBarras,
detectBoletoInputKind,
formatBoleto,
stripCodigoBarras,
stripLinhaDigitavel,
validateBoleto
} from "@br-validators/core";
function resolveInput13(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function printBoletoValidation(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? {
ok: true,
value: result.value,
inputKind: result.inputKind,
format: result.format,
situacao: result.situacao,
...options.source ? { source: options.source } : {}
} : {
ok: false,
code: result.code,
message: result.message,
...result.inputKind ? { inputKind: result.inputKind } : {}
},
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(`valid: yes (${result.inputKind})`);
io.stdout.push(`value: ${result.value}`);
io.stdout.push(`format: ${result.format}`);
io.stdout.push(`situacao: ${result.situacao}`);
if (options.source) {
io.stdout.push(`source: ${options.source}`);
}
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
if (result.inputKind) {
io.stderr.push(`inputKind: ${result.inputKind}`);
}
return EXIT.INVALID;
}
function printBoletoDetect(inputKind, options, io = { stdout: [] }) {
if (options.json) {
io.stdout.push(JSON.stringify({ inputKind }, null, 2));
} else if (!options.quiet) {
io.stdout.push(inputKind);
}
return EXIT.OK;
}
function printBoletoConvert(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? { ok: true, value: result.value, inputKind: result.inputKind, format: result.format } : { ok: false, code: result.code, message: result.message, ...result.inputKind ? { inputKind: result.inputKind } : {} },
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(result.value);
return EXIT.OK;
}
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
return EXIT.INVALID;
}
function printBoletoFormat(input, options, io = { stdout: [], stderr: [] }) {
return printFormat(formatBoleto(input), { json: options.json, quiet: options.quiet }, io);
}
function printBoletoStrip(input, options, io = { stdout: [] }) {
const kind = detectBoletoInputKind(input);
const stripped = kind === "codigo-barras" ? stripCodigoBarras(input) : stripLinhaDigitavel(input);
if (options.json) {
io.stdout.push(JSON.stringify({ stripped, inputKind: kind }, null, 2));
} else if (!options.quiet) {
io.stdout.push(stripped);
}
return EXIT.OK;
}
function runBoletoCommand(action, input, options, direction, io = { stdout: [], stderr: [] }) {
const source = options.source ? BOLETO_OFFICIAL_SOURCE_URL : void 0;
const validateOptions = options.kind ? { kind: options.kind } : void 0;
switch (action) {
case "validate":
return printBoletoValidation(validateBoleto(input, validateOptions), { json: options.json, quiet: options.quiet, source }, io);
case "detect":
return printBoletoDetect(detectBoletoInputKind(input), { json: options.json, quiet: options.quiet }, io);
case "convert": {
if (direction === "linha-to-barras") {
return printBoletoConvert(convertLinhaToCodigoBarras(input), { json: options.json, quiet: options.quiet }, io);
}
if (direction === "barras-to-linha") {
return printBoletoConvert(convertCodigoBarrasToLinhaDigitavel(input), { json: options.json, quiet: options.quiet }, io);
}
io.stderr.push("Missing convert direction. Use linha-to-barras or barras-to-linha.");
return EXIT.USAGE;
}
case "format":
return printBoletoFormat(input, { json: options.json, quiet: options.quiet }, io);
case "strip":
return printBoletoStrip(input, { json: options.json, quiet: options.quiet }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runBoleto(action, value, options, direction, io = { stdout: [], stderr: [] }) {
const input = resolveInput13(value, options.file);
if (input === null) {
io.stderr.push("Missing boleto value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runBoletoCommand(action, input, options, direction, io);
}
// src/commands/cartao.ts
import {
CARTAO_OFFICIAL_SOURCE_URL,
detectCardBrand,
formatCartaoCredito,
stripCartaoCredito,
validateCartaoCredito
} from "@br-validators/core";
function resolveInput14(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function printCartaoValidation(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? {
ok: true,
value: result.value,
brand: result.brand,
format: result.format,
...options.source ? { source: options.source } : {}
} : {
ok: false,
code: result.code,
message: result.message,
...result.brand ? { brand: result.brand } : {}
},
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(`valid: yes (${result.format})`);
io.stdout.push(`brand: ${result.brand}`);
io.stdout.push(`value: ${result.value}`);
if (options.source) {
io.stdout.push(`source: ${options.source}`);
}
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
if (result.brand) {
io.stderr.push(`brand: ${result.brand}`);
}
return EXIT.INVALID;
}
function printCartaoDetect(brand, options, io = { stdout: [] }) {
if (options.json) {
io.stdout.push(JSON.stringify({ brand }, null, 2));
} else if (!options.quiet) {
io.stdout.push(brand);
}
return EXIT.OK;
}
function runCartaoCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const source = options.source ? CARTAO_OFFICIAL_SOURCE_URL : void 0;
switch (action) {
case "validate":
return printCartaoValidation(validateCartaoCredito(input), { json: options.json, quiet: options.quiet, source }, io);
case "detect":
return printCartaoDetect(detectCardBrand(stripCartaoCredito(input)), { json: options.json, quiet: options.quiet }, io);
case "format":
return printFormat(formatCartaoCredito(input), { json: options.json, quiet: options.quiet }, io);
case "strip":
return printStrip(stripCartaoCredito(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runCartao(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput14(value, options.file);
if (input === null) {
io.stderr.push("Missing credit card PAN value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runCartaoCommand(action, input, options, io);
}
// src/commands/ie.ts
import {
formatIeProdutorRural,
formatInscricaoEstadual,
getIeOfficialSourceUrl,
getIeProdutorRuralOfficialSourceUrl,
IE_SUPPORTED_UFS,
isSpRuralIeInput,
stripIeSpRural,
stripInscricaoEstadual,
validateIeProdutorRural,
validateInscricaoEstadual
} from "@br-validators/core";
function resolveInput15(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function resolveUf(uf) {
if (uf === void 0) {
return null;
}
const normalized = uf.toUpperCase();
if (IE_SUPPORTED_UFS.includes(normalized)) {
return normalized;
}
return null;
}
function isSpRuralRoute(uf, input) {
return uf === "SP" && isSpRuralIeInput(input);
}
function printIeValidation(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(
JSON.stringify(
result.ok ? {
ok: true,
value: result.value,
uf: result.uf,
format: result.format,
...options.rural ? { produtorRural: true } : {},
...options.source ? { source: options.source } : {}
} : {
ok: false,
code: result.code,
message: result.message,
...result.uf ? { uf: result.uf } : {}
},
null,
2
)
);
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(`valid: yes (${result.uf})`);
if (options.rural) {
io.stdout.push("kind: produtor-rural");
}
io.stdout.push(`value: ${result.value}`);
io.stdout.push(`format: ${result.format}`);
if (options.source) {
io.stdout.push(`source: ${options.source}`);
}
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
if (result.uf) {
io.stderr.push(`uf: ${result.uf}`);
}
return EXIT.INVALID;
}
function runIeCommand(action, input, options, io = { stdout: [], stderr: [] }) {
const uf = resolveUf(options.uf);
if (!uf) {
io.stderr.push(`Missing or invalid --uf. Use one of: ${IE_SUPPORTED_UFS.join(", ")}.`);
return EXIT.USAGE;
}
const rural = isSpRuralRoute(uf, input);
const source = options.source ? rural ? getIeProdutorRuralOfficialSourceUrl() : getIeOfficialSourceUrl(uf) : void 0;
switch (action) {
case "validate":
return printIeValidation(
rural ? validateIeProdutorRural(uf, input) : validateInscricaoEstadual(input, { uf }),
{ json: options.json, quiet: options.quiet, source, rural },
io
);
case "format":
return printFormat(
rural ? formatIeProdutorRural(input) : formatInscricaoEstadual(input, { uf }),
{ json: options.json, quiet: options.quiet },
io
);
case "strip":
return printStrip(rural ? stripIeSpRural(input) : stripInscricaoEstadual(input), { json: options.json }, io);
default: {
const _exhaustive = action;
io.stderr.push(`Unknown action: ${_exhaustive}`);
return EXIT.USAGE;
}
}
}
function runIe(action, value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput15(value, options.file);
if (input === null) {
io.stderr.push("Missing IE value. Pass an argument or use --file.");
return EXIT.USAGE;
}
return runIeCommand(action, input, options, io);
}
// src/commands/detect.ts
import { detect } from "@br-validators/core";
function resolveInput16(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function printDetect(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(JSON.stringify(result, null, 2));
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push(`type: ${result.type}`);
io.stdout.push("valid: yes");
io.stdout.push(`value: ${result.value}`);
if (result.format) {
io.stdout.push(`format: ${result.format}`);
}
if (result.meta) {
io.stdout.push(`meta: ${JSON.stringify(result.meta)}`);
}
return EXIT.OK;
}
io.stderr.push(`type: ${result.type}`);
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
return EXIT.INVALID;
}
function runDetect(value, options, io = { stdout: [], stderr: [] }) {
const input = resolveInput16(value, options.file);
if (input === null) {
io.stderr.push("Missing value. Pass an argument or use --file.");
return EXIT.USAGE;
}
const uf = options.uf?.toUpperCase();
const result = detect(input, uf ? { uf } : {});
return printDetect(result, options, io);
}
// src/commands/sanitize.ts
import { sanitize } from "@br-validators/core";
var SANITIZABLE_TYPES = [
"cpf",
"cnpj",
"cep",
"placa",
"pis-pasep",
"telefone",
"cnh",
"renavam",
"titulo-eleitor",
"nfe-chave",
"boleto",
"cartao-credito",
"inscricao-estadual",
"inscricao-estadual-produtor-rural"
];
function isSanitizableType(type) {
return SANITIZABLE_TYPES.includes(type);
}
function resolveInput17(value, fileContent) {
const input = value ?? fileContent?.trim();
if (!input) {
return null;
}
return input;
}
function printSanitize(result, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(JSON.stringify(result, null, 2));
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (options.quiet) {
return result.ok ? EXIT.OK : EXIT.INVALID;
}
if (result.ok) {
io.stdout.push("valid: yes");
io.stdout.push(`value: ${result.value}`);
io.stdout.push(`fixes: ${result.fixes.join(", ")}`);
return EXIT.OK;
}
io.stderr.push("valid: no");
io.stderr.push(`code: ${result.code}`);
io.stderr.push(`message: ${result.message}`);
return EXIT.INVALID;
}
function runSanitize(type, value, options, io = { stdout: [], stderr: [] }) {
if (!isSanitizableType(type)) {
io.stderr.push(`Unsupported sanitize type: ${type}`);
return EXIT.USAGE;
}
const input = resolveInput17(value, options.file);
if (input === null) {
io.stderr.push("Missing value. Pass an argument or use --file.");
return EXIT.USAGE;
}
const uf = options.uf?.toUpperCase();
const result = sanitize(input, type, uf ? { uf } : {});
return printSanitize(result, options, io);
}
// src/commands/generate.ts
import { generate, isGeneratableCardBrand } from "@br-validators/core";
var GENERATABLE_TYPES = [
"cpf",
"cnpj",
"cep",
"placa",
"pis-pasep",
"renavam",
"cnh",
"telefone",
"cartao-credito",
"inscricao-estadual",
"titulo-eleitor"
];
function isGeneratableType(type) {
return GENERATABLE_TYPES.includes(type);
}
function buildGenerateOptions(options) {
const core = {};
if (options.masked) {
core.masked = true;
}
if (options.seed !== void 0) {
core.seed = options.seed;
}
if (options.format) {
core.format = options.format;
}
if (options.uf) {
core.uf = options.uf.toUpperCase();
}
if (options.brand && isGeneratableCardBrand(options.brand)) {
core.brand = options.brand;
}
return core;
}
function printGenerate(value, options, io = { stdout: [], stderr: [] }) {
if (options.json) {
io.stdout.push(JSON.stringify({ ok: true, value }, null, 2));
return EXIT.OK;
}
if (options.quiet) {
return EXIT.OK;
}
io.stdout.push(value);
return EXIT.OK;
}
function runGenerate(type, options, io = { stdout: [], stderr: [] }) {
if (!isGeneratableType(type)) {
io.stderr.push(`Unsupported generate type: ${type}`);
return EXIT.USAGE;
}
const value = generate(type, buildGenerateOptions(options));
return printGenerate(value, options, io);
}
// src/commands/list.ts
function listSupportedTypes(io = { stdout: [] }) {
for (const type of SUPPORTED_TYPES) {
io.stdout.push(type);
}
return 0;
}
// src/handlers.ts
function readInputFile(path, io) {
try {
const fsModule = __require("fs");
return fsModule.readFileSync(path, "utf8");
} catch {
io.stderr.push(`Cannot read file: ${path}`);
return null;
}
}
function handleListCli(io = { stdout: [], stderr: [] }) {
return listSupportedTypes(io);
}
function handleCnpjCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runCnpj(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleCpfCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runCpf(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleCepCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runCep(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleTelefoneCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runTelefone(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleCnhCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runCnh(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handlePlacaCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runPlaca(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handlePisPasepCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runPisPasep(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handlePixCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runPix(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
type: opts.type,
file: fileContent
},
io
);
}
function handleBoletoCli(action, value, opts, direction, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runBoleto(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
kind: opts.kind,
file: fileContent
},
direction,
io
);
}
function handleCartaoCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runCartao(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleCartaoCreditoCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runCartao(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleRenavamCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runRenavam(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleTituloEleitorCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runTituloEleitor(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleNfeChaveCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runNfeChave(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleBrCodeCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runBrCode(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
file: fileContent
},
io
);
}
function handleIeCli(action, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runIe(
action,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
source: Boolean(opts.source),
uf: opts.uf,
file: fileContent
},
io
);
}
function handleDetectCli(value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runDetect(
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
uf: opts.uf,
file: fileContent
},
io
);
}
function handleSanitizeCli(type, value, opts, io = { stdout: [], stderr: [] }) {
let fileContent;
if (opts.file) {
const content = readInputFile(opts.file, io);
if (content === null) {
return EXIT.USAGE;
}
fileContent = content;
}
return runSanitize(
type,
value,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
uf: opts.uf,
file: fileContent
},
io
);
}
function handleGenerateCli(type, opts, io = { stdout: [], stderr: [] }) {
return runGenerate(
type,
{
json: Boolean(opts.json),
quiet: Boolean(opts.quiet),
masked: Boolean(opts.masked),
format: opts.format,
seed: opts.seed,
uf: opts.uf,
brand: opts.brand
},
io
);
}
// src/argv-dispatch.ts
var STANDARD_ACTIONS = ["validate", "format", "strip"];
var NFE_ACTIONS = ["validate", "parse", "format", "strip"];
var BRCODE_ACTIONS = ["parse", "validate"];
var PLACA_ACTIONS = ["validate", "format", "strip", "convert"];
var PIX_ACTIONS = ["detect", "validate", "format"];
var BOLETO_CONVERT = ["linha-to-barras", "barras-to-linha"];
function usage(io, message) {
io.stderr.push(message);
return EXIT.USAGE;
}
function parseArgv(tokens) {
const positionals = [];
const opts = {};
for (let index = 0; index < tokens.length; index += 1) {
const token = tokens[index];
if (token === "--json") {
opts.json = true;
continue;
}
if (token === "-q" || token === "--quiet") {
opts.quiet = true;
continue;
}
if (token === "--source") {
opts.source = true;
continue;
}
if (token === "--masked") {
opts.masked = true;
continue;
}
if (token === "-f" || token === "--file") {
opts.file = tokens[index + 1];
index += 1;
continue;
}
if (token === "--uf") {
opts.uf = tokens[index + 1];
index += 1;
continue;
}
if (token === "--format") {
opts.format = tokens[index + 1];
index += 1;
continue;
}
if (token === "--seed") {
opts.seed = Number(tokens[index + 1]);
index += 1;
continue;
}
if (token === "--kind") {
opts.kind = tokens[index + 1];
index += 1;
continue;
}
if (token === "--type") {
opts.type = tokens[index + 1];
index += 1;
continue;
}
if (token.startsWith("-")) {
continue;
}
positionals.push(token);
}
return { positionals, opts };
}
function pickValue(rest) {
return rest.slice(1).join(" ") || void 0;
}
function dispatchStandard(rest, opts, io, handler) {
const action = rest[0];
if (!action || !STANDARD_ACTIONS.includes(action)) {
return usage(io, "Expected action: validate | format | strip");
}
const value = rest.slice(1).join(" ") || void 0;
return handler(action, value, opts, io);
}
function dispatchArgv(tokens, io) {
if (tokens.length === 0 || tokens.includes("--help") || tokens.includes("-h")) {
io.stdout.push("br-validators \u2014 100% open-source Brazilian document validators");
io.stdout.push("Usage: br-validators <command> ...");
io.stdout.push("Commands: list \xB7 cpf \xB7 cnpj \xB7 cep \xB7 telefone \xB7 cnh \xB7 renavam \xB7 titulo-eleitor \xB7 nfe-chave \xB7 brcode \xB7 placa \xB7 pis-pasep \xB7 pix \xB7 boleto \xB7 cartao \xB7 cartao-credito \xB7 ie \xB7 detect \xB7 sanitize \xB7 generate");
return EXIT.OK;
}
if (tokens.includes("--version") || tokens.includes("-V")) {
io.stdout.push("0.12.0-alpha.1");
return EXIT.OK;
}
const { positionals, opts } = parseArgv(tokens);
const [root, ...rest] = positionals;
if (!root) {
return usage(io, "Missing command");
}
switch (root) {
case "list":
return handleListCli(io);
case "cnpj":
return dispatchStandard(rest, opts, io, handleCnpjCli);
case "cpf":
return dispatchStandard(rest, opts, io, handleCpfCli);
case "cep":
return dispatchStandard(rest, opts, io, handleCepCli);
case "telefone":
return dispatchStandard(rest, opts, io, handleTelefoneCli);
case "cnh":
return dispatchStandard(rest, opts, io, handleCnhCli);
case "renavam":
return dispatchStandard(rest, opts, io, handleRenavamCli);
case "titulo-eleitor":
return dispatchStandard(rest, opts, io, handleTituloEleitorCli);
case "nfe-chave": {
const action = rest[0];
if (!action || !NFE_ACTIONS.includes(action)) {
return usage(io, "Expected action: validate | parse | format | strip");
}
const value = rest.slice(1).join(" ") || void 0;
return handleNfeChaveCli(action, value, opts, io);
}
case "brcode": {
const action = rest[0];
if (!action || !BRCODE_ACTIONS.includes(action)) {
return usage(io, "Expected action: parse | validate");
}
const value = rest.slice(1).join(" ") || void 0;
return handleBrCodeCli(action, value, opts, io);
}
case "placa": {
const action = rest[0];
if (!action || !PLACA_ACTIONS.includes(action)) {
return usage(io, "Expected action: validate | format | strip | convert");
}
const value = rest.slice(1).join(" ") || void 0;
return handlePlacaCli(action, value, opts, io);
}
case "pis-pasep":
return dispatchStandard(rest, opts, io, handlePisPasepCli);
case "pix": {
const action = rest[0];
if (!action || !PIX_ACTIONS.includes(action)) {
return usage(io, "Expected action: detect | validate | format");
}
const value = rest.slice(1).join(" ") || void 0;
return handlePixCli(action, value, opts, io);
}
case "boleto": {
const action = rest[0];
if (action === "convert") {
const direction = rest[1];
if (!direction || !BOLETO_CONVERT.includes(direction)) {
return usage(io, "Expected: boleto convert linha-to-barras|barras-to-linha <value>");
}
const value2 = rest.slice(2).join(" ") || void 0;
return handleBoletoCli("convert", value2, opts, direction, io);
}
if (!action || !["detect", "validate", "format", "strip"].includes(action)) {
return usage(io, "Expected action: detect | validate | format | strip | convert");
}
const value = pickValue(rest);
return handleBoletoCli(
action,
value,
opts,
void 0,
io
);
}
case "cartao": {
const action = rest[0];
if (!action || !["detect", "validate", "format", "strip"].includes(action)) {
return usage(io, "Expected action: detect | validate | format | strip");
}
const value = pickValue(rest);
return handleCartaoCli(action, value, opts, io);
}
case "cartao-credito":
return dispatchStandard(rest, opts, io, handleCartaoCreditoCli);
case "ie":
return dispatchStandard(
rest,
opts,
io,
(action, value, ieOpts, ioArg) => handleIeCli(action, value, ieOpts, ioArg)
);
case "detect":
return handleDetectCli(rest.join(" ") || void 0, opts, io);
case "sanitize":
return handleSanitizeCli(rest[0] ?? "", rest.slice(1).join(" ") || void 0, opts, io);
case "generate":
return handleGenerateCli(rest[0] ?? "", opts, io);
default:
return usage(io, `Unknown command: ${root}`);
}
}
// src/run-captured.ts
function runCaptured(argv) {
const io = { stdout: [], stderr: [] };
const tokens = argv[0] === "node" ? argv.slice(2) : argv[0] === "br-validators" ? argv.slice(1) : argv;
const exitCode = dispatchArgv(tokens, io);
return {
exitCode,
stdout: io.stdout.join("\n"),
stderr: io.stderr.join("\n")
};
}
export {
runCaptured
};
+22
-8
#!/usr/bin/env node
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});

@@ -6,5 +12,2 @@ // src/program.ts

// src/handlers.ts
import { readFileSync } from "fs";
// src/commands/brcode.ts

@@ -1181,3 +1184,3 @@ import {

// src/commands/generate.ts
import { generate } from "@br-validators/core";
import { generate, isGeneratableCardBrand } from "@br-validators/core";
var GENERATABLE_TYPES = [

@@ -1192,3 +1195,5 @@ "cpf",

"telefone",
"cartao-credito"
"cartao-credito",
"inscricao-estadual",
"titulo-eleitor"
];

@@ -1209,2 +1214,8 @@ function isGeneratableType(type) {

}
if (options.uf) {
core.uf = options.uf.toUpperCase();
}
if (options.brand && isGeneratableCardBrand(options.brand)) {
core.brand = options.brand;
}
return core;

@@ -1243,3 +1254,4 @@ }

try {
return readFileSync(path, "utf8");
const fsModule = __require("fs");
return fsModule.readFileSync(path, "utf8");
} catch {

@@ -1642,3 +1654,5 @@ io.stderr.push(`Cannot read file: ${path}`);

format: opts.format,
seed: opts.seed
seed: opts.seed,
uf: opts.uf,
brand: opts.brand
},

@@ -1831,3 +1845,3 @@ io

});
program.command("generate <type>").description("Generate synthetic valid test document").option("--json", "JSON output").option("-q, --quiet", "Exit code only").option("--masked", "Return masked/formatted output").option("--format <format>", "Format variant (numeric, alphanumeric, legacy, mercosul, celular, fixo)").option("--seed <number>", "Deterministic PRNG seed", (v) => Number(v)).action((type, opts) => {
program.command("generate <type>").description("Generate synthetic valid test document").option("--json", "JSON output").option("-q, --quiet", "Exit code only").option("--masked", "Return masked/formatted output").option("--format <format>", "Format variant (numeric, alphanumeric, legacy, mercosul, celular, fixo)").option("--seed <number>", "Deterministic PRNG seed", (v) => Number(v)).option("--uf <uf>", "State code (required for inscricao-estadual and titulo-eleitor)").option("--brand <brand>", "Card brand (visa, mastercard, amex, elo, hipercard)").action((type, opts) => {
const io = { stdout: [], stderr: [] };

@@ -1834,0 +1848,0 @@ process.exitCode = handleGenerateCli(type, opts, io);

{
"name": "@br-validators/cli",
"version": "0.12.0-alpha.1",
"version": "0.12.0-alpha.2",
"description": "CLI for @br-validators/core — CPF, CNPJ, NF-e, IE, PIX, boleto + detect/sanitize/generate",

@@ -39,5 +39,12 @@ "license": "MIT",

},
"exports": {
".": "./dist/index.js",
"./run-captured": {
"types": "./dist/run-captured.d.ts",
"default": "./dist/run-captured.js"
}
},
"dependencies": {
"commander": "^13.1.0",
"@br-validators/core": "0.12.0-alpha.1"
"@br-validators/core": "0.12.0-alpha.2"
},

@@ -44,0 +51,0 @@ "devDependencies": {