-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-path.ts
executable file
·52 lines (43 loc) · 1.45 KB
/
add-path.ts
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
#!/usr/bin/env -S deno run --allow-read --allow-write
import { walk } from 'https://deno.land/[email protected]/fs/mod.ts';
import { relative, resolve } from 'https://deno.land/[email protected]/path/mod.ts';
async function addPathComments(rootDir: string) {
const resolvedRoot = resolve(rootDir);
for await (const entry of walk(resolvedRoot, {
includeDirs: false,
exts: ['.md'],
})) {
const filePath = entry.path;
const relPath = relative(resolvedRoot, filePath);
const comment = `<!-- ${relPath} -->\n`;
try {
const content = await Deno.readTextFile(filePath);
if (content.startsWith(comment)) {
console.log(`Comment already present in ${relPath}`);
continue;
}
const lines = content.split('\n');
while (lines.length > 0 && (lines[0].trim() === '' || lines[0].trim().startsWith('<!--'))) {
lines.shift();
}
lines.unshift(comment.trim());
const newContent = lines.join('\n');
await Deno.writeTextFile(filePath, newContent);
console.log(`Updated comment in ${relPath}`);
} catch (err) {
const error = err as Error;
console.error(`Error processing ${relPath}: ${error.message}`);
}
}
}
if (import.meta.main) {
const rootDir = Deno.cwd();
try {
await addPathComments(rootDir);
console.log('Finished adding file path comments');
} catch (err) {
const error = err as Error;
console.error('Error:', error.message);
Deno.exit(1);
}
}