首页 > 解决方案 > 在自定义 atom 包中运行终端命令

问题描述

我目前正在创建一个 atom 包,它在 windows 命令提示符而不是atom 命令提示符上运行命令。到目前为止,我只有代码:

if (editor = atom.workspace.getActiveTextEditor()){
  let editor = atom.workspace.getActiveTextEditor();
  let file = editor.buffer.file;
  let path = file.path;

  editor.save();

  editor.insertText(path);
}

我不知道如何生成命令窗口或如何运行命令。该代码所做的只是检查用户是否在文本窗口中,然后出于测试目的将路径插入文本窗口。最终,我将需要运行cd path.

标签: javascriptshellatom-editor

解决方案


要执行程序,您可以使用child_process与 Node 捆绑在一起的 with 模块(Atom 有自己的包装器,请参阅BufferedProcess

例子:

// Somewhere in your header
const { spawn } = require('child_process');

// Where you need to execute the Java compiler
const javac = spawn('javac', [path], {stdio: inherit});

出于调试目的,您可能希望使用控制台面板之类的东西来打印消息。


推荐阅读