-
Notifications
You must be signed in to change notification settings - Fork 92
/
app.js
156 lines (133 loc) · 4.25 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
//Padding in number of modules
case "-p":
case "--padding":
config.padding = parseFloat(args[++i]);
break;
//Width in pixels
case "-w":
case "--width":
config.width = parseFloat(args[++i]);
break;
//Height in pixels
case "-h":
case "--height":
config.height = parseFloat(args[++i]);
break;
//Foreground color
case "-fg":
case "--color":
config.color = args[++i];
break;
//Background color
case "-bg":
case "--background":
config.background = args[++i];
break;
case "--ecl":
config.ecl = args[++i];
break;
case "--join":
config.join = true;
break;
case "--predefined":
config.predefined = true;
break;
case "--viewbox":
config.container = "svg-viewbox";
break;
case "--no-prettify":
config.pretty = false;
break;
case "--swap-fix":
config.swap = true;
break;
case "-f":
case "--force":
config.force = true;
break;
case "-o":
case "--output":
config.outputFile = args[++i];
break;
case "-v":
case "--version":
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(" --version, -v Print version number");
console.log(" --padding , -p [value] Offset in number of modules");
console.log(" --width, -w [px] Image width in pixels");
console.log(" --height, -h [px] Image height in pixels");
console.log(" --color, -fg [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(" --join Join modules into one SVG path, i.e. for crisp rendering");
console.log(" --predefined Use 'defs' and 'use' elements in SVG, i.e. for compact output");
console.log(" --no-prettify Avoid indenting and new lines in SVG, i.e. for compact output");
console.log(" --viewbox Use 'viewBox' instead of 'width' and 'height' attributes");
console.log(" --swap-fix Swap X and Y modules to fix issues with some QR readers");
console.log(" --output, -o [file] Output file name");
console.log(" --force, -f Force overwrite");
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 -p 4 -w 256 -h 256 --join --viewbox \"Responsive...\"");
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);
}