Last active
November 23, 2021 19:27
-
-
Save DaniilVysotskiy/623467d257e591a76c10ccbc4874e87a to your computer and use it in GitHub Desktop.
Barcode generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sourceFile should be *.txt file with strings like `MAC: F0989D000000` or `S/N: ############`, each on new line | |
const JsBarcode = require("jsbarcode"); | |
const { DOMImplementation, XMLSerializer } = require("xmldom"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const [, , sourceFile = 'payload.txt', targetPath = '', limit = 5] = process.argv; | |
const xmlSerializer = new XMLSerializer(); | |
const createSvg = (content = 'test', options = {}) => { | |
const document = new DOMImplementation().createDocument( | |
"http://www.w3.org/1999/xhtml", | |
"html", | |
null | |
); | |
const svgNode = document.createElementNS("http://www.w3.org/2000/svg", "svg"); | |
options = { | |
xmlDocument: document, | |
lineColor: "#000", | |
fontOptions: "bold", | |
font: "Arial Black", | |
fontSize: 40, | |
width:4, | |
height:120, | |
text: content, | |
...options, | |
}; | |
JsBarcode(svgNode, content, options); | |
return xmlSerializer.serializeToString(svgNode); | |
} | |
const createDir = (targetPath) => { | |
fs.mkdir(targetPath, { recursive: true }, (err) => { | |
if (err) throw err; | |
}); | |
} | |
const getName = (arr) => { | |
const prefix = arr[0]; | |
const value = arr[1]; | |
return prefix === 'MAC:' ? prefix + ' ' + value.match(/.{2}/g).join(':') : prefix + ' ' + value; | |
} | |
const createSvgsInLoop = (array, targetPath) => { | |
for (let index = 0; index < limit; index++) { | |
const splittedStr = array[index].split(' '); | |
const value = splittedStr[1]; | |
const name = getName(splittedStr); | |
const svgDefault = createSvg(value, { text: name, height: 50, fontSize: 40 }); | |
const svgShort = createSvg(value, { text: name, height: 40, fontSize: 40 }); | |
const svgBig = createSvg(value, { text: name, height: 200, fontSize: 50 }); | |
const fileName = name.replace('/', '').replace(':', ''); | |
const path1 = path.format({ dir: targetPath, name: fileName, ext: '.svg'}); | |
const path2 = path.format({ dir: targetPath, name: fileName + '_short', ext: '.svg'}); | |
const path3 = path.format({ dir: targetPath, name: fileName + '_big', ext: '.svg'}); | |
fs.writeFile(path1, svgDefault, (err) => { | |
if (err) throw(err); | |
}); | |
fs.writeFile(path2, svgShort, (err) => { | |
if (err) throw(err); | |
}); | |
fs.writeFile(path3, svgBig, (err) => { | |
if (err) throw(err); | |
}); | |
} | |
} | |
const array = fs.readFileSync(sourceFile).toString().split("\n"); | |
createDir(targetPath) | |
createSvgsInLoop(array, targetPath); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment