-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply.ts
More file actions
85 lines (68 loc) · 2.71 KB
/
apply.ts
File metadata and controls
85 lines (68 loc) · 2.71 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
import * as fs from 'fs';
import * as path from 'path';
// Configuration
const sourceDir = '../simulo/lua_docs/autogenerated';
const outputDir = './out/api';
// Ensure output directory exists
function ensureDirectoryExists(dirPath: string): void {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
// Remove output directory if it exists
function cleanOutputDirectory(): void {
if (fs.existsSync('./out')) {
fs.rmSync('./out', { recursive: true, force: true });
}
// Recreate output directory structure
ensureDirectoryExists(outputDir);
}
// Process each file from source directory
function processFiles(): void {
// Read all files in the source directory
const files = fs.readdirSync(sourceDir);
files.forEach(file => {
const sourcePath = path.join(sourceDir, file);
const stats = fs.statSync(sourcePath);
if (stats.isFile() && file.endsWith('.md')) {
// Read the file content
const content = fs.readFileSync(sourcePath, 'utf8');
// Split content into lines
const lines = content.split('\n');
let subDir = outputDir; // Default to out/api
let finalContent = content;
let dirInfo = '';
// Check if first line starts with ':'
if (lines.length > 0 && lines[0].trim().startsWith(':')) {
// Extract directory name (remove the leading ':')
const dirName = lines[0].trim().substring(1).trim();
if (dirName) {
// Create a subdirectory based on the extracted name
subDir = path.join(outputDir, dirName);
dirInfo = ` (using directory: ${dirName})`;
// Remove the first line from content
finalContent = lines.slice(1).join('\n');
}
}
// Ensure the target directory exists
ensureDirectoryExists(subDir);
// Create new filename with .mdx extension
const newFileName = path.basename(file, '.md') + '.mdx';
const outputPath = path.join(subDir, newFileName);
// Write the transformed content to the new file
fs.writeFileSync(outputPath, finalContent);
console.log(`Processed: ${file} -> ${outputPath}${dirInfo}`);
} else if (stats.isDirectory()) {
// Handle nested directories if needed
console.log(`Skipping directory: ${file}`);
}
});
}
// Main execution
function main(): void {
console.log('Starting file transformation process...');
cleanOutputDirectory();
processFiles();
console.log('File transformation complete!');
}
main();