Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>JS Command RunnerNew to Visual Studio Code? Get it now.
JS Command Runner

JS Command Runner

FancyWorks

|
192 installs
| (0) | Free
a js command tool
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

vscode-plugin-command-runner README

一个可以自定义 js 命令的插件, 自定义命令可以使用 vscode 的基础功能

示例

示例配置如下

    1. 在工程根目录, 创建配置文件: command-runner.json
[
  {
    "label": "生成FormItem",
    "jsModule": "./commands/GenFormItem.js"
  }
]
    1. 然后创建对应的目录和文件

./commands/GenFormItem.js

    1. js 脚本的内容

示例脚本功能:

解析光标当前行的文本, 生成 FormItem

// GenFormItem.js
async ({ vscode }) => {
  const editor = vscode.window.activeTextEditor;
  if (editor) {
    // 获取光标所在位置
    const position = editor.selection.active;
    // 获取当前行
    const line = editor.document.lineAt(position.line);

    const startIndex = line.firstNonWhitespaceCharacterIndex;
    const endIndex = line.range.end.character;
    // 构造有效部分的范围
    const range = new vscode.Range(
      position.line,
      startIndex,
      position.line,
      endIndex
    );

    const lineContent = line.text;

    const [formItemText, controllerText] = lineContent.trim().split(";");

    const [label, name, span2 = ""] = formItemText.split(",");
    const [controllerName, modelName = name, enumName = ""] =
      controllerText.split(",");

    let result = `<NFormItem label="${label}" name="${name}" ${span2}>
          <${controllerName} v-model:value="model.${modelName}" ${
      enumName ? 'name="' + enumName + '"' : ""
    } />
        </NFormItem>`;

    /**
     * 标题 name span2;NInput
     * =>
     *  <NFormItem label="标题" name="name" span2>
          <NInput v-model:value="model.name" />
        </NFormItem>
     */

    // 替换当前行的文本
    editor.edit((builder) => {
      builder.replace(range, result);
    });

    // vscode.env.clipboard.writeText(line).then(() => {
    //   vscode.window.showInformationMessage(line);
    // });
  }
};
    1. 使用

a. 输入命令, > Command Runner: ShowCommands 或者 快捷键 Cmd+E(mac), Ctrl+E(win), 可以打开脚本菜单

脚本菜单

b. 选择脚本, 回车就可以执行

Features

可运行 js 脚本

Requirements

None

Extension Settings

None for now

Known Issues

None

Release Notes

Users appreciate release notes as you update your extension.

0.0.1

a demo version

Following extension guidelines

None

For more information

  • Visual Studio Code's Markdown Support
  • Markdown Syntax Reference

Enjoy!

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft