-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
284 lines (269 loc) · 9.22 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
const {shell, remote} = require('electron');
const {dialog, app} = remote;
const fs = require('fs');
const path = require('path');
const XLSX = require('xlsx');
const DEFAULT_NAMING_SCHEME = '<WORKBOOK> - <SHEET>.csv';
const RGX_META = /<(WORKBOOK|SHEET|SHEET_NUMBER|EXTENSION)>/g;
function recurseDirSync(currentDirPath, opt_filter) {
let result = {
isFile: false,
path: currentDirPath,
stat: fs.statSync(currentDirPath),
files: []
};
fs.readdirSync(currentDirPath).forEach(function (name) {
let filePath = path.join(currentDirPath, name),
stat = fs.statSync(filePath),
isFile = stat.isFile();
if ((isFile || stat.isDirectory()) && (!opt_filter || opt_filter(filePath, isFile, stat))) {
result.files.push(isFile ? { isFile: true, path: filePath, stat: stat } : recurseDirSync(filePath, opt_filter));
}
});
return result;
}
function ensureDirExists(dirPath) {
if (!fs.existsSync(dirPath)) {
ensureDirExists(path.dirname(dirPath));
fs.mkdirSync(dirPath);
}
}
window.recurseDirSync = recurseDirSync;
$(function() {
$('#app-title').text(document.title);
let myVue = new Vue({
el: '#myVue',
data: {
folderPath: null,
workbooks: [],
sheetFilters: [{ value: '', isRegExp: false, error: null }],
converting: false,
namingScheme: DEFAULT_NAMING_SCHEME,
DEFAULT_NAMING_SCHEME: DEFAULT_NAMING_SCHEME
},
watch: {
sheetFilters: {
deep: true,
handler() {
var i = this.sheetFilters.findIndex(f => !f.value);
if (this.sheetFilters.findIndex(f => !f.value) < 0) {
this.sheetFilters.push({ value: '', isRegExp: false, error: null });
}
}
}
},
computed: {
includedWorkbooks() {
return this.workbooks.filter(function(wb) {
return wb.include;
});
},
normalizedNamingScheme() {
return this.namingScheme.replace(/[\\\/]+/g, path.sep);
},
namingSchemeError() {
let namingScheme = this.normalizedNamingScheme;
return namingScheme.endsWith(path.sep)
? 'Cannot end with "' + path.sep + '".'
: /(^|[\\\/])\.\./.test(namingScheme)
? 'Cannot start with "..".'
: /[~#%&*\{\}:\?\+\|"]/.test(namingScheme)
? 'Cannot contain any of the following: ~ # & * { } : ? + |'
: /[<>]/.test(namingScheme.replace(RGX_META, ''))
? 'Cannot contain the less than (<) or greater than (>) characters except for when adding "<WORKBOOK>", "<SHEET>", and "<SHEET_NUMBER>".'
: !RGX_META.test(namingScheme)
? 'Must use at least one of the following: <WORKBOOK>, <SHEET>, <SHEET_NUMBER>, <EXTENSION>'
: false;
},
isValidNamingScheme() {
return !this.namingSchemeError;
},
hasInvalidFilters() {
return this.sheetFilters.filter(f => f.error).length > 0;
},
canConvert() {
let myVue = this;
return myVue.workbooks.length
&& myVue.normalizedNamingScheme
&& !myVue.namingSchemeError
&& !myVue.hasInvalidFilters
&& myVue.preConversions.filter(file => myVue.matchesFilters(file.sheetName)).length;
},
workbookSheets() {
let result = [],
myVue = this;
myVue.includedWorkbooks.forEach(objWB => {
let wb = XLSX.readFileSync(objWB.path),
sheetNumber = 0;
wb.Workbook.Sheets.forEach(sheetDetails => {
if (sheetDetails.Hidden === 0) {
result.push({
workbook: wb,
workbookPath: objWB.path,
sheetName: sheetDetails.name,
sheet: wb.Sheets[sheetDetails.name],
sheetDetails,
sheetNumber: ++sheetNumber
});
}
});
});
return result;
},
preConversions() {
let myVue = this,
ext = path.extname(myVue.normalizedNamingScheme),
typeName = /\.json$/i.test(ext)
? 'json'
: /\.(txt|tsv)$/i.test(ext)
? 'tsv'
: 'csv';
return this.workbookSheets.map(o => JS.extend(o, {
newFilePath: myVue.getSheetFilePath(o.workbookPath, o.sheetName, o.sheetNumber),
typeName
}));
}
},
methods: {
matchesFilters(sheetName) {
let myVue = this,
filters = myVue.sheetFilters.filter(f => f.isRegExp ? JS.isRegExp(myVue.parseRegExp(f.value)) : f.value),
i = filters.length;
if (!i) {
return true;
}
for (; i--; ) {
let filter = filters[i];
if (filter.isRegExp ? myVue.parseRegExp(filter.value).test(sheetName) : (filter.value == sheetName)) {
return true;
}
}
return false;
},
onChangeSheetFilter(sheetFilter) {
var emptyIndices = [],
sheetFilters = this.sheetFilters;
for (let i = sheetFilters.length; i--; ) {
if (!sheetFilters[i].value) {
emptyIndices.push(i);
}
}
if (emptyIndices.length > 1) {
emptyIndices.slice(1).forEach(i => sheetFilters.splice(i, 1));
}
sheetFilter.error = null;
if (sheetFilter.value && sheetFilter.isRegExp) {
let rgx = this.parseRegExp(sheetFilter.value);
if (!JS.isRegExp(rgx)) {
sheetFilter.error = rgx;
}
}
},
parseRegExp(strRegExp) {
try {
let rgx;
strRegExp = strRegExp.replace(
/^([\|\/%\$:])((?:\\.|[^\1])+)\1(\w*)$/,
function(m, delim, body, flags) {
rgx = new RegExp(body, flags);
return '';
}
);
if (strRegExp) {
throw new Error('Regular expression not recognized. Perhaps the leading and ending delimiter was not specified. Delimiters can be any of the following: / | % $ :');
}
return rgx;
}
catch (e) {
return e.message;
}
},
browse() {
dialog.showOpenDialog({properties: ['openDirectory']}, function(filePaths) {
let dirPath = filePaths && filePaths[0];
if (dirPath) {
myVue.folderPath = dirPath;
myVue.workbooks.splice(0, Infinity);
let foundWorkbookInSub = false;
let maxLevelsDown = 0;
recurseDirSync(dirPath, function(filePath, isFile, stat) {
if (/\.xlsx?$/i.test(filePath)) {
let fileDirPath = path.dirname(filePath);
if (!foundWorkbookInSub && (fileDirPath != dirPath)) {
foundWorkbookInSub = true;
maxLevelsDown = dialog.showMessageBox({
title: 'How Many Levels Down',
message: 'How many levels down would you like to search for Excel files?',
buttons:['Selected Only', '1', '2', 'All'],
defaultId: 0,
type: 'question'
});
maxLevelsDown = maxLevelsDown < 3 ? maxLevelsDown : Infinity;
}
let levelsDown = fileDirPath.slice(dirPath.length).split(path.sep).length - 1;
if (levelsDown <= maxLevelsDown) {
myVue.workbooks.push({
path: filePath,
include: true
});
}
}
return true;
});
}
});
},
getSheetFilePath(filePath, sheetName, sheetNumber) {
let extName = path.extname(filePath);
let wbName = path.basename(filePath).slice(0, -extName.length);
extName = extName.replace(/^\./, '');
let newPath = /^[\\\/]/.test(this.normalizedNamingScheme)
? this.folderPath + this.normalizedNamingScheme
: (path.dirname(filePath) + path.sep + this.normalizedNamingScheme);
return newPath.replace(
RGX_META,
function(m, typeName) {
return typeName == 'WORKBOOK'
? wbName
: typeName == 'SHEET'
? sheetName
: typeName == 'SHEET_NUMBER'
? sheetNumber
: extName;
}
);
},
convert() {
let myVue = this;
myVue.preConversions.forEach(file => {
myVue.converting = true;
if (myVue.matchesFilters(file.sheetName)) {
let output = XLSX.utils['json' == file.typeName ? 'sheet_to_json' : 'sheet_to_csv'](
file.sheet,
{ FS: 'tsv' == file.typeName ? '\t' : ',' }
);
ensureDirExists(path.dirname(file.newFilePath));
fs.writeFileSync(
file.newFilePath,
'string' == typeof output ? output : JSON.stringify(output)
);
}
myVue.converting = false;
});
$('#modalMessage').modal('show');
},
visitBlog() {
shell.openExternal("http://www.cwestblog.com");
},
close() {
app.exit();
}
},
updated() {
$('[title]').tooltip();
},
mounted() {
$('[title]').tooltip();
}
});
});