This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
forked from papnkukn/qrcode-svg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
117 lines (99 loc) · 3.05 KB
/
app.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var os = require('os');
var fs = require('fs');
var path = require('path');
var QRCode = require("./lib/qrcode.js");
//Default configuration
var config = {
verbose: process.env.NODE_VERBOSE == "true" || process.env.NODE_VERBOSE == "1"
};
//Command line interface
var args = process.argv.slice(2);
for (var i = 0; i < args.length; i++) {
switch (args[i]) {
case "--help":
help();
process.exit(0);
break;
case "--padding":
config.padding = parseFloat(args[++i]);
break;
case "--width":
config.width = parseFloat(args[++i]);
break;
case "--height":
config.height = parseFloat(args[++i]);
break;
case "--color":
config.color = args[++i];
break;
case "--background":
config.background = args[++i];
break;
case "--ecl":
config.ecl = args[++i];
break;
case "-f":
config.force = true;
break;
case "-o":
config.outputFile = args[++i];
break;
case "-v":
console.log(require('./package.json').version);
process.exit(0);
break;
default:
if (i == args.length - 1) {
config.content = args[i];
}
else {
console.error("Unknown command line argument: " + args[i]);
process.exit(2);
}
break;
}
}
//Prints help message
function help() {
console.log("Usage:");
console.log(" qrcode-svg [options] <content>");
console.log("");
console.log("Options:");
console.log(" --help Print this message");
console.log(" --padding [value] Offset in number of modules");
console.log(" --width [px] Image width in pixels");
console.log(" --height [px] Image height in pixels");
console.log(" --color [color] Foreground color, hex or name");
console.log(" --background [color] Background color, hex or name");
console.log(" --ecl [value] Error correction level: L, M, H, Q");
console.log(" -o [file] Output file name");
console.log(" -f Force overwrite");
console.log(" -v Print version number");
console.log("");
console.log("Examples:");
console.log(" qrcode-svg http://github.com");
console.log(" qrcode-svg -f -o hello.svg \"Hello World\"");
console.log(" qrcode-svg --padding 2 --width 120 --height 120 \"Little fox...\"");
console.log(" qrcode-svg --color blue --background #ececec \"...jumps over\"");
}
if (args.length == 0) {
help();
process.exit(0);
}
if (typeof config.content != "string" || config.content.length == 0) {
console.error("Content is missing!");
process.exit(2);
}
var qrcode = new QRCode(config);
var svg = qrcode.svg();
if (typeof config.outputFile == "string" && config.outputFile.length > 0) {
if (!config.force && fs.existsSync(config.outputFile)) {
console.error("File already exists: " + config.outputFile);
process.exit(2);
}
fs.writeFileSync(config.outputFile, svg);
console.log("Done!");
}
else {
console.log(svg);
}