-
Notifications
You must be signed in to change notification settings - Fork 40
/
shc.js
37 lines (31 loc) · 1.04 KB
/
shc.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
/*
* Extract JSON payload from SHC QR code
* $ node shc.js "/path/to/qrcode.png"
*/
const fs = require("fs");
const mime = require("mime")
const { nanoid } = require("nanoid");
const { readQrCode, parseShc } = require("./src/parsers")
try {
const qrCodeFilePath = process.argv[2]
if (!qrCodeFilePath) throw new Error('Please provide the file path to the PNG QR code to decode.')
const mimeType = mime.getType(qrCodeFilePath)
if (mimeType !== 'image/png') throw new Error(`${qrCodeFilePath} is detected as ${mimeType}, please convert to image/png`)
const qrCodeData = readQrCode(fs.readFileSync(qrCodeFilePath));
parseShc(qrCodeData).then(extractedData => {
if (!fs.existsSync('./out')) {
fs.mkdirSync('./out');
}
const prettyJson = JSON.stringify(extractedData, null, 4)
const nanoId = nanoid(10);
fs.writeFile(`./out/${nanoId}.json`, prettyJson, (error) => {
if (error) {
console.log(error)
} else {
console.log(`JSON data was extracted to ./out/${nanoId}.json`);
}
});
});
} catch (e) {
console.log(e.message)
}