continue-custom/core/util/paths.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

269 lines
7.5 KiB
TypeScript

import dotenv from "dotenv";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { defaultConfig, defaultConfigJetBrains } from "../config/default.js";
import Types from "../config/types.js";
import { IdeType, SerializedContinueConfig } from "../index.js";
export function getContinueGlobalPath(): string {
// This is ~/.continue on mac/linux
const continuePath = path.join(os.homedir(), ".continue");
if (!fs.existsSync(continuePath)) {
fs.mkdirSync(continuePath);
}
return continuePath;
}
export function getSessionsFolderPath(): string {
const sessionsPath = path.join(getContinueGlobalPath(), "sessions");
if (!fs.existsSync(sessionsPath)) {
fs.mkdirSync(sessionsPath);
}
return sessionsPath;
}
export function getIndexFolderPath(): string {
const indexPath = path.join(getContinueGlobalPath(), "index");
if (!fs.existsSync(indexPath)) {
fs.mkdirSync(indexPath);
}
return indexPath;
}
export function getGlobalContextFilePath(): string {
return path.join(getIndexFolderPath(), "globalContext.json");
}
export function getSessionFilePath(sessionId: string): string {
return path.join(getSessionsFolderPath(), `${sessionId}.json`);
}
export function getSessionsListPath(): string {
const filepath = path.join(getSessionsFolderPath(), "sessions.json");
if (!fs.existsSync(filepath)) {
fs.writeFileSync(filepath, JSON.stringify([]));
}
return filepath;
}
export function getConfigJsonPath(ideType: IdeType = "vscode"): string {
const p = path.join(getContinueGlobalPath(), "config.json");
if (!fs.existsSync(p)) {
if (ideType === "jetbrains") {
fs.writeFileSync(p, JSON.stringify(defaultConfigJetBrains, null, 2));
} else {
fs.writeFileSync(p, JSON.stringify(defaultConfig, null, 2));
}
}
return p;
}
export function getConfigTsPath(): string {
const p = path.join(getContinueGlobalPath(), "config.ts");
if (!fs.existsSync(p)) {
fs.writeFileSync(
p,
`export function modifyConfig(config: Config): Config {
return config;
}`,
);
}
const typesPath = path.join(getContinueGlobalPath(), "types");
if (!fs.existsSync(typesPath)) {
fs.mkdirSync(typesPath);
}
const corePath = path.join(typesPath, "core");
if (!fs.existsSync(corePath)) {
fs.mkdirSync(corePath);
}
const packageJsonPath = path.join(getContinueGlobalPath(), "package.json");
if (!fs.existsSync(packageJsonPath)) {
fs.writeFileSync(
packageJsonPath,
JSON.stringify({
name: "continue-config",
version: "1.0.0",
description: "My Continue Configuration",
main: "config.js",
}),
);
}
fs.writeFileSync(path.join(corePath, "index.d.ts"), Types);
return p;
}
export function getConfigJsPath(): string {
// Do not create automatically
return path.join(getContinueGlobalPath(), "out", "config.js");
}
export function getTsConfigPath(): string {
const tsConfigPath = path.join(getContinueGlobalPath(), "tsconfig.json");
if (!fs.existsSync(tsConfigPath)) {
fs.writeFileSync(
tsConfigPath,
JSON.stringify(
{
compilerOptions: {
target: "ESNext",
useDefineForClassFields: true,
lib: ["DOM", "DOM.Iterable", "ESNext"],
allowJs: true,
skipLibCheck: true,
esModuleInterop: false,
allowSyntheticDefaultImports: true,
strict: true,
forceConsistentCasingInFileNames: true,
module: "System",
moduleResolution: "Node",
noEmit: false,
noEmitOnError: false,
outFile: "./out/config.js",
typeRoots: ["./node_modules/@types", "./types"],
},
include: ["./config.ts"],
},
null,
2,
),
);
}
return tsConfigPath;
}
export function devDataPath(): string {
const sPath = path.join(getContinueGlobalPath(), "dev_data");
if (!fs.existsSync(sPath)) {
fs.mkdirSync(sPath);
}
return sPath;
}
export function getDevDataSqlitePath(): string {
return path.join(devDataPath(), "devdata.sqlite");
}
export function getDevDataFilePath(fileName: string): string {
return path.join(devDataPath(), `${fileName}.jsonl`);
}
export function editConfigJson(
callback: (config: SerializedContinueConfig) => SerializedContinueConfig,
) {
const config = fs.readFileSync(getConfigJsonPath(), "utf8");
let configJson = JSON.parse(config);
configJson = callback(configJson);
fs.writeFileSync(getConfigJsonPath(), JSON.stringify(configJson, null, 2));
return configJson;
}
function getMigrationsFolderPath(): string {
const migrationsPath = path.join(getContinueGlobalPath(), ".migrations");
if (!fs.existsSync(migrationsPath)) {
fs.mkdirSync(migrationsPath);
}
return migrationsPath;
}
export function migrate(id: string, callback: () => void) {
const migrationsPath = getMigrationsFolderPath();
const migrationPath = path.join(migrationsPath, id);
if (!fs.existsSync(migrationPath)) {
fs.writeFileSync(migrationPath, "");
callback();
}
}
export function getIndexSqlitePath(): string {
return path.join(getIndexFolderPath(), "index.sqlite");
}
export function getLanceDbPath(): string {
return path.join(getIndexFolderPath(), "lancedb");
}
export function getTabAutocompleteCacheSqlitePath(): string {
return path.join(getIndexFolderPath(), "autocompleteCache.sqlite");
}
export function getDocsSqlitePath(): string {
return path.join(getIndexFolderPath(), "docs.sqlite");
}
export function getRemoteConfigsFolderPath(): string {
const dir = path.join(getContinueGlobalPath(), ".configs");
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir;
}
export function getPathToRemoteConfig(remoteConfigServerUrl: string): string {
let url: URL | undefined = undefined;
try {
url =
typeof remoteConfigServerUrl !== "string" || remoteConfigServerUrl === ""
? undefined
: new URL(remoteConfigServerUrl);
} catch (e) {}
const dir = path.join(getRemoteConfigsFolderPath(), url?.hostname ?? "None");
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir;
}
export function getConfigJsonPathForRemote(
remoteConfigServerUrl: string,
): string {
return path.join(getPathToRemoteConfig(remoteConfigServerUrl), "config.json");
}
export function getConfigJsPathForRemote(
remoteConfigServerUrl: string,
): string {
return path.join(getPathToRemoteConfig(remoteConfigServerUrl), "config.js");
}
export function getContinueDotEnv(): { [key: string]: string } {
const filepath = path.join(getContinueGlobalPath(), ".env");
if (fs.existsSync(filepath)) {
return dotenv.parse(fs.readFileSync(filepath));
}
return {};
}
export function getCoreLogsPath(): string {
return path.join(getContinueGlobalPath(), "core.log");
}
export function getGlobalPromptsPath(): string {
return path.join(getContinueGlobalPath(), ".prompts");
}
export function readAllGlobalPromptFiles(
folderPath: string = getGlobalPromptsPath(),
): { path: string; content: string }[] {
if (!fs.existsSync(folderPath)) {
return [];
}
const files = fs.readdirSync(folderPath);
const promptFiles: { path: string; content: string }[] = [];
files.forEach((file) => {
const filepath = path.join(folderPath, file);
const stats = fs.statSync(filepath);
if (stats.isDirectory()) {
const nestedPromptFiles = readAllGlobalPromptFiles(filepath);
promptFiles.push(...nestedPromptFiles);
} else {
const content = fs.readFileSync(filepath, "utf8");
promptFiles.push({ path: filepath, content });
}
});
return promptFiles;
}