Skip to content

Commit

Permalink
feat: refector logger and add logger for webview
Browse files Browse the repository at this point in the history
  • Loading branch information
2214962083 committed Sep 23, 2024
1 parent f408bbf commit bfa86a8
Show file tree
Hide file tree
Showing 14 changed files with 338 additions and 266 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@
"langchain",
"langgraph",
"Mhchem",
"multistream",
"Nicepkg",
"nodir",
"Nolebase",
"Ollama",
"onnxruntime",
"openai",
"pino",
"Pipfile",
"Pluggable",
"pyproject",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@
"apache-arrow": "^17.0.0",
"autoprefixer": "^10.4.20",
"babel-plugin-react-compiler": "latest",
"chalk": "^5.3.0",
"cheerio": "^1.0.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
Expand Down
7 changes: 5 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 72 additions & 28 deletions src/extension/file-utils/paths.ts
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()
9 changes: 9 additions & 0 deletions src/extension/file-utils/vscode-fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable unused-imports/no-unused-vars */
import JSONC from 'comment-json'
import * as vscode from 'vscode'

export class VsCodeFS {
Expand Down Expand Up @@ -105,4 +106,12 @@ export class VsCodeFS {
return false
}
}

static async writeJsonFile(filePath: string, data: any): Promise<void> {
await this.writeFile(filePath, JSON.stringify(data, null, 2))
}

static async readJsonFile<T extends object>(filePath: string): Promise<T> {
return JSONC.parse(await this.readFile(filePath, 'utf8')) as T
}
}
Loading

0 comments on commit bfa86a8

Please sign in to comment.