I would like to run an executable and its path contains an enviroment variable, for example if I would like to run chrome.exe I would like to write something like this
我想运行一个可执行文件,它的路径包含一个环境变量,例如,如果我想运行chrome.exe我想写这样的东西
var spawn = require('child_process').spawn;
spawn('chrome',[], {cwd: '%LOCALAPPDATA%\\Google\\Chrome\\Application', env: process.env})
instead of
var spawn = require('child_process').spawn;
spawn('chrome',[], {cwd: 'C:\\Users\myuser\\AppData\\Local\\Google\\Chrome\\Application', env: process.env}).
Is there a package I can use in order to achieve this?
有没有我可以使用的包来实现这个目标?
2 个解决方案
#1
9
You can use a regex to replace your variable with the relevant property of process.env
:
您可以使用正则表达式将您的变量替换为process.env的相关属性:
var str = '%LOCALAPPDATA%\\Google\\Chrome\\Application';
var replaced = str.replace(/%([^%]+)%/g, function(_,n) {
return process.env[n];
})
I don't think a package is needed when it's 2 lines to write.
当写入2行时,我认为不需要包。
#2
0
On Linux/MacOS, I spawn a process to resolve paths with env variables, is safe - let bash to do the work for you. Obviously less performant, but a lot more robust. Looks like this:
在Linux / MacOS上,我产生了一个用env变量解析路径的进程,是安全的 - 让bash为你做的工作。显然性能较差,但更强大。看起来像这样:
import * as cp from 'child_process';
// mapPaths takes an array of paths/strings with env vars, and expands each one
export const mapPaths = (searchRoots: Array<string>, cb: Function) => {
const mappedRoots = searchRoots.map(function (v) {
return `echo "${v}"`;
});
const k = cp.spawn('bash');
k.stdin.end(mappedRoots.join(';'));
const results: Array<string> = [];
k.stderr.pipe(process.stderr);
k.stdout.on('data', (d: string) => {
results.push(d);
});
k.once('error', (e) => {
log.error(e.stack || e);
cb(e);
});
k.once('exit', code => {
const pths = results.map((d) => {
return String(d || '').trim();
})
.filter(Boolean);
cb(code, pths);
});
};
#1
9
You can use a regex to replace your variable with the relevant property of process.env
:
您可以使用正则表达式将您的变量替换为process.env的相关属性:
var str = '%LOCALAPPDATA%\\Google\\Chrome\\Application';
var replaced = str.replace(/%([^%]+)%/g, function(_,n) {
return process.env[n];
})
I don't think a package is needed when it's 2 lines to write.
当写入2行时,我认为不需要包。
#2
0
On Linux/MacOS, I spawn a process to resolve paths with env variables, is safe - let bash to do the work for you. Obviously less performant, but a lot more robust. Looks like this:
在Linux / MacOS上,我产生了一个用env变量解析路径的进程,是安全的 - 让bash为你做的工作。显然性能较差,但更强大。看起来像这样:
import * as cp from 'child_process';
// mapPaths takes an array of paths/strings with env vars, and expands each one
export const mapPaths = (searchRoots: Array<string>, cb: Function) => {
const mappedRoots = searchRoots.map(function (v) {
return `echo "${v}"`;
});
const k = cp.spawn('bash');
k.stdin.end(mappedRoots.join(';'));
const results: Array<string> = [];
k.stderr.pipe(process.stderr);
k.stdout.on('data', (d: string) => {
results.push(d);
});
k.once('error', (e) => {
log.error(e.stack || e);
cb(e);
});
k.once('exit', code => {
const pths = results.map((d) => {
return String(d || '').trim();
})
.filter(Boolean);
cb(code, pths);
});
};