Skip to content

Commit

Permalink
feat(cli): encode multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
xseman committed Oct 23, 2023
1 parent 333db4f commit d70b611
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
4 changes: 2 additions & 2 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 a file and print the corresponding QR code. The file
Encode JSON data from files and print the corresponding QR code. The file
argument should be a path to a JSON file.

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

## Decode
Expand Down
51 changes: 33 additions & 18 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { existsSync, readFileSync } from "node:fs";
import { parseArgs } from "node:util";
import process from "node:process";

import { encode } from "./encode.js";
import { decode } from "./decode.js";
Expand All @@ -14,8 +15,8 @@ const args = parseArgs({
short: "d"
},
encode: {
type: "string",
short: "e"
type: "boolean",
short: "e",
},
help: {
type: "boolean",
Expand All @@ -25,25 +26,37 @@ const args = parseArgs({
});

if (process.stdin.isTTY) {
/** json file */
/**
* Process multiple files if the encode option is used
*/
if (args.values.encode) {
const file = args.values.encode;
if (existsSync(file)) {
for (const file of args.positionals) {
if (existsSync(file) === false) {
console.error(`File ${file} doesn't exist`);
process.exit(1);
}

const data = readFileSync(file, "utf8");
console.log(encode(JSON.parse(data)));
} else {
console.error(`File ${file} doesn't exists`);
process.exit(1);
const encoded = encode(JSON.parse(data));
console.log(encoded);
}

process.exit(0);
}

/** input string */
/**
* Input string
*/
if (args.values.decode) {
const qrstring = args.values.decode;
console.log(JSON.stringify(decode(qrstring), null, 4));
process.exit(0);
}

if (args.values.help || Object.keys(args.values).length === 0) {
if (
args.values.help ||
Object.keys(args.values).length === 0
) {
console.log([
"NAME",
" bysquare - Simple Node.js library to generate and parse PAY bysquare standard",
Expand All @@ -61,19 +74,21 @@ 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>",
" Encode JSON data from a file and print the corresponding QR code.",
" The file argument should be a path to a JSON file.",
" -e, --encode <file(s)>",
" 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 a file",
" Encoding JSON data from one or more files",
"",
` ${process.argv[1]} --encode <file>`,
" The <file> argument should be the path to the JSON file you want to encode.",
" The tool will read the file, generate a QR code representing the JSON data",
` ${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.",
"",
" Decoding a QR code string",
"",
Expand Down

0 comments on commit d70b611

Please sign in to comment.