Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
322 lines (275 loc) · 10.2 KB
/
Copy pathindex.js
File metadata and controls
322 lines (275 loc) · 10.2 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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://'); // removes www. (DON'T USE! Some links break!)
line = line.replace(/\((https?:\/\/.*?)\/\)/g, "($1)"); // removes trailing /
// 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`);
}
/**
* @returns {Array} list of links in a given file
*/
function getLinks(file = "README.md") {
const data = fs.readFileSync(file, "utf8");
return Array.from(data.matchAll(/\[[^\]]*\]\((https?:\/\/[^)\s]+)\)/g), (match) => match[1]);
}
/** Print the amount of links in a given file */
function countLinks(file = "README.md") {
const linkCount = getLinks(file).length;
console.log(`Total links in ${file}: \x1b[32m${linkCount}\x1b[0m`);
}
async function testUrl(url, { fetchImpl = fetch, timeoutMs = 10000 } = {}) {
const request = (method) => fetchImpl(url, {
method,
redirect: "follow",
headers: { "user-agent": "awesome-free-apps-link-audit/1.0" },
signal: AbortSignal.timeout(timeoutMs),
});
try {
let response = await request("HEAD");
if (!response.ok) {
response = await request("GET");
}
return { url, ok: response.ok, status: response.status };
} catch (error) {
return {
url,
ok: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
async function mapWithConcurrency(items, concurrency, worker) {
if (!Number.isInteger(concurrency) || concurrency < 1) {
throw new TypeError("concurrency must be a positive integer");
}
const results = new Array(items.length);
let nextIndex = 0;
async function runWorker() {
while (nextIndex < items.length) {
const index = nextIndex++;
results[index] = await worker(items[index], index);
}
}
const workerCount = Math.min(concurrency, items.length);
await Promise.all(Array.from({ length: workerCount }, runWorker));
return results;
}
/**
* Test if all links in a given file are valid (reachable)
* @param {string} file
* @returns {Promise}
*/
async function testLinksReachable(
file = "README.md",
{ concurrency = 12, testUrlImpl = testUrl } = {},
) {
const links = [...new Set(getLinks(file))];
console.log(`Testing ${links.length} unique links in \x1b[32m${file}\x1b[0m`);
const results = await mapWithConcurrency(links, concurrency, testUrlImpl);
const failures = results.filter((result) => !result.ok);
for (const failure of failures) {
const reason = failure.status ? `HTTP ${failure.status}` : failure.error;
console.error(`- ${failure.url} (${reason || "request failed"})`);
}
console.log(
`${file}: ${results.length - failures.length}/${results.length} links reachable`,
);
return { file, total: results.length, failures };
}
async function testLinksInAllFiles(options) {
const reports = [];
reports.push(await testLinksReachable("README.md", options));
reports.push(await testLinksReachable("MOBILE.md", options));
const total = reports.reduce((sum, report) => sum + report.total, 0);
const failures = reports.flatMap((report) => report.failures);
console.log(`Link audit complete: ${total - failures.length}/${total} reachable`);
return { total, failures, reports };
}
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");
}
function main(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 if (args.includes('--test-links')) {
testLinksInAllFiles()
.then(({ failures }) => {
if (failures.length > 0) process.exitCode = 1;
})
.catch((error) => {
console.error("Link audit failed:", error);
process.exitCode = 1;
});
} 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)");
}
}
if (require.main === module) {
main();
}
module.exports = {
getLinks,
mapWithConcurrency,
testLinksInAllFiles,
testLinksReachable,
testUrl,
};