-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refector logger and add logger for webview
- Loading branch information
1 parent
f408bbf
commit bfa86a8
Showing
14 changed files
with
338 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,93 @@ | ||
import crypto from 'crypto' | ||
import os from 'os' | ||
import path from 'path' | ||
import JSONC from 'comment-json' | ||
import { getWorkspaceFolder } from '@extension/utils' | ||
import fs from 'fs-extra' | ||
|
||
import { VsCodeFS } from './vscode-fs' | ||
|
||
const AIDE_DIR = process.env.AIDE_GLOBAL_DIR ?? path.join(os.homedir(), '.aide') | ||
|
||
export class PathManager { | ||
static ensureDir(dirPath: string): string { | ||
fs.ensureDirSync(dirPath) | ||
return dirPath | ||
} | ||
export const getExt = (filePath: string): string => | ||
path.extname(filePath).slice(1) | ||
|
||
export class AidePaths { | ||
private static instance: AidePaths | ||
|
||
static getPath(...segments: string[]): string { | ||
return PathManager.ensureDir(path.join(AIDE_DIR, ...segments)) | ||
private aideDir: string | ||
|
||
private constructor() { | ||
this.aideDir = AIDE_DIR | ||
} | ||
|
||
static getFilePath(...segments: string[]): string { | ||
return path.join(AIDE_DIR, ...segments) | ||
public static getInstance(): AidePaths { | ||
if (!AidePaths.instance) { | ||
AidePaths.instance = new AidePaths() | ||
} | ||
return AidePaths.instance | ||
} | ||
|
||
static writeJsonFile(filePath: string, data: any): void { | ||
fs.writeFileSync(filePath, JSON.stringify(data, null, 2)) | ||
private ensurePath(pathToEnsure: string, isDirectory: boolean): string { | ||
if (isDirectory) { | ||
fs.ensureDirSync(pathToEnsure) | ||
} else { | ||
fs.ensureFileSync(pathToEnsure) | ||
} | ||
return pathToEnsure | ||
} | ||
|
||
static readJsonFile(filePath: string): any { | ||
return JSONC.parse(fs.readFileSync(filePath, 'utf8')) | ||
private joinAideGlobalPath( | ||
isDirectory: boolean, | ||
...segments: string[] | ||
): string { | ||
const fullPath = path.join(this.aideDir, ...segments) | ||
return this.ensurePath(fullPath, isDirectory) | ||
} | ||
|
||
static getExt(filePath: string): string { | ||
return path.extname(filePath).slice(1) | ||
private joinAideNamespacePath( | ||
isDirectory: boolean, | ||
...segments: string[] | ||
): string { | ||
const fullPath = path.join(this.aideDir, this.getNamespace(), ...segments) | ||
return this.ensurePath(fullPath, isDirectory) | ||
} | ||
} | ||
|
||
export const Paths = { | ||
aideDir: AIDE_DIR, | ||
config: () => PathManager.getFilePath('index', 'config.json'), | ||
sessionFile: (sessionId: string) => | ||
PathManager.getFilePath('sessions', `${sessionId}.json`), | ||
sessionsList: () => { | ||
const filePath = PathManager.getFilePath('sessions', 'sessions.json') | ||
getSessionFilePath = (sessionId: string) => | ||
this.joinAideNamespacePath(false, 'sessions', `${sessionId}.json`) | ||
|
||
getSessionsListPath = async () => { | ||
const filePath = this.joinAideNamespacePath( | ||
false, | ||
'sessions', | ||
'sessions.json' | ||
) | ||
|
||
if (!fs.existsSync(filePath)) { | ||
PathManager.writeJsonFile(filePath, []) | ||
await VsCodeFS.writeJsonFile(filePath, []) | ||
} | ||
|
||
return filePath | ||
}, | ||
lanceDb: () => PathManager.getPath('index', 'lancedb'), | ||
logs: () => PathManager.getPath('logs') | ||
} | ||
|
||
getLanceDbPath = () => this.joinAideNamespacePath(true, 'lancedb') | ||
|
||
getLogsPath = () => this.joinAideNamespacePath(true, 'logs') | ||
|
||
getNamespace = () => { | ||
const workspacePath = getWorkspaceFolder().uri.fsPath | ||
|
||
const workspaceName = path | ||
.basename(workspacePath) | ||
.replace(/[^a-zA-Z0-9]/g, '_') | ||
|
||
const workspaceFullPathHash = crypto | ||
.createHash('md5') | ||
.update(workspacePath) | ||
.digest('hex') | ||
.substring(0, 8) | ||
|
||
return `${workspaceName}_${workspaceFullPathHash}`.toLowerCase() | ||
} | ||
} | ||
|
||
export const aidePaths = AidePaths.getInstance() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.