-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathdirectory-entry-ids
More file actions
executable file
·107 lines (88 loc) · 2.81 KB
/
directory-entry-ids
File metadata and controls
executable file
·107 lines (88 loc) · 2.81 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
#! /usr/bin/env node
import { readFileSync, writeFileSync, readdirSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Character set from the formula
const charset = "abcdefghijkmnopqrstuvwxyzACDEFGHJKLMNPQRTUVWXY34679";
// Generate a random 6-character ID
function generateId(existingIds) {
let id = "";
let attempts = 0;
const maxAttempts = 1000;
do {
id = "";
for (let i = 0; i < 6; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
id += charset[randomIndex];
}
attempts++;
if (attempts > maxAttempts) {
throw new Error(
`Failed to generate unique ID after ${maxAttempts} attempts`,
);
}
} while (existingIds.has(id));
return id;
}
// Process all .yaml files in the directory
const dir = join(__dirname, "../src/content/directory");
const files = readdirSync(dir).filter((f) => f.endsWith(".yaml"));
console.log(`Found ${files.length} .yaml files\n`);
// Track all existing IDs
const existingIds = new Map(); // id -> filename
const errors = [];
let addedCount = 0;
// First pass: collect existing IDs and check for issues
for (const file of files) {
const filePath = join(dir, file);
const content = readFileSync(filePath, "utf-8");
const lines = content.split("\n");
// Check if file has an id on the first line
if (lines[0].startsWith("id: ")) {
const id = lines[0].substring(4).trim();
// Check for duplicate IDs
if (existingIds.has(id)) {
errors.push(
`❌ ${file} - duplicate id "${id}" (also in ${existingIds.get(id)})`,
);
} else {
existingIds.set(id, file);
console.log(`✓ ${file} - has id: ${id}`);
}
}
}
// Second pass: add IDs to files that don't have them
for (const file of files) {
const filePath = join(dir, file);
const content = readFileSync(filePath, "utf-8");
// Check if file already has an id on the first line
if (content.startsWith("id: ")) {
continue;
}
// Generate a unique ID
const id = generateId(existingIds);
existingIds.set(id, file);
// Add the id as the first line
const newContent = `id: ${id}\n${content}`;
writeFileSync(filePath, newContent, "utf-8");
console.log(`✓ ${file} - added id: ${id}`);
addedCount++;
}
// Report results
console.log(`\n${"=".repeat(50)}`);
console.log(`Summary:`);
console.log(` Total files: ${files.length}`);
console.log(` Files with existing IDs: ${existingIds.size - addedCount}`);
console.log(` Files with added IDs: ${addedCount}`);
console.log(` Unique IDs: ${existingIds.size}`);
if (errors.length > 0) {
console.log(`\n${"=".repeat(50)}`);
console.log(`Errors found:`);
errors.forEach((error) => console.log(error));
process.exit(1);
} else {
console.log(` Errors: 0`);
console.log(`\n✅ All IDs are unique and properly placed!`);
}