FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ops-codegraph-tool/src/shared/file-utils.ts at main · Deriddle-Dev/ops-codegraph-tool · GitHub
Deriddle-Dev
/
ops-codegraph-tool
Public
forked from
optave/ops-codegraph-tool
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ops-codegraph-tool
/
src
/
shared
/
file-utils.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
233 lines (214 loc) · 7.22 KB
Breadcrumbs
ops-codegraph-tool
/
src
/
shared
/
file-utils.ts
Copy path
File metadata and controls
233 lines (214 loc) · 7.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
223
224
225
226
227
228
229
230
231
232
233
import
fs
from
'node:fs'
;
import
path
from
'node:path'
;
import
{
LANGUAGE_REGISTRY
}
from
'../domain/parser.js'
;
import
{
debug
}
from
'../infrastructure/logger.js'
;
/**
* Resolve a file path relative to repoRoot, rejecting traversal outside the repo.
* Returns null if the resolved path escapes repoRoot.
*/
export
function
safePath
(
repoRoot
:
string
,
file
:
string
)
:
string
|
null
{
const
resolved
=
path
.
resolve
(
repoRoot
,
file
)
;
if
(
!
resolved
.
startsWith
(
repoRoot
+
path
.
sep
)
&&
resolved
!==
repoRoot
)
return
null
;
return
resolved
;
}
interface
ReadSourceRangeOpts
{
excerptLines
?:
number
;
}
export
function
readSourceRange
(
repoRoot
:
string
,
file
:
string
,
startLine
:
number
|
undefined
,
endLine
:
number
|
undefined
,
opts
:
ReadSourceRangeOpts
=
{
}
,
)
:
string
|
null
{
try
{
const
absPath
=
safePath
(
repoRoot
,
file
)
;
if
(
!
absPath
)
return
null
;
const
content
=
fs
.
readFileSync
(
absPath
,
'utf-8'
)
;
const
lines
=
content
.
split
(
'\n'
)
;
const
excerptLines
=
opts
.
excerptLines
??
50
;
const
start
=
Math
.
max
(
0
,
(
startLine
||
1
)
-
1
)
;
const
end
=
Math
.
min
(
lines
.
length
,
endLine
||
(
startLine
||
1
)
+
excerptLines
)
;
return
lines
.
slice
(
start
,
end
)
.
join
(
'\n'
)
;
}
catch
(
e
:
unknown
)
{
debug
(
`readSourceRange failed for
${
file
}
:
${
(
e
as
Error
)
.
message
}
`
)
;
return
null
;
}
}
interface
ExtractSummaryOpts
{
jsdocEndScanLines
?:
number
;
jsdocOpenScanLines
?:
number
;
summaryMaxChars
?:
number
;
}
/** Truncate text to maxChars, appending "..." if truncated. */
function
truncate
(
text
:
string
,
maxChars
:
number
)
:
string
{
return
text
.
length
>
maxChars
?
`
${
text
.
slice
(
0
,
maxChars
)
}
...`
:
text
;
}
/** Try to extract a single-line comment (// or #) above the definition. */
function
extractSingleLineComment
(
fileLines
:
string
[
]
,
idx
:
number
,
scanLines
:
number
,
maxChars
:
number
,
)
:
string
|
null
{
for
(
let
i
=
idx
;
i
>=
Math
.
max
(
0
,
idx
-
scanLines
)
;
i
--
)
{
const
trimmed
=
fileLines
[
i
]
!
.
trim
(
)
;
if
(
trimmed
.
endsWith
(
'*/'
)
)
return
null
;
// hit a block comment — defer to JSDoc extractor
if
(
trimmed
.
startsWith
(
'//'
)
||
trimmed
.
startsWith
(
'#'
)
)
{
const
text
=
trimmed
.
replace
(
/
^
\/
\/
\s
*
/
,
''
)
.
replace
(
/
^
#
\s
*
/
,
''
)
.
trim
(
)
;
return
truncate
(
text
,
maxChars
)
;
}
if
(
trimmed
!==
''
&&
!
trimmed
.
startsWith
(
'*'
)
&&
!
trimmed
.
startsWith
(
'/*'
)
)
return
null
;
}
return
null
;
}
/** Find the line index where a block comment (*/) ends, scanning upward from idx. */
function
findJsdocEndLine
(
fileLines
:
string
[
]
,
idx
:
number
,
scanLines
:
number
)
:
number
{
for
(
let
i
=
idx
;
i
>=
Math
.
max
(
0
,
idx
-
scanLines
)
;
i
--
)
{
const
trimmed
=
fileLines
[
i
]
!
.
trim
(
)
;
if
(
trimmed
.
endsWith
(
'*/'
)
)
return
i
;
if
(
trimmed
!==
''
&&
!
trimmed
.
startsWith
(
'*'
)
&&
!
trimmed
.
startsWith
(
'/*'
)
&&
!
trimmed
.
startsWith
(
'//'
)
&&
!
trimmed
.
startsWith
(
'#'
)
)
{
break
;
}
}
return
-
1
;
}
/** Extract the first description line from a JSDoc block ending at jsdocEnd. */
function
extractJsdocDescription
(
fileLines
:
string
[
]
,
jsdocEnd
:
number
,
openScanLines
:
number
,
maxChars
:
number
,
)
:
string
|
null
{
for
(
let
i
=
jsdocEnd
;
i
>=
Math
.
max
(
0
,
jsdocEnd
-
openScanLines
)
;
i
--
)
{
if
(
!
fileLines
[
i
]
!
.
trim
(
)
.
startsWith
(
'/**'
)
)
continue
;
for
(
let
j
=
i
+
1
;
j
<=
jsdocEnd
;
j
++
)
{
const
docLine
=
fileLines
[
j
]
!
.
trim
(
)
.
replace
(
/
^
\*
\s
?
/
,
''
)
.
trim
(
)
;
if
(
docLine
&&
!
docLine
.
startsWith
(
'@'
)
&&
docLine
!==
'/'
&&
docLine
!==
'*/'
)
{
return
truncate
(
docLine
,
maxChars
)
;
}
}
break
;
}
return
null
;
}
export
function
extractSummary
(
fileLines
:
string
[
]
|
null
,
line
:
number
|
undefined
,
opts
:
ExtractSummaryOpts
=
{
}
,
)
:
string
|
null
{
if
(
!
fileLines
||
!
line
||
line
<=
1
)
return
null
;
const
idx
=
line
-
2
;
// line above the definition (0-indexed)
const
jsdocEndScanLines
=
opts
.
jsdocEndScanLines
??
10
;
const
jsdocOpenScanLines
=
opts
.
jsdocOpenScanLines
??
20
;
const
summaryMaxChars
=
opts
.
summaryMaxChars
??
100
;
// Try single-line comment first
const
singleLine
=
extractSingleLineComment
(
fileLines
,
idx
,
jsdocEndScanLines
,
summaryMaxChars
)
;
if
(
singleLine
)
return
singleLine
;
// Try JSDoc block comment
const
jsdocEnd
=
findJsdocEndLine
(
fileLines
,
idx
,
jsdocEndScanLines
)
;
if
(
jsdocEnd
>=
0
)
{
return
extractJsdocDescription
(
fileLines
,
jsdocEnd
,
jsdocOpenScanLines
,
summaryMaxChars
)
;
}
return
null
;
}
interface
ExtractSignatureOpts
{
signatureGatherLines
?:
number
;
}
export
interface
Signature
{
params
:
string
|
null
;
returnType
:
string
|
null
;
}
/** Per-language signature patterns. Each entry has a regex and an extractor for return type. */
const
SIGNATURE_PATTERNS
:
Array
<
{
regex
:
RegExp
;
returnType
:
(
m
:
RegExpMatchArray
)
=>
string
|
null
;
}
>
=
[
// JS/TS: function name(params) or async function
{
regex
:
/
(?:
e
x
p
o
r
t
\s
+
)
?
(?:
a
s
y
n
c
\s
+
)
?
f
u
n
c
t
i
o
n
\s
*
\*
?
\s
*
\w
*
\s
*
\(
(
[
^
)
]
*
)
\)
\s
*
(?:
:
\s
*
(
[
^
\n
{
]
+
)
)
?
/
,
returnType
:
(
m
)
=>
(
m
[
2
]
?
m
[
2
]
.
trim
(
)
.
replace
(
/
\s
*
\{
$
/
,
''
)
:
null
)
,
}
,
// Arrow: const name = (params) => or (params):ReturnType =>
{
regex
:
/
=
\s
*
(?:
a
s
y
n
c
\s
+
)
?
\(
(
[
^
)
]
*
)
\)
\s
*
(?:
:
\s
*
(
[
^
=
>
\n
{
]
+
)
)
?
\s
*
=
>
/
,
returnType
:
(
m
)
=>
(
m
[
2
]
?
m
[
2
]
.
trim
(
)
:
null
)
,
}
,
// Python: def name(params) -> return:
{
regex
:
/
d
e
f
\s
+
\w
+
\s
*
\(
(
[
^
)
]
*
)
\)
\s
*
(?:
-
>
\s
*
(
[
^
:
\n
]
+
)
)
?
/
,
returnType
:
(
m
)
=>
(
m
[
2
]
?
m
[
2
]
.
trim
(
)
:
null
)
,
}
,
// Go: func (recv) name(params) (returns)
{
regex
:
/
f
u
n
c
\s
+
(?:
\(
[
^
)
]
*
\)
\s
+
)
?
\w
+
\s
*
\(
(
[
^
)
]
*
)
\)
\s
*
(?:
\(
(
[
^
)
]
+
)
\)
|
(
\w
[
^
\n
{
]
*
)
)
?
/
,
returnType
:
(
m
)
=>
(
m
[
2
]
||
m
[
3
]
||
''
)
.
trim
(
)
||
null
,
}
,
// Rust: fn name(params) -> ReturnType
{
regex
:
/
f
n
\s
+
\w
+
\s
*
\(
(
[
^
)
]
*
)
\)
\s
*
(?:
-
>
\s
*
(
[
^
\n
{
]
+
)
)
?
/
,
returnType
:
(
m
)
=>
(
m
[
2
]
?
m
[
2
]
.
trim
(
)
:
null
)
,
}
,
]
;
export
function
extractSignature
(
fileLines
:
string
[
]
|
null
,
line
:
number
|
undefined
,
opts
:
ExtractSignatureOpts
=
{
}
,
)
:
Signature
|
null
{
if
(
!
fileLines
||
!
line
)
return
null
;
const
idx
=
line
-
1
;
const
signatureGatherLines
=
opts
.
signatureGatherLines
??
5
;
const
chunk
=
fileLines
.
slice
(
idx
,
Math
.
min
(
fileLines
.
length
,
idx
+
signatureGatherLines
)
)
.
join
(
'\n'
)
;
for
(
const
pattern
of
SIGNATURE_PATTERNS
)
{
const
m
=
chunk
.
match
(
pattern
.
regex
)
;
if
(
m
)
{
return
{
params
:
m
[
1
]
!
.
trim
(
)
||
null
,
returnType
:
pattern
.
returnType
(
m
)
,
}
;
}
}
return
null
;
}
export
function
createFileLinesReader
(
repoRoot
:
string
)
:
(
file
:
string
)
=>
string
[
]
|
null
{
const
cache
=
new
Map
<
string
,
string
[
]
|
null
>
(
)
;
return
function
getFileLines
(
file
:
string
)
:
string
[
]
|
null
{
if
(
cache
.
has
(
file
)
)
return
cache
.
get
(
file
)
!
;
try
{
const
absPath
=
safePath
(
repoRoot
,
file
)
;
if
(
!
absPath
)
{
cache
.
set
(
file
,
null
)
;
return
null
;
}
const
lines
=
fs
.
readFileSync
(
absPath
,
'utf-8'
)
.
split
(
'\n'
)
;
cache
.
set
(
file
,
lines
)
;
return
lines
;
}
catch
(
e
:
unknown
)
{
debug
(
`getFileLines failed for
${
file
}
:
${
(
e
as
Error
)
.
message
}
`
)
;
cache
.
set
(
file
,
null
)
;
return
null
;
}
}
;
}
export
function
isFileLikeTarget
(
target
:
string
)
:
boolean
{
if
(
target
.
includes
(
'/'
)
||
target
.
includes
(
'\\'
)
)
return
true
;
const
ext
=
path
.
extname
(
target
)
.
toLowerCase
(
)
;
if
(
!
ext
)
return
false
;
for
(
const
entry
of
LANGUAGE_REGISTRY
)
{
if
(
entry
.
extensions
.
includes
(
ext
)
)
return
true
;
}
return
false
;
}
Back
|
FazBrowse Home
|
New Git URL