Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Fix wrong path in nodejs when using reload #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/terminal/TerminalHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
);
})
);
}
Expand Down
51 changes: 50 additions & 1 deletion src/utils/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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);
}