Skip to content

Commit 537e225

Browse files
committed
fix: code rabbit review
1 parent 591a92f commit 537e225

24 files changed

+166
-815
lines changed

pnpm-lock.yaml

Lines changed: 47 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoV2/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pnpm run build # Builds to proto-ts/
8383
```
8484
protoV2/
8585
├── _scripts/
86+
│ ├── generate-dts.mjs # Shared: Generate .d.ts files
87+
│ ├── fix-imports.mjs # Shared: Fix ESM import extensions
88+
│ ├── tsup-base.config.mjs # Shared: Base tsup configuration
8689
│ ├── update-index-template.sh # Auto-generate index templates
8790
│ └── README.md # Script documentation
8891
├── core/
@@ -91,7 +94,7 @@ protoV2/
9194
│ │ └── generated/ # Generated proto files
9295
│ ├── proto-ts/ # Built output
9396
│ ├── gen.sh # Generation script
94-
│ └── tsup.config.ts # Build configuration
97+
│ └── tsup.config.ts # Build configuration (uses shared base)
9598
├── indexer/
9699
│ ├── src/
97100
│ │ ├── index.template.ts # Template for exports (auto-gen)
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/usr/bin/env node
2+
/**
3+
* Shared script to fix ESM imports by adding .js extensions.
4+
* Usage: node ../_scripts/fix-imports.mjs
5+
*
6+
* Must be run from a package directory (e.g., protoV2/core/)
7+
*/
28
import { readFileSync, writeFileSync, readdirSync, statSync } from 'fs'
3-
import { join, dirname } from 'path'
4-
import { fileURLToPath } from 'url'
5-
6-
const __dirname = dirname(fileURLToPath(import.meta.url))
9+
import { join } from 'path'
710

811
// Recursively find all .js files
912
function findJsFiles(dir) {
@@ -24,8 +27,8 @@ function findJsFiles(dir) {
2427
return results
2528
}
2629

27-
// Find all .js files in proto-ts
28-
const outDir = join(__dirname, 'proto-ts')
30+
// Find all .js files in proto-ts (relative to cwd)
31+
const outDir = join(process.cwd(), 'proto-ts')
2932
const files = findJsFiles(outDir)
3033

3134
console.log(`Found ${files.length} .js files to process`)
@@ -60,7 +63,7 @@ files.forEach((fullPath) => {
6063

6164
if (fileReplacements > 0) {
6265
writeFileSync(fullPath, newContent, 'utf8')
63-
const relativePath = fullPath.replace(__dirname + '/', '')
66+
const relativePath = fullPath.replace(process.cwd() + '/', '')
6467
console.log(` ✓ ${relativePath}: ${fileReplacements} imports fixed`)
6568
totalReplacements += fileReplacements
6669
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#!/usr/bin/env node
2+
/**
3+
* Shared script to generate .d.ts files for proto packages.
4+
* Usage: node ../_scripts/generate-dts.mjs
5+
*
6+
* Must be run from a package directory (e.g., protoV2/core/)
7+
*/
28
import { readdirSync, statSync, copyFileSync, mkdirSync } from 'fs'
39
import { join, relative } from 'path'
410

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import path from 'path'
2+
import { readdirSync, statSync } from 'fs'
3+
4+
/**
5+
* Recursively find all TypeScript files in a directory.
6+
* @param {string} dir - Directory to search
7+
* @returns {string[]} Array of file paths
8+
*/
9+
export function findFiles(dir) {
10+
const result = []
11+
12+
function traverse(currentDir) {
13+
const files = readdirSync(currentDir)
14+
15+
for (const file of files) {
16+
const fullPath = path.join(currentDir, file)
17+
const stat = statSync(fullPath)
18+
19+
if (stat.isDirectory()) {
20+
traverse(fullPath)
21+
} else if (
22+
file.endsWith('.ts') &&
23+
!file.endsWith('.d.ts') &&
24+
!file.endsWith('.spec.ts')
25+
) {
26+
result.push(fullPath)
27+
}
28+
}
29+
}
30+
31+
traverse(dir)
32+
return result
33+
}
34+
35+
/**
36+
* Create tsup configuration for proto packages.
37+
* All proto packages share the same build configuration.
38+
* @returns {import('tsup').Options[]}
39+
*/
40+
export function createProtoTsupConfig() {
41+
// Automatically find all generated files
42+
const generatedFiles = findFiles('src/generated')
43+
44+
const entries = {
45+
index: 'src/index.ts',
46+
}
47+
48+
// Add all generated files as entry points
49+
generatedFiles.forEach((file) => {
50+
const key = path.relative('src', file).replace(/\.ts$/, '')
51+
entries[key] = file
52+
})
53+
54+
return [
55+
{
56+
entry: entries,
57+
outDir: 'proto-ts',
58+
format: ['esm'],
59+
dts: false, // Disable DTS generation in tsup (too slow/memory intensive)
60+
sourcemap: false,
61+
clean: true,
62+
splitting: false,
63+
treeshake: false,
64+
minify: false,
65+
bundle: false,
66+
outExtension: () => ({ js: '.js' }),
67+
external: [
68+
'@protobuf-ts/runtime',
69+
'@protobuf-ts/runtime-rpc',
70+
'@protobuf-ts/grpcweb-transport',
71+
],
72+
},
73+
]
74+
}

protoV2/abacus/generate-dts.mjs

Lines changed: 0 additions & 40 deletions
This file was deleted.

protoV2/abacus/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"generate": "sh ./gen.sh",
55
"generate:persist": "sh ./gen.sh --persist",
66
"generate:skip-clone": "sh ./gen.sh --skip-clone",
7-
"build": "tsup && node generate-dts.mjs && node fix-imports.mjs",
8-
"build:esm": "tsup --format esm && node generate-dts.mjs && node fix-imports.mjs",
7+
"build": "tsup && node ../_scripts/generate-dts.mjs && node ../_scripts/fix-imports.mjs",
8+
"build:esm": "tsup --format esm && node ../_scripts/generate-dts.mjs && node ../_scripts/fix-imports.mjs",
99
"build:cjs": "tsup --format cjs"
1010
},
1111
"devDependencies": {

protoV2/abacus/tsup.config.ts

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,4 @@
1-
import path from 'path'
21
import { defineConfig } from 'tsup'
3-
import { readdirSync, statSync } from 'fs'
2+
import { createProtoTsupConfig } from '../_scripts/tsup-base.config.mjs'
43

5-
function findFiles(dir: string): string[] {
6-
const result: string[] = []
7-
8-
function traverse(currentDir: string) {
9-
const files = readdirSync(currentDir)
10-
11-
for (const file of files) {
12-
const fullPath = path.join(currentDir, file)
13-
const stat = statSync(fullPath)
14-
15-
if (stat.isDirectory()) {
16-
traverse(fullPath)
17-
} else if (
18-
file.endsWith('.ts') &&
19-
!file.endsWith('.d.ts') &&
20-
!file.endsWith('.spec.ts')
21-
) {
22-
result.push(fullPath)
23-
}
24-
}
25-
}
26-
27-
traverse(dir)
28-
return result
29-
}
30-
31-
export default defineConfig(() => {
32-
// Automatically find all generated files
33-
const generatedFiles = findFiles('src/generated')
34-
35-
const entries: Record<string, string> = {
36-
index: 'src/index.ts',
37-
}
38-
39-
// Add all generated files as entry points
40-
generatedFiles.forEach((file: string) => {
41-
const key = path.relative('src', file).replace(/\.ts$/, '')
42-
entries[key] = file
43-
})
44-
45-
return [
46-
{
47-
entry: entries,
48-
outDir: 'proto-ts',
49-
format: ['esm'],
50-
dts: false, // Disable DTS generation in tsup (use generate-dts.mjs instead)
51-
sourcemap: false,
52-
clean: true,
53-
splitting: false,
54-
treeshake: false,
55-
minify: false,
56-
bundle: false,
57-
outExtension: () => ({ js: '.js' }),
58-
external: [
59-
'@protobuf-ts/runtime',
60-
'@protobuf-ts/runtime-rpc',
61-
'@protobuf-ts/grpcweb-transport',
62-
],
63-
},
64-
]
65-
})
4+
export default defineConfig(createProtoTsupConfig)

protoV2/core/fix-imports.mjs

Lines changed: 0 additions & 71 deletions
This file was deleted.

protoV2/core/generate-dts.mjs

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)