forked from solana-foundation/developer-content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coder.ts
222 lines (193 loc) · 6.12 KB
/
coder.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
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
import { promises as fs } from "node:fs";
import path from "node:path";
import os from "node:os";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
import remarkFrontmatter from "remark-frontmatter";
import { visit } from "unist-util-visit";
import ignore, { type Ignore } from "ignore";
import importCode from "./src/utils/code-import";
import chokidar from "chokidar";
let debugMode = false;
const debug = (...args: string[]) => {
if (debugMode) {
console.log("[DEBUG]", ...args);
}
};
const hasCodeComponentWithFileMeta = async (
filePath: string,
): Promise<boolean> => {
const content = await fs.readFile(filePath, "utf8");
let hasMatch = false;
const tree = unified().use(remarkParse).use(remarkFrontmatter).parse(content);
visit(tree, "code", node => {
if (node.meta?.includes("file=")) {
hasMatch = true;
return false; // Stop visiting
}
});
return hasMatch;
};
const getIgnore = async (directory: string): Promise<Ignore> => {
const ig = ignore();
try {
const gitignoreContent = await fs.readFile(
path.join(directory, ".gitignore"),
"utf8",
);
ig.add(gitignoreContent);
// ignore all dotfiles
ig.add([".*"]);
// ignore CONTRIBUTING.md because it mentions the code component example
ig.add("CONTRIBUTING.md");
} catch (error) {
// If .gitignore doesn't exist, just continue without it
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
throw error;
}
}
return ig;
};
const getMarkdownAndMDXFiles = async (directory: string): Promise<string[]> => {
const ig = await getIgnore(directory);
const walkDir = async (dir: string): Promise<string[]> => {
const entries = await fs.readdir(dir, { withFileTypes: true });
const files = await Promise.all(
entries.map(async entry => {
const res = path.resolve(dir, entry.name);
const relativePath = path.relative(directory, res);
if (ig.ignores(relativePath) || entry.name === ".gitignore") {
debug(`Ignoring file: ${relativePath}`);
return [];
}
if (entry.isDirectory()) {
return walkDir(res);
}
if (
entry.isFile() &&
(entry.name.endsWith(".md") || entry.name.endsWith(".mdx"))
) {
if (await hasCodeComponentWithFileMeta(res)) {
debug(`Found file with code component: ${relativePath}`);
return res;
}
debug(
`Skipping file (no code component with file meta): ${relativePath}`,
);
}
return [];
}),
);
return files.flat();
};
return walkDir(directory);
};
const processContent = async (
content: string,
filePath: string,
): Promise<string> => {
try {
const file = await unified()
.use(remarkParse)
.use(remarkFrontmatter)
.use(importCode, {
preserveTrailingNewline: false,
removeRedundantIndentations: true,
rootDir: process.cwd(),
})
.use(remarkStringify, {
bullet: "-",
emphasis: "*",
fences: true,
listItemIndent: "one",
rule: "-",
ruleSpaces: false,
strong: "*",
tightDefinitions: true,
})
.process(content);
return String(file);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
throw new Error(
`File not found: ${(error as NodeJS.ErrnoException).path}`,
);
}
throw error;
}
};
const processFile = async (filePath: string): Promise<void> => {
try {
if (!(await hasCodeComponentWithFileMeta(filePath))) {
debug(`Skipping ${filePath}: No code component with file meta found.`);
return;
}
const originalContent = await fs.readFile(filePath, "utf8");
const processedContent = await processContent(originalContent, filePath);
if (originalContent !== processedContent) {
await fs.writeFile(filePath, processedContent);
console.log(`Updated: ${filePath}`);
} else {
debug(`No changes needed for: ${filePath}`);
}
} catch (error) {
console.error(`Error processing ${filePath}: ${(error as Error).message}`);
}
};
const processInChunks = async <T>(
items: T[],
processItem: (item: T) => Promise<void>,
chunkSize: number,
): Promise<void> => {
for (let i = 0; i < items.length; i += chunkSize) {
const chunk = items.slice(i, i + chunkSize);
await Promise.all(chunk.map(processItem));
}
};
const watchFiles = async (directory: string): Promise<void> => {
const watcher = chokidar.watch(["**/*.md", "**/*.mdx"], {
ignored: [
"**.**",
/(^|[\/\\])\../,
"**/node_modules/**",
"**/.git/**",
".gitignore",
], // ignore dotfiles, node_modules, .git, and .gitignore
persistent: true,
cwd: directory,
});
console.log("Watch mode started. Waiting for file changes...");
watcher
.on("add", filePath => processFile(path.join(directory, filePath)))
.on("change", filePath => processFile(path.join(directory, filePath)))
.on("unlink", filePath => console.log(`File ${filePath} has been removed`));
};
const main = async (): Promise<void> => {
const filePath = process.argv[2];
const watchMode =
process.argv.includes("--watch") || process.argv.includes("-w");
debugMode = process.argv.includes("--debug") || process.argv.includes("-d");
if (debugMode) {
console.log("Debug mode enabled");
}
if (filePath && !watchMode && !debugMode) {
// Process single file
const absolutePath = path.resolve(process.cwd(), filePath);
console.log(`Processing single file: ${absolutePath}`);
await processFile(absolutePath);
} else if (watchMode) {
// Watch mode
await watchFiles(process.cwd());
} else {
// Process all files
const files = await getMarkdownAndMDXFiles(process.cwd());
const chunkSize = Math.max(1, Math.ceil(files.length / os.cpus().length));
console.log(`Processing ${files.length} files...`);
await processInChunks(files, processFile, chunkSize);
}
if (!watchMode) {
console.log("Sync process completed.");
}
};
main().catch(console.error);