简体   繁体   English

如何通过node-pty设置$ PATH作为在节点中扩展进程的一部分?

[英]How can I set $PATH as part of spawing a process in node via node-pty?

We are trying to encapsulate a very old command-line application which has these requirements: 我们正在尝试封装一个非常老的命令行应用程序,它具有以下要求:

  1. the application must be launched from the location of our data files (ie the CWD is the data file directory) 必须从我们的数据文件的位置启动该应用程序(即CWD是数据文件目录)
  2. the application exists at a location not in the default $PATH 该应用程序在一个位置存在在默认的$ PATH
  3. the application launches sub-processes that expect the application path to be in $PATH 应用程序启动子进程,该子进程期望应用程序路径位于$ PATH中
  4. Due to 2 & 3, the application's path must be manually added to $PATH prior to launching it 由于2&3,应用程序的路径必须在启动之前手动添加到$ PATH中。

So the normal way to run it is to cd to the data directory, add the application directory to the $PATH, and then run the application. 因此,运行它的通常方法是cd到数据目录,将应用程序目录添加到$ PATH,然后运行该应用程序。 We can't just launch the application by its full path -- the $PATH must be set because the application expects to call other sub-processes in the same directory. 我们不能只通过其完整路径启动应用程序-必须设置$ PATH,因为应用程序希望在同一目录中调用其他子进程。 We don't have the ability to modify the application or its requirements. 我们没有能力修改应用程序或其要求。

The problem is this: how do I encapsulate the setting of $PATH and the launching of the application when spawning a process in node via node-pty. 问题是这样的:当通过node-pty在node中生成进程时,如何封装$ PATH的设置和应用程序的启动。

var pty = require('node-pty');
// myapp lives in /usr/bin/app/
var term = pty.spawn("myapp", [], {
        cwd: '/usr/data/mydata'
      });

In short, I want the spawn to happen in the /usr/data/mydata directory, have /usr/bin/app/ added to $PATH, and myapp to get spawned, all in one shot. 简而言之,我希望生成发生在/ usr / data / mydata目录中,将/ usr / bin / app /添加到$ PATH中,并生成myapp,所有操作一次完成。

PATH is an environmental variable, so setting it with the env parameter should work. PATH是一个环境变量,因此使用env参数进行设置应该可以。

var pty = require('node-pty');
// myapp lives in /usr/bin/app/
var term = pty.spawn("myapp", [], {
  cwd: '/usr/data/mydata',
  env: { PATH: '/usr/data/app:' + process.env.PATH }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM