-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·78 lines (72 loc) · 3 KB
/
index.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
#!/usr/bin/env node
const {promises:fs} = require('fs')
const csv = require('csvtojson')
const path = require('path')
const { program } = require('commander')
const QRCode = require('qrcode')
let jimp = require('jimp')
let QR_CODE_SIZE = 300
const getConstants= async function(options){
const QrFONT = options[0].label ? await jimp.loadFont(jimp.FONT_SANS_16_BLACK) : null
const chemin_qr = path.join(options[1], `${options[2]}` + '.png')
await QRCode.toFile(chemin_qr, `${options[0].prefix ? `${options[0].prefix}` : ''}${options[2]}`, {width:QR_CODE_SIZE})
return [QrFONT, chemin_qr, QRCode];}
program
.version('1.0.0').description('generate unique QR codes from a csv file')
/*csvPath is required for terminal*/
.arguments('<csvPath>')
.action(async (f, options) => {
const ids=new Array()
try {
// Get ids CSV
f = path.join(process.cwd(), f)
f = await csv({noheader:!!1}).fromFile(f)
f = f.map(jsonId=>jsonId.field1.replace(';',''))
//Create new output folder
let erreursQR=[], output_path = path.join(__dirname, options.dest)
await fs.mkdir(output_path, { recursive: true })
await Promise.all(
f.map(async (id_Qr) => {
const id = id_Qr
try {
// Charge police pour les libellés
const CONSTANTS= await getConstants([options, output_path,id_Qr])
const QrFONT = CONSTANTS[0]
const chemin_qr = CONSTANTS[1]
// Ending if label unneed
if(!options.label){
return false
}else {
const image = await jimp.read(chemin_qr)
const d=2
// Adding label on image
image.print(options.label ? await jimp.loadFont(jimp.FONT_SANS_16_BLACK) : null,
QR_CODE_SIZE/d - jimp.measureText(options.label ?
await jimp.loadFont(jimp.FONT_SANS_16_BLACK) : null, id_Qr)/d,
QR_CODE_SIZE-jimp.measureTextHeight(options.label ? await jimp.loadFont(jimp.FONT_SANS_16_BLACK) : null, id_Qr) - 8, id_Qr)
return image
.writeAsync(chemin_qr)
}
}catch(erreur){
// don't stop others
erreursQR.push(id)
console.error('QR Code failed', id, erreur)
return
}
ids.push(id)
}),
)
// Exit process sucessfuly
const s = Math.round(ids.length-erreursQR.length)
console.log(`✓QR codes generated, ${s} succeeded,${erreursQR.length} failed`);process.exit(0)
}catch (err) {
console.error('QR Code generation failed', err);process.exit(1)
}
})
// --dest is required by commander
program.requiredOption('-d, --dest <qrCodeDest>',
'path where all the generated QR codes will be stored')
.option('-p, --prefix <qrCodePrefix>',
'prefix that will be added to each id')
.option('-l, --label', 'Whether to add label or not to each QR code')
.parse(process.argv)