-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathindex.js
210 lines (179 loc) · 6.92 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
const fs = require('fs');
const { execSync } = require('child_process');
function create(file, fileName, emoji) {
const startTime = Date.now();
let data = fs.readFileSync(file, 'utf8');
const emojis = new Set(["🪟", "🍎", "🐧", "🟢", "⭐", "🤖"]);
let result = [];
let lineCount = 0;
const lines = data.split('\n');
const outputDir = './filter';
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (lineCount < 20) {
result.push(line);
lineCount++;
continue;
}
line = line.replace(/ /g, '');
if ([...emojis].some(emojiChar => line.includes(emojiChar)) && !line.includes(emoji)) {
continue;
}
result.push(lines[i]);
}
fs.writeFileSync(`${outputDir}/${fileName}.md`, result.join('\n').replaceAll('./filter/', '').replaceAll('href="./', 'href="../').replaceAll('src="./', 'src="../'));
console.log(`${fileName} time: \x1b[32m${Date.now() - startTime}ms\x1b[0m`);
}
function categorize() {
create("README.md", "windows-only", "🪟");
create("README.md","macOS-only", "🍎");
create("README.md","linux-only", "🐧");
create("README.md","open-source-only", "🟢");
create("README.md","recommended-only", "⭐");
create("MOBILE.md", "android-only", "🤖");
create("MOBILE.md","iOS-only", "🍎");
create("MOBILE.md","open-source-mobile-only", "🟢");
create("MOBILE.md","recommended-mobile-only", "⭐");
}
function format(file = "README.md") {
const data = fs.readFileSync(file, 'utf8');
const updatedData = data
.split('\n')
.map(line => {
line = line.replace(/https:\/\/www\./g, 'https://');
if (line.startsWith('- [')) {
const parts = line.split(' - ');
if (parts.length >= 2) {
let description = parts[1];
description = description.replace(/^A\s+/i, '').replace(/^An\s+/i, '');
description = description.charAt(0).toUpperCase() + description.slice(1);
parts[1] = description;
line = parts.join(' - ');
}
}
return line;
})
.join('\n');
fs.writeFileSync(file, updatedData, 'utf8');
console.log(`\x1b[32m${file} has been formatted.\x1b[0m`);
}
function countLinks(file = "README.md") {
const data = fs.readFileSync(file, 'utf8');
const linkCount = (data.match(/\[.*?\]\(https?:\/\/.*?\)/g) || []).length;
console.log(`Total links in ${file}: \x1b[32m${linkCount}\x1b[0m`);
}
function fastGit(message = "update") {
try {
execSync('git add -A', { stdio: 'inherit' });
execSync(`git commit -m "${message}"`, { stdio: 'inherit' });
execSync('git push', { stdio: 'inherit' });
console.log('Changes have been committed and pushed.');
} catch (error) {
console.error('Error running git commands:', error);
}
}
function runAll() {
countLinks("README.md");
countLinks("MOBILE.md");
formatFiles();
createToC();
categorize();
countLinks("README.md");
countLinks("MOBILE.md");
}
function analyze(file = "README.md") {
const data = fs.readFileSync(file, 'utf8');
const words = data.match(/\b\w+\b/g);
const wordCount = words ? words.length : 0;
const linkCount = (data.match(/\[.*?\]\(https?:\/\/.*?\)/g) || []).length;
const characterCount = data.length;
console.log(`${file} Word Count: \x1b[32m${wordCount}\x1b[0m`);
console.log(`${file} Character Count: \x1b[32m${characterCount}\x1b[0m`);
console.log(`${file} Link Count: \x1b[32m${linkCount}\x1b[0m`);
}
function generateToc(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const lines = content.split('\n');
let toc = [];
lines.forEach(line => {
const match = /^#{2,6}\s+(.+)/.exec(line);
if (match) {
const level = match[0].indexOf(' ') - 1;
const title = match[1].trim();
const anchor = title
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '');
toc.push(`${' '.repeat(level - 1)}- [${title}](#${anchor})`);
}
});
const tocContent = toc.slice(1).join('\n');
return tocContent;
}
function findAF(file, name, message, suffix) {
const data = fs.readFileSync(file, 'utf8');
const startMarker = `<!-- AF-${name}`.trim();
const endMarker = `<!-- AF-END -->`.trim();
const startIndex = data.indexOf(startMarker);
if (startIndex !== -1) {
const cleanStartIndex = data.indexOf(">", startIndex) + 1;
const endIndex = data.indexOf(endMarker, cleanStartIndex);
if (endIndex !== -1) {
let newData = data.slice(0, cleanStartIndex) + message + data.slice(endIndex);
if (suffix) {
const newStartMarker = `<!-- AF-${name} ${suffix} -->`;
newData = newData.replace(data.slice(startIndex, cleanStartIndex), newStartMarker);
}
fs.writeFileSync(file, newData, 'utf8');
console.log(file + " updated successfully.");
} else {
console.log(`No end marker found for ${startMarker}`);
}
} else {
console.log(`No content found for ${startMarker}`);
}
}
function createToC() {
findAF("README.md", "TOC", "\n\n" + generateToc("README.md") + "\n\n", `: ${new Date().toLocaleString('en-US', { timeZoneName: 'short', weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true })}`);
findAF("MOBILE.md", "TOC", "\n\n" + generateToc("MOBILE.md") + "\n\n", `: ${new Date().toLocaleString('en-US', { timeZoneName: 'short', weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true })}`);
}
function formatFiles() {
format("README.md");
format("MOBILE.md");
}
const args = process.argv.slice(2);
if (args.includes('--analyze')) {
analyze("README.md");
analyze("MOBILE.md");
} else if (args.includes('--toc')) {
createToC();
} else if (args.includes('--categorize')) {
categorize();
} else if (args.includes('--format')) {
formatFiles();
} else if (args.includes('--links')) {
countLinks("README.md");
countLinks("MOBILE.md");
} else if (args.includes('--fastgit')) {
const commitMessage = args.slice(1).join(' ');
if (commitMessage) {
fastGit(commitMessage);
} else {
console.log('Please provide a commit message after --fastgit');
}
} else if (args.includes('--all')) {
runAll();
} else {
console.log("Usage:");
console.log(" node index.js --categorize Categorize based on icons");
console.log(" node index.js --format Format README.md");
console.log(" node index.js --links Count and display total links in README.md");
console.log(" node index.js --fastgit <msg> Run git commands with the specified commit message");
console.log(" node index.js --analyze Print some info about README.md");
console.log(" node index.js --toc Update the table of contents");
console.log(" node index.js --all Run all the commands (format, categorize, links)");
}