-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·171 lines (146 loc) · 3.34 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
(function() {
'use strict';
const
DEFAULT_FOLDER_NAME = 'partkeepr-labels';
const
argv = require('yargs')
.alias('i', 'input')
.alias('o', 'output')
.alias('p', 'part-number')
.demand(['i'])
.default('o', DEFAULT_FOLDER_NAME)
.argv,
fs = require('fs'),
mkdirp = require('mkdirp'),
child_process = require('child_process'),
swig = require('swig');
let
input,
records = [];
function readInput() {
input = fs.readFileSync(argv.input);
}
function parseInput() {
const
STATE_START = 0,
STATE_STRING = 1,
STATE_QUOTED_STRING = 2,
STATE_QUOTE_ESCAPE = 3;
const
separator = ',';
let
lines = input.toString().split('\n'),
buffer,
record,
line,
state = STATE_START;
// I swear there are tons of libraries that
// do fancy streamed/piped processing and
// can process gigabytes of data using multiple
// threads but I haven't found a single working
// synchronous parser
for (let i = 0; i < lines.length; i++) {
line = lines[i];
if (line.trim() === '') {
continue;
}
record = [];
buffer = '';
state = STATE_START;
for (let j = 0; j < line.length; j++) {
switch (state) {
case STATE_START:
if (line[j] === separator) {
record.push(buffer);
} else if (line[j] === '"') {
state = STATE_QUOTED_STRING;
} else {
state = STATE_STRING;
buffer += line[j];
}
break;
case STATE_STRING:
if (line[j] === '"') {
state = STATE_QUOTE_ESCAPE;
} else if (line[j] === separator) {
state = STATE_START;
record.push(buffer);
buffer = '';
} else {
buffer += line[j];
}
break;
case STATE_QUOTE_ESCAPE:
if (line[j] === '"') {
buffer += '"';
state = STATE_QUOTED_STRING;
} else if (line[j] === separator) {
state = STATE_START;
record.push(buffer);
buffer = '';
}
break;
case STATE_QUOTED_STRING:
if (line[j] === '"') {
state = STATE_QUOTE_ESCAPE;
} else {
buffer += line[j];
}
break;
}
}
record.push(buffer);
records.push(record);
}
}
function saveHTML() {
let
labels = [];
const
nameIndex = records[0].indexOf('name'),
locationIndex = records[0].indexOf('storageLocation.name');
for (let i = records.length - 1; i > 0; i--) {
labels.push({
partName: records[i][nameIndex],
storageLocation: records[i][locationIndex],
barcode: i + '.svg'
});
}
const
result = swig.renderFile('./templates/page.html', {
labels: labels
});
mkdirp.sync(argv.output + '/images');
fs.writeFileSync(argv.output + '/index.html', result);
const
escape = (str) => {
return '\'' + str
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"') + '\'';
};
for (let i = records.length - 1; i > 0; i--) {
let
svg = child_process.execSync(
'zint --notext --directsvg -d ' +
escape(records[i][locationIndex])
).toString();
svg = svg.replace(
'version="1.1"',
'version="1.1" preserveAspectRatio="xMidYMid slice"'
);
fs.writeFileSync(argv.output + '/images/' + i + '.svg', svg);
}
}
function main() {
try {
readInput();
parseInput();
saveHTML();
} catch (e) {
console.error('Error: ' + e.message);
console.log(e.stack);
}
}
main();
})();