-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-types.ts
More file actions
executable file
·104 lines (86 loc) · 2.91 KB
/
Copy pathgenerate-types.ts
File metadata and controls
executable file
·104 lines (86 loc) · 2.91 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
#!/usr/bin/env -S deno run --allow-read --allow-write
/**
* Generate TypeScript declaration files (.d.ts)
*
* This script creates .d.ts files that re-export from the original .ts files.
* Since Deno uses TypeScript natively, the .ts files serve as the source of truth.
*/
async function* walkDir(dir: string): AsyncGenerator<string> {
for await (const entry of Deno.readDir(dir)) {
const path = `${dir}/${entry.name}`;
if (entry.isDirectory && !entry.name.startsWith('.') && entry.name !== 'node_modules' && entry.name !== 'coverage') {
yield* walkDir(path);
} else if (entry.isFile && entry.name.endsWith('.ts') && !entry.name.endsWith('.test.ts')) {
yield path;
}
}
}
async function ensureDir(path: string) {
try {
await Deno.mkdir(path, { recursive: true });
} catch (err) {
if (!(err instanceof Deno.errors.AlreadyExists)) {
throw err;
}
}
}
function dirname(path: string): string {
const parts = path.split('/');
parts.pop();
return parts.join('/');
}
/**
* Calculate relative path from 'from' directory to 'to' file
* Note: This is simplified for the specific use case where:
* - from = src directory
* - to = file within src directory
* Result will always be a forward path (no ../)
*/
function relative(from: string, to: string): string {
const fromParts = from.split('/');
const toParts = to.split('/');
// Remove common prefix
while (fromParts.length && toParts.length && fromParts[0] === toParts[0]) {
fromParts.shift();
toParts.shift();
}
return toParts.join('/');
}
const projectRoot = Deno.cwd();
const srcDir = `${projectRoot}/src`;
const distDir = `${projectRoot}/dist`;
console.log("🔨 Generating TypeScript declaration files...\n");
console.log(`📁 Source: ${srcDir}`);
console.log(`📁 Output: ${distDir}\n`);
// Clean dist directory
try {
await Deno.remove(distDir, { recursive: true });
} catch {
// Directory might not exist
}
await ensureDir(distDir);
let fileCount = 0;
// Process all TypeScript files
for await (const filePath of walkDir(srcDir)) {
const relativePath = relative(srcDir, filePath);
const outputPath = `${distDir}/${relativePath.replace(/\.ts$/, '.d.ts')}`;
// Create declaration file content
const dtsContent = `/**
* Type definitions for ${relativePath.replace(/\.ts$/, '')}
* AUTO-GENERATED - do not edit manually
*
* This file re-exports types from the TypeScript source file.
* For Deno projects, the .ts files themselves serve as type definitions.
*/
export * from '../src/${relativePath}';
`;
// Ensure output directory exists
const outputDir = dirname(outputPath);
await ensureDir(outputDir);
// Write the declaration file
await Deno.writeTextFile(outputPath, dtsContent);
fileCount++;
console.log(`✓ ${relativePath.replace(/\.ts$/, '.d.ts')}`);
}
console.log(`\n✅ Generated ${fileCount} declaration files successfully!`);
console.log(`📁 Output: ${distDir}\n`);