From 7133e864cd6e59541d22d44ef6c682ea32f15c28 Mon Sep 17 00:00:00 2001 From: superx101 <2467028684@qq.com> Date: Mon, 27 Nov 2023 15:03:18 +0800 Subject: [PATCH 1/2] fix: wrong path in nodejs when using reload --- src/terminal/TerminalHelper.ts | 20 +++++++++----- src/utils/FileUtils.ts | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) 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..9a349c1 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -17,6 +17,7 @@ import { randomUUID } from "crypto"; import { rejects } from "assert"; import { resolve } from "path"; import { ConfigScope, Sections } from "../data/ConfigScope"; +import path = require("path"); /** * 同步查找文件匹配 @@ -165,3 +166,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); +} \ No newline at end of file From 5c68583e1685f3fdd0cef1f64ee77d794af05210 Mon Sep 17 00:00:00 2001 From: superx101 <95033886+superx101@users.noreply.github.com> Date: Mon, 27 Nov 2023 15:41:21 +0800 Subject: [PATCH 2/2] style: update import in FileUtils.ts --- src/utils/FileUtils.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index 9a349c1..1529854 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -15,9 +15,8 @@ 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"; -import path = require("path"); /** * 同步查找文件匹配 @@ -214,4 +213,4 @@ export function getFilePath(fsPath: string, getBase: boolean): string { return packageJson.name; } return path.join(nodePath, nodeFileName); -} \ No newline at end of file +}