jan/core/src/node/extension/manager.ts
Louis 5250061c11
feat: Jan Server, API and decoupled clients (#948)
* chore: expose fs apis

* chore: correct electron import path

* update download api

Signed-off-by: James <james@jan.ai>

* update chat_completion

Signed-off-by: James <james@jan.ai>

* fix electron import

Signed-off-by: James <james@jan.ai>

* feat: adding API support at 1337 (#991)

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>

* feat: Add /chat/completion api and handler

* chore: add todo for modelList

* chore: read engine.json for openai chat_completion (#1030)

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>

* refactor: move routes to shared node module

* refactor: exported modules from core with types (#1172)

* refactor: exported modules from core with types

* fix: fix file reading args

* refactor: fileManager handles

* fix: app issues with server refactoring

* refactor: shared server module (#1210)

* chore: resolve main

* chore: update makefile

---------

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
Co-authored-by: NamH <NamNh0122@gmail.com>
Co-authored-by: hiro <vuonghoainam.work@gmail.com>
2023-12-28 17:57:39 +07:00

62 lines
1.8 KiB
TypeScript

import { join, resolve } from "path";
import { existsSync, mkdirSync, writeFileSync } from "fs";
import { init } from "./index";
import { homedir } from "os"
/**
* Manages extension installation and migration.
*/
export const userSpacePath = join(homedir(), "jan");
export class ExtensionManager {
public static instance: ExtensionManager = new ExtensionManager();
extensionsPath: string | undefined = join(userSpacePath, "extensions");
constructor() {
if (ExtensionManager.instance) {
return ExtensionManager.instance;
}
}
/**
* Sets up the extensions by initializing the `extensions` module with the `confirmInstall` and `extensionsPath` options.
* The `confirmInstall` function always returns `true` to allow extension installation.
* The `extensionsPath` option specifies the path to install extensions to.
*/
setupExtensions() {
init({
// Function to check from the main process that user wants to install a extension
confirmInstall: async (_extensions: string[]) => {
return true;
},
// Path to install extension to
extensionsPath: join(userSpacePath, "extensions"),
});
}
setExtensionsPath(extPath: string) {
// Create folder if it does not exist
let extDir;
try {
extDir = resolve(extPath);
if (extDir.length < 2) throw new Error();
if (!existsSync(extDir)) mkdirSync(extDir);
const extensionsJson = join(extDir, "extensions.json");
if (!existsSync(extensionsJson))
writeFileSync(extensionsJson, "{}", "utf8");
this.extensionsPath = extDir;
} catch (error) {
throw new Error("Invalid path provided to the extensions folder");
}
}
getExtensionsFile() {
return join(this.extensionsPath ?? "", "extensions.json");
}
}