Skip to content

Commit 1fb223a

Browse files
committed
Export to Excel feature
1 parent 443bc8c commit 1fb223a

File tree

5 files changed

+2077
-11
lines changed

5 files changed

+2077
-11
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# NetBeans specific #
2+
nbproject/private/
3+
build/
4+
nbbuild/
5+
dist/
6+
nbdist/
7+
nbactions.xml
8+
nb-configuration.xml
9+
10+
# Text Files #
11+
*.txt
12+
*.log
13+
14+
# Other Files #
15+
*.xls
16+
*.xlsx
17+
test.js

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Implemented features:
1111
- Search for pages with incomplete translation in the selected language
1212
- Displaying the date and time of changes made to translations into the selected language and the name of the editor for each file
1313
- Displays a list of new pages added to the documentation for the specified period
14+
- Create an Excel spreadsheet of the project documents
1415

1516
Installation:
1617
-----------------

main.js

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@ const _ = require('underscore');
1515
const cliProgress = require('cli-progress');
1616
const colors = require('ansi-colors');
1717
const EventEmitter = require('events');
18+
const { jsonToExcel, exportJsonToExcel } = require('@papb/json-excel');
1819

1920
class Emitter extends EventEmitter {};
2021
class Emitter2 extends EventEmitter {};
2122
const parser_events = new Emitter();
2223
const tasks_events = new Emitter2();
2324

25+
const linux = (process.platform === 'linux');
26+
const win32 = (process.platform === 'win32');
27+
2428
var Directories = [];
2529
var Projects = [];
26-
var Files = [];
30+
var newFiles = [];
31+
var fileGroups = [];
2732
var Schema = {};
2833
var Blocks = {};
2934
var Outdated = {};
3035
var Empty_pages = [];
3136
var No_translation = [];
37+
var xlsx = [];
3238
var words_count_total = 0;
3339
var verbose = false;
3440
var recount_words = false;
@@ -37,9 +43,11 @@ var empty_pages = false;
3743
var check_translations = false;
3844
var translation_commiter = false;
3945
var new_pages = false;
46+
var xls_table = false;
4047
var lang, lang_name, regexp, begin, end, begin_ts, end_ts;
4148

4249
const basedir = '../Superalgos';
50+
const xlsFileName = 'Superalgos-Docs.xlsx';
4351

4452
const date = new Date();
4553
var m = date.getMonth() + 1;
@@ -69,6 +77,7 @@ process.argv.forEach((arg, i) => {
6977
console.log('[ --new-pages=[begin]]\t If [end] is not specified, then up to the present.The date format is "YYYY-MM-DD".');
7078
console.log('\t\t\t\t Without parameters it is displayed for the current month.');
7179
console.log('\t\t\t\t Examples: node main --new-pages=2021.01.01:2022.01.07, node main --new-pages=2022.01.01');
80+
console.log('[-x, --xls-table]\t\t Create an Excel spreadsheet of the project documents');
7281
}
7382
process.exit();
7483
break;
@@ -161,14 +170,19 @@ process.argv.forEach((arg, i) => {
161170
new_pages = true;
162171
break;
163172

173+
case '-x':
174+
case '--xls-table':
175+
xls_table = true;
176+
break;
177+
164178
default:
165179
console.log(`Invalid argument[${i - 1}]: ${arg}`);
166180
break;
167181
}
168182
}
169183
});
170184

171-
if (!recount_words && !check_outdated && !check_translations && !empty_pages && !translation_commiter && !new_pages)
185+
if (!recount_words && !check_outdated && !check_translations && !empty_pages && !translation_commiter && !new_pages && !xls_table)
172186
{ console.log("No assignments. Use '-h' for help"); process.exit();}
173187

174188
const bar = new cliProgress.SingleBar({
@@ -188,7 +202,7 @@ FileHound.create()
188202
.then(directories => {
189203
console.log();
190204
directories.forEach((directory) => {
191-
Projects.push(
205+
fileGroups.push(
192206
FileHound.create()
193207
.path(directory)
194208
.discard('App-Schema')
@@ -197,14 +211,13 @@ FileHound.create()
197211
);
198212
});
199213

200-
Promise.all(Projects).then((groups) => {
214+
Promise.all(fileGroups).then((groups) => {
201215
groups = _.reject(groups, function (elem) { return !elem.length;});
202216
groups.forEach((group, gi) => {
203217
group.forEach((file, fi) => {
204-
let arr = file.split('\\'), project = arr[3], category, type, block, translated = true;
205-
let isBlock;
206-
207-
switch (arr[5]) {
218+
let arr = (win32)? file.split('\\') : file.split('/'), project = arr[3], categoryDirName = arr[5], category, block, translated = true, isBlock, file_name = _.last(arr);
219+
220+
switch (categoryDirName) {
208221
case 'Docs-Nodes':
209222
category = 'Node';
210223
break;
@@ -233,12 +246,15 @@ FileHound.create()
233246
console.warn(`No category is assigned for --> ${arr[5]}`);
234247
break;
235248
}
249+
if (!Projects[project]) Projects[project] = {};
250+
if (!Projects[project][categoryDirName]) Projects[project][categoryDirName] = { category: category + 's'};
236251

237252
try {
238253
let type, topic;
239254
let obj = JSON.parse(fs.readFileSync(file));
240255

241256
type = obj.type;
257+
Projects[project][categoryDirName][file_name] = { type: type };
242258

243259
if (_.has(obj, 'topic')) {
244260
topic = obj.topic;
@@ -352,7 +368,7 @@ parser_events.on('end', (groups) => {
352368
group.forEach((file, fi) => {
353369
try {
354370
if (recount_words || translation_commiter) console.log(file);
355-
let words_count = 0, type, topic, definition;
371+
let words_count = 0, type, topic, definition, arr = (win32)? file.split('\\') : file.split('/'), categoryDirName = arr[5], file_name = _.last(arr), project = arr[3];
356372
let obj = JSON.parse(fs.readFileSync(file));
357373

358374
if (translation_commiter || new_pages) {
@@ -374,7 +390,7 @@ parser_events.on('end', (groups) => {
374390
if (new_pages) {
375391
let t = _.first(text), ts = Date.parse(t.split('(').pop().split(')').shift().split(' +').shift().slice(-19).split(' ').shift());
376392
bar.increment(); bar.update({ file: fn });
377-
if (begin_ts <= ts && ts <= end_ts) { Files.push(file);}
393+
if (begin_ts <= ts && ts <= end_ts) { newFiles.push(file);}
378394
if (gi === (groups.length - 1) && fi === (group.length - 1)) { setTimeout(() => { tasks_events.emit('complete');}, 3000 );}
379395
}
380396
}
@@ -462,6 +478,7 @@ parser_events.on('end', (groups) => {
462478
if (verbose) console.log(obj);
463479
console.log(`words: ${words_count}\n`);
464480
}
481+
Projects[project][categoryDirName][file_name].wordcount = words_count;
465482
words_count_total += words_count;
466483
}
467484
} catch(err) {
@@ -478,6 +495,7 @@ tasks_events.on('complete', () => {
478495

479496
process.on('beforeExit', () => {
480497
//console.log(Schema);
498+
//console.log(Projects);
481499
if (recount_words) console.log(`Words Total: ${words_count_total}`);
482500
if (check_outdated && Object.keys(Outdated).length) {
483501
console.log('Pages with outdated translations:');
@@ -508,10 +526,32 @@ process.on('beforeExit', () => {
508526
bar.stop();
509527
console.log();
510528
console.log(`Pages added from ${begin.d}-${begin.m}-${begin.y} to ${end.d}-${end.m}-${end.y}`);
511-
if (Files.length) Files.forEach(page => console.log(page));
529+
if (newFiles.length) newFiles.forEach(page => console.log(page));
512530
else console.log('No new pages found');
513531
}
514532

533+
if (xls_table) {
534+
console.log();
535+
for (let project in Projects) {
536+
xlsx.push({ sheetName: project.split('-').join(' '), data: [], formatAsTable: false });
537+
for (let category in Projects[project]) {
538+
let last = xlsx.length - 1, categories = Projects[project][category], start_of_line;
539+
for (let item in categories) {
540+
if (item === 'category') xlsx[last].data.push([categories[item], '']);
541+
else xlsx[last].data.push([ categories[item].type, (categories[item].wordcount) ? categories[item].wordcount.toString() : '']);
542+
}
543+
xlsx[last].data.push(['', '']);
544+
}
545+
}
546+
547+
exportJsonToExcel(xlsFileName, xlsx, { overwrite: true })
548+
.then(() => {
549+
console.log(`File ${xlsFileName} exported to the main directory`);
550+
process.exit();
551+
});
552+
553+
}
554+
515555
console.log('\nDone!');
516556
});
517557

0 commit comments

Comments
 (0)