-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.mts
118 lines (103 loc) · 4.14 KB
/
utils.mts
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
import fs from 'fs'
import os from 'os'
import path from 'path'
export function readConfig() {
const homeDir = os.homedir()
const configFile = path.join(homeDir, '.SourceSailor', 'config.json')
if (fs.existsSync(configFile)) {
try {
const config = JSON.parse(fs.readFileSync(configFile, 'utf8'))
return config
} catch (error) {
console.error('Error parsing config file:', error)
}
return {}
}
return {}
}
export function writeConfig(config: any) {
const homeDir = os.homedir()
const configDir = path.join(homeDir, '.SourceSailor')
const configFile = path.join(configDir, 'config.json')
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, {recursive: true})
}
fs.writeFileSync(configFile, JSON.stringify(config, null, 2))
}
export function addAnalysisInGitIgnore(projectRoot: string) {
// First check if the project root has gitignore file, if the file is not found don't create one.
// If the file is found, append .SourceSailor/analysis to the file.
const gitignoreFile = path.join(projectRoot, '.gitignore')
if (fs.existsSync(gitignoreFile)) {
const gitignoreContent = fs.readFileSync(gitignoreFile, 'utf8')
if (!gitignoreContent.includes('.SourceSailor/analysis')) {
fs.appendFileSync(gitignoreFile, '\n.SourceSailor/analysis')
}
}
}
export function writeAnalysis(projectRoot: string, analysisName: string, analysisContent: any, isJson = false, isProjectRoot = false) {
const analysisDir = isProjectRoot ? path.join(projectRoot, '.SourceSailor', 'analysis') : path.join(projectRoot, 'analysis')
if (!fs.existsSync(analysisDir)) {
fs.mkdirSync(analysisDir, {recursive: true})
}
if (isJson) {
const analysisFile = path.join(analysisDir, `${analysisName}.json`)
fs.writeFileSync(analysisFile, JSON.stringify(analysisContent, null, 4))
} else {
const analysisFile = path.join(analysisDir, `${analysisName}.md`)
fs.writeFileSync(analysisFile, analysisContent)
}
}
export function writeError(projectRoot: string, errorType: string, errorContent: any, errorMssage: string) {
const errorDir = path.join(projectRoot, 'errors')
if (!fs.existsSync(errorDir)) {
fs.mkdirSync(errorDir, {recursive: true})
}
const errorFile = path.join(errorDir, `${errorType}.txt`)
fs.writeFileSync(errorFile, `${errorContent}\n\n${errorMssage}`)
}
export function getAnalysis(projectRoot: string, isProjectRoot: boolean) {
const analysis: Record<string, any> = {}
const analysisDir = isProjectRoot ? path.join(projectRoot, '.SourceSailor', 'analysis') : path.join(projectRoot, 'analysis')
if (!fs.existsSync(analysisDir)) {
return analysis
}
const entries = fs.readdirSync(analysisDir)
for (const entry of entries) {
const fullPath = path.join(analysisDir, entry)
const isDirectory = fs.statSync(fullPath).isDirectory()
// console.log({entry})
const fileName = path.basename(entry, path.extname(entry))
if (isDirectory) {
analysis[fileName] = getAnalysis(fullPath, false)
} else {
analysis[fileName] = fs.readFileSync(fullPath, 'utf-8')
}
}
return analysis
}
export async function processStream<T>(iterator: AsyncIterator<T>, processChunk: (chunk: T) => string): Promise<string> {
let fullResponse = ''
const {value, done} = await iterator.next()
while (done) {
const message = processChunk(value)
process.stdout.write(message)
fullResponse += message
}
return fullResponse
}
export function maskSensitiveInfo(input: string): string {
// This is a simple implementation. You might want to enhance it based on your specific needs.
const sensitivePatterns = [
/api[-_]?key/i,
/password/i,
/secret/i,
/token/i,
/credential/i
]
let maskedInput = input
for (const pattern of sensitivePatterns) {
maskedInput = maskedInput.replace(new RegExp(`(${pattern.source}\\s*[=:])\\s*["']?[^"'\\s]+["']?`, 'gi'), '$1 "********"')
}
return maskedInput
}