Skip to content

Commit

Permalink
feat(cli): encode jsonl
Browse files Browse the repository at this point in the history
  • Loading branch information
xseman committed Oct 23, 2023
1 parent d70b611 commit 6694495
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ const model = decode(

## Encode

Encode JSON data from files and print the corresponding QR code. The file
argument should be a path to a JSON file.
Encode JSON or JSONL data from files and print the corresponding QR code.

```sh
npx bysquare --encode <file1> <file2> <fileN>
npx bysquare --encode file1.json file2.json...
npx bysquare --encode file.jsonl
```

## Decode
Expand Down
2 changes: 2 additions & 0 deletions docs/examples/cli/example.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ "invoiceId": "random-id", "payments": [{ "type": 1, "amount": 100.0, "bankAccounts": [{ "iban": "SK9611000000002918599669" }], "currencyCode": "EUR", "variableSymbol": "123" }] }
{ "invoiceId": "random-id", "payments": [{ "type": 1, "amount": 100.0, "bankAccounts": [{ "iban": "SK9611000000002918599669" }], "currencyCode": "EUR", "variableSymbol": "123" }] }
54 changes: 35 additions & 19 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const args = parseArgs({
},
encode: {
type: "boolean",
short: "e",
short: "e"
},
help: {
type: "boolean",
Expand All @@ -26,27 +26,45 @@ const args = parseArgs({
});

if (process.stdin.isTTY) {
/**
* Process multiple files if the encode option is used
*/
if (args.values.encode) {
if (args.positionals.length === 0) {
console.error("No files provided for encoding.");
process.exit(1);
}

for (const file of args.positionals) {
if (existsSync(file) === false) {
console.error(`File ${file} doesn't exist`);
process.exit(1);
}

if (
file.endsWith(".json") === false &&
file.endsWith(".jsonl") === false
) {
console.error(`Unsupported file format for ${file}`);
process.exit(1);
}

const data = readFileSync(file, "utf8");
const encoded = encode(JSON.parse(data));
console.log(encoded);
}
if (file.endsWith(".jsonl")) {
const lines = data.split('\n');
for (const line of lines) {
if (!line) continue;

process.exit(0);
const json = JSON.parse(line);
console.log(encode(json));
}
}

if (file.endsWith(".json")) {
console.log(encode(JSON.parse(data)));
}

process.exit(0);
}
}

/**
* Input string
*/
if (args.values.decode) {
const qrstring = args.values.decode;
console.log(JSON.stringify(decode(qrstring), null, 4));
Expand All @@ -62,7 +80,7 @@ if (process.stdin.isTTY) {
" bysquare - Simple Node.js library to generate and parse PAY bysquare standard",
"",
"SYNOPSIS",
" bysquare [OPTIONS]",
" bysquare [OPTIONS] [FILES...]",
"",
"DESCRIPTION",
" bysquare is a command-line tool that provides a simple Node.js library to generate ",
Expand All @@ -74,21 +92,19 @@ if (process.stdin.isTTY) {
" Decode the specified QR code string and print the corresponding JSON data.",
" The qrstring argument should be a valid QR code string.",
"",
" -e, --encode <file(s)>",
" -e, --encode",
" Encode JSON data from one or more files and print the corresponding QR code.",
" The file(s) argument should be a path to JSON file(s). You can specify multiple",
" files separated by spaces.",
"",
" -h, --help",
" Display the help message and exit.",
"",
"USAGE",
" Encoding JSON data from one or more files",
"",
` ${process.argv[1]} --encode <file1> <file2> ...`,
" The <file1>, <file2>, ... arguments should be the paths to the JSON files you want ",
" to encode. The tool will read each file, generate a QR code representing the JSON ",
" data, and print them.",
` ${process.argv[1]} --encode file1.json file2.json ...`,
" The file1.json, file2.json, ... arguments should be the paths to the JSON or JSONL",
" files you want to encode. The tool will read each file, generate a QR code representing",
" the JSON data, and print them.",
"",
" Decoding a QR code string",
"",
Expand Down

0 comments on commit 6694495

Please sign in to comment.