-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessQR.js
98 lines (88 loc) · 4.04 KB
/
processQR.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import QRCode from "qrcode";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
const prompt = async (query) => {
process.stdout.write(query);
return new Promise((resolve) =>
process.stdin.once("data", (data) => resolve(data.toString().trim()))
);
};
const isBatch = await prompt("Batch? (y/N): ");
const batch = isBatch.toLowerCase() === "y";
const dataSource = await prompt(batch ? "Enter the data source [json/CSV]: " : "URL: ");
const save = (await prompt("Save to file? (y/N): ")).toLowerCase() === "y";
if (batch) {
const source = dataSource.toLowerCase();
if (!["csv", "json"].includes(source)) {
throw new Error("Invalid data source: " + source);
}
const linkToData = await prompt("Enter the link to the data: ");
let data;
if (linkToData.startsWith("http://") || linkToData.startsWith("https://")) {
data = await (await fetch(linkToData)).text();
} else {
data = await readFileSync(linkToData, "utf-8");
}
if (!data) { throw new Error("No data found"); }
let headers;
if (source === "csv") {
const rows = data.split("\n");
headers = rows[0].split(",");
const result = [];
for (let i = 1; i < rows.length; i++) {
const obj = {};
const currentRow = rows[i].split(",");
for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = currentRow[j];
}
result.push(obj);
}
data = result;
}
if (source === "json") {
const result = JSON.parse(data);
if (!Array.isArray(result)) {
throw new Error("Invalid data. Array of objects expected.");
}
headers = Object.keys(result[0]);
data = result;
}
if (!Array.isArray(data) || !Array.isArray(headers) || !headers.length || !data.length) {
throw new Error("Invalid data. Array of objects expected.");
}
const format = await prompt("\n-------------\nEnter the format by which each batch item should uniquely modify.\n\ni.e- https://mydomain.com/$$<keyX>/$$<keyY>?$$<someOtherKey>=$$<someFinalKey>\n\nVariables are inserted: $$<key>\n\nFormat: ");
const group = await prompt("Enter the key by which the data should be grouped []: ");
const naming = await prompt("Enter the format which defines the naming convention for the files (use of variable keys $$<key> required): ");
const result = {};
const formatPresentKeys = headers.filter((key) => format.includes(`$$<${key}>`));
const namingPresentKeys = headers.filter((key) => naming.includes(`$$<${key}>`));
for (let i = 0; i < data.length; i++) {
const item = data[i];
const fileName = namingPresentKeys.reduce((acc, key) => acc.replace(`$$<${key}>`, item[key]), naming);
const qrCode = formatPresentKeys.reduce((acc, key) => acc.replace(`$$<${key}>`, item[key]), format);
const groupName = group ? item[group] : "default";
if (typeof result[groupName] !== "object") {
result[groupName] = { [fileName]: qrCode };
} else {
result[groupName][fileName] = qrCode;
}
}
if (save) {
writeFileSync("./Out/data.json", JSON.stringify(result, null, 2));
const folderName = (await prompt("Enter the folder name [QRCode]: ")) || "QRCode";
for (const groupName in result) {
if (!existsSync(`./Out/${folderName}/${groupName}`)) {
mkdirSync(`./Out/${folderName}/${groupName}`, { recursive: true });
}
for (const fileName in result[groupName]) {
await QRCode.toFile(`./Out/${folderName}/${groupName}/${fileName}.png`, result[groupName][fileName], { errorCorrectionLevel: "H" });
}
}
}
} else {
const qrCode = await QRCode.toString(dataSource, { type: "terminal", errorCorrectionLevel: "H" });
console.log(qrCode);
if (save) {
const filename = await prompt("Enter the filename: ");
await QRCode.toFile('./Out/' + filename + '.png', dataSource, { errorCorrectionLevel: "H" });
}
}