diff --git a/src/terminal/TerminalHelper.ts b/src/terminal/TerminalHelper.ts index ce365e2..dbc4466 100644 --- a/src/terminal/TerminalHelper.ts +++ b/src/terminal/TerminalHelper.ts @@ -8,7 +8,7 @@ import * as vscode from "vscode"; import "./TerminalConst"; import * as path from "path"; import { CommandType, TerminalKeys, TerminalState } from "./TerminalConst"; -import { getLiteLoaderpath } from "../utils/FileUtils"; +import { getLiteLoaderpath, getFilePath } from "../utils/FileUtils"; // import { getBDSCwdPath, getBDSPath } from "../utils/WorkspaceUtil"; export class TerminalHelper { static terminal: vscode.Terminal | undefined; @@ -44,16 +44,22 @@ export class TerminalHelper { this.stopConsole(); }), vscode.commands.registerCommand("extension.llseaids.load", (uri) => { - const _path = uri.fsPath; - this.managePlugin(CommandType.LOAD, _path); + this.managePlugin( + CommandType.LOAD, + getFilePath(uri.fsPath, false) + ); }), vscode.commands.registerCommand("extension.llseaids.unload", (uri) => { - const _path = path.parse(uri.fsPath).base; - this.managePlugin(CommandType.UNLOAD, _path); + this.managePlugin( + CommandType.UNLOAD, + getFilePath(uri.fsPath, true) + ); }), vscode.commands.registerCommand("extension.llseaids.reload", (uri) => { - const _path = path.parse(uri.fsPath).base; - this.managePlugin(CommandType.RELOAD, _path); + this.managePlugin( + CommandType.RELOAD, + getFilePath(uri.fsPath, true) + ); }) ); } diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index 81edff0..1529854 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -15,7 +15,7 @@ import StreamZip = require("node-stream-zip"); import * as vscode from "vscode"; import { randomUUID } from "crypto"; import { rejects } from "assert"; -import { resolve } from "path"; +import * as path from "path"; import { ConfigScope, Sections } from "../data/ConfigScope"; /** @@ -165,3 +165,52 @@ export function getLiteLoaderpath(): string { vscode.commands.executeCommand("extension.llseaids.config"); throw new Error("BDSPATH未配置"); } + +function _returnFilePath(filePath: string, getBase: boolean): string { + if(getBase) { + return path.parse(filePath).base; + } + return filePath; +} + +/** + * Obtain different filePaths based on the type of file + * @param fsPath + * @param getBase + * @returns + */ +export function getFilePath(fsPath: string, getBase: boolean): string { + const cwdPath = getLiteLoaderpath(); + const pluginsPath = path.join(cwdPath, "plugins"); + const nodePath = path.join(pluginsPath, "nodejs"); + + const relativePath = path.relative(nodePath, fsPath); + // path not in nodejs, return directly + if (relativePath.startsWith("..")) { + return _returnFilePath(fsPath, getBase); + } + + // path in nodejs + const pathArray = relativePath.split(path.sep); + const nodeFileName = pathArray[0]; + const packagePath = path.join(nodePath, nodeFileName, "package.json"); + if (!fs.existsSync(packagePath)) { + return _returnFilePath(fsPath, getBase); + } + const packageJson = JSON.parse( + fs.readFileSync(packagePath, "utf-8") + ); + + // determine if it is a nodejs entry file + const resolvedFsPath = path.resolve(fsPath); + const indexPath = path.join(nodePath, nodeFileName, packageJson.main); + if (indexPath.toLowerCase() !== resolvedFsPath.toLowerCase()) { + // return as a QuickJS file + return _returnFilePath(resolvedFsPath, getBase); + } + // return as nodejs + if (getBase) { + return packageJson.name; + } + return path.join(nodePath, nodeFileName); +}