Implement a vscode plug-in: automatically switch nvm based on the .nvmrc file when opening multiple vscode projects

Insert image description here

Development background and final functionality

When you need to maintain some old projects and develop new projects at the same time, switching nvm is very annoying.
Finally, the vscode plug-in is implemented: each vscode instance opens a project, and the version can be automatically switched when switching vscode instances (requires a .nvmrc file in the project root directory)

Plug-in download

vscode plug-in market searchvscode-nvmrcInsert image description here

Design ideas

Create a new file in the project root directory .nvmrc. This is the official file of nvm. nvm useThis file will be automatically searched when used. Windows systems generally use this. nvm-for-windowsIt is a Windows version maintained by another developer and does not support nvm usesearching .nvmrc
. However, this does not It affects the functions implemented in the vscode plug-in nvm use, but it .nvmrcis the official document of nvm.

Not much to say, the code is very simple. The file vscode.window.onDidChangeWindowStateis read in the vscode plug-in method .nvmrc, e.focusedwhich means it is triggered when the vscode window is displayed. It can be triggered when the vscode instance is switched, and then calls the child_process.exec runnvm use

import * as vscode from "vscode";
import {
    
     exec } from "child_process";
import {
    
     readFile } from "fs";
import {
    
     resolve } from "path";

let statusBar: vscode.StatusBarItem | undefined;
let timeout: NodeJS.Timeout;

enum Status {
    
    
  error = "error",
}
function customStatusBar(text: string, type?: Status, time = 4000) {
    
    
  if (statusBar) {
    
    
    statusBar.dispose();
  }
  if (timeout) {
    
    
    clearTimeout(timeout);
  }
  statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
  statusBar.color = "#ffffff";

  if (type === Status.error) {
    
    
    statusBar.backgroundColor = new vscode.ThemeColor(
      "statusBarItem.errorBackground"
    );
  }
  if (!type) {
    
    
    statusBar.backgroundColor = new vscode.ThemeColor(
      "statusBarItem.warningBackground"
    );
  }
  statusBar.text = "vscode-nvmrc: " + text;
  statusBar.show();
  timeout = setTimeout(() => {
    
    
    if (statusBar) {
    
    
      statusBar.dispose();
    }
  }, time);
  return;
}

function execute(cmd: string) {
    
    
  exec(cmd, (error, stdout, stderr) => {
    
    
    if (error) {
    
    
      customStatusBar(`${
      
      error}`);
    } else {
    
    
      if (stderr) {
    
    
        customStatusBar(stderr);
      } else {
    
    
        customStatusBar(stdout);
      }
    }
  });
}

function nvmuse(url: string) {
    
    
  readFile(url, {
    
     encoding: "utf8" }, (err, data) => {
    
    
    if (err) {
    
    
      customStatusBar(".nvmrc file not found.");
      return;
    }
    execute("nvm use " + data);
  });
}

function resolveRootPathAndNvmuse() {
    
    
  const workspaceFolders = vscode.workspace.workspaceFolders;
  if (workspaceFolders && workspaceFolders.length > 0) {
    
    
    const rootPath = workspaceFolders[0].uri.fsPath;
    if (rootPath) {
    
    
      const url = resolve(rootPath, ".nvmrc");
      nvmuse(url);
    }
  }
}

export function activate(context: vscode.ExtensionContext) {
    
    
  resolveRootPathAndNvmuse();
  const disposable = vscode.window.onDidChangeWindowState((e) => {
    
    
    if (e.focused) {
    
    
      resolveRootPathAndNvmuse();
    }
  });
  context.subscriptions.push(disposable);
}

export function deactivate() {
    
    }

Guess you like

Origin blog.csdn.net/qq_42611074/article/details/131662365