我想创建一个nodejs文件,简单地运行/包装一个包含所有输入和输出的可执行二进制文件。
现在至少在Windows上。
为什么:
想要在npm install -g
上安装一个可执行工具,将其置于控制台中,而不进行PATH
更改。 (npm全局包包含在PATH中)
答案 0 :(得分:0)
我使用了这样的解决方案:
3
但是对于“.exe to PATH”问题,有一种更简单的方法(如果你只想要Windows)
只需将SaveWorkRequest(workRequest: any) {
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
let url = 'deployment/SaveWorkRequest';
let dto = { 'workResponse': workRequest };
// post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
return this.http.post(GlobalVariables.SITE_ROOT + url, dto, options)
//.toPromise()
//.then(this.extractData) //...and calling .json() on the response to return data
.map(this.extractData)
.catch(this.handleError);
}
//
private extractData(res: Response) {
let body = res.json();
console.log(body);
return body || {};
}
中的 let response = this.workRequestService.SaveWorkRequest(this.workRequest)
.subscribe(
hero => this.message = hero,
error => this.errorMessage = <any>error);
console.log(this.message);
属性设置为const path = require("path");
const spawnSync = require('child_process').spawnSync;
const pathToMyExe = path.join(__dirname, 'bin', 'myfile.exe'); //just path to exe
const input = process.argv.slice(2); //minus "node" and "this js" arguments
spawnSync(pathToMyExe, input, {stdio: 'inherit'});
https://docs.npmjs.com/files/package.json#bin
答案 1 :(得分:0)
您可以使用bin-wrapper
npm包。
<强> 编辑: 强>
有一个名为bin-manager
的更好的选择。
$ npm install --save bin-manager
const bmanager = require('bin-manager');
const base = 'https://github.com/imagemin/gifsicle-bin/raw/master/vendor';
const bin = bmanager('bin', 'gifsicle')
.src(base + '/macos/gifsicle', 'darwin')
.src(base + '/linux/x64/gifsicle', 'linux', 'x64')
.src(base + '/win/x64/gifsicle.exe', 'win32', 'x64')
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
bin.run(['--version'], (err, out) => {
if (err) {
console.log(error);
return;
}
console.log(out.stdout);
});