-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloc.js
More file actions
118 lines (102 loc) · 3.29 KB
/
cloc.js
File metadata and controls
118 lines (102 loc) · 3.29 KB
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
'use strict';
/*
* Access to filesystem in order to read and write files.
*/
const fs = require('fs');
/*
* The file name of the input file.
*/
var resultFile = null;
/*
* The file name of the output file.
*/
var readmeFile = 'readme.md';
/*
* Saves the heading of the section we want to update.
*/
var readmeHeading = '## Lines of code';
/**
* Inserts a character at a given position in a string.
*/
String.prototype.insertAt = function (index, char) {
return this.substring(0, index) + char + this.substring(index);
}
/**
* Replaces a character at a given position in a string by one or more new characters.
*/
String.prototype.replaceAt = function (index, newChars) {
return this.substring(0, index) + newChars + this.substring(index + 1);
}
/*
* Gets the input via command line arguments (note: index 2 is the first command line argument).
*/
process.argv.forEach(function (val, index, array) {
switch (index) {
case 2:
resultFile = val;
break;
default:
return;
}
});
/*
* Takes the input and formats it for markdown style. Then we write it into the readme file.
*/
if (resultFile != null && fs.existsSync(resultFile)) {
var result = fs.readFileSync(resultFile, {
encoding: 'utf8'
});
var numberOfLines = 1;
// Remove all information before the actual table
result = result.substring(result.indexOf('Language'));
// Add markdown column separators at the beginning and end of each line.
for (var i = result.length - 2; i >= 0; i--) {
if (result[i] === '\n') {
result = result.substring(0, i) + '|\n|' + result.substring(i + 1);
numberOfLines++;
}
}
result = '|' + result.substring(0, result.length - 1) + '|';
// Calculate index positions for the other column separators.
var positions = [];
var lineLength;
for (var i = 1; i < result.length; i++) {
if (/^[a-zA-Z]+$/.test(result[i]) && /^\s$/.test(result[i - 1])) {
positions.push(i);
}
if (result[i] === '\n') {
lineLength = i + 1;
break;
}
}
// Remove last line
result = result.substring(0, lineLength * (numberOfLines - 1));
numberOfLines--;
// Add the column separators at the calculated index positions.
for (var i = numberOfLines - 1; i >= 0; i--) {
for (var j = positions.length - 1; j >= 0; j--) {
result = result.insertAt(i * lineLength + positions[j], '|');
}
}
// Remove unnecessary dashes
lineLength += positions.length; // Because we added pipe symbols
var lineToReplace = result.substring(lineLength * (numberOfLines - 2), lineLength * (numberOfLines - 1));
lineToReplace = lineToReplace.replace(/-/g, ' ');
for (var i = 0; i < lineToReplace.length; i++) {
if (lineToReplace[i] === '|') {
lineToReplace = lineToReplace.substring(0, i + 1) + '-' + lineToReplace.substring(i + 2);
}
}
lineToReplace = lineToReplace.substring(0, lineToReplace.length - 1); // One dash too much
result = result.substring(0, lineLength * (numberOfLines - 2)) + lineToReplace +
result.substring(lineLength * (numberOfLines - 1) - 1);
// Update the markdown content (assumes that nothing follows after the section we want to update)
var readme = fs.readFileSync(readmeFile, {
encoding: 'utf8'
});
readme = readme.substring(0, readme.indexOf(readmeHeading) + readmeHeading.length + 1) + '\n' + result;
// Write back to markdown file
fs.writeFileSync(readmeFile, readme, {
encoding: 'utf8'
});
}