gpt4 book ai didi

node.js - 如何从 gulp 调用执行 PowerShell 脚本?

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:00 25 4
gpt4 key购买 nike

我正在使用 gulp 来构建和部署我们的应用程序。

var msbuild = require('gulp-msbuild');
gulp.task('build', ['clean'], function () {
return gulp.src('../../*.sln')
.pipe(msbuild({
toolsVersion: 14.0,
targets: ['Rebuild'],
errorOnFail: true,
properties: {
DeployOnBuild: true,
DeployTarget: 'Package',
PublishProfile: 'Development'
},
maxBuffer: 2048 * 1024,
stderr: true,
stdout: true,
fileLoggerParameters: 'LogFile=Build.log;Append;Verbosity=detailed',
}));
});

但是在构建之后我必须调用一个 PowerShell 脚本文件“publish.ps1”,我如何在 gulp 中调用它?

最佳答案

我还没有测试过这个,但如果你把两者结合起来,它看起来会像这样。只需运行默认任务,它使用运行序列来管理依赖顺序。

    var gulp = require('gulp'),
runSequence = require('run-sequence'),
msbuild = require('gulp-msbuild'),
spawn = require("child_process").spawn,
child;

gulp.task('default', function(){
runSequence('clean', 'build', 'powershell');
});

gulp.task('build', ['clean'], function () {
return gulp.src('../../*.sln')
.pipe(msbuild({
toolsVersion: 14.0,
targets: ['Rebuild'],
errorOnFail: true,
properties: {
DeployOnBuild: true,
DeployTarget: 'Package',
PublishProfile: 'Development'
},
maxBuffer: 2048 * 1024,
stderr: true,
stdout: true,
fileLoggerParameters: 'LogFile=Build.log;Append;Verbosity=detailed',
}));
});

gulp.task('powershell', function(callback){
child = spawn("powershell.exe",["c:\\temp\\helloworld.ps1"]);
child.stdout.on("data",function(data){
console.log("Powershell Data: " + data);
});
child.stderr.on("data",function(data){
console.log("Powershell Errors: " + data);
});
child.on("exit",function(){
console.log("Powershell Script finished");
});
child.stdin.end(); //end input
callback();
});

编辑

调用带参数的powershell文件

var exec = require("child_process").exec;

gulp.task("powershell", function(callback) {
exec(
"Powershell.exe -executionpolicy remotesigned -File file.ps1",
function(err, stdout, stderr) {
console.log(stdout);
callback(err);
}
);
});

解决方案根目录中的 Powershell file.ps1

Write-Host 'hello'

编辑 2

好的,再试一次。你能把参数/参数放在 file.ps1 中吗?

function Write-Stuff($arg1, $arg2){
Write-Output $arg1;
Write-Output $arg2;
}
Write-Stuff -arg1 "hello" -arg2 "See Ya"

编辑 3

从 gulp 任务传递参数::

gulp.task('powershell', function (callback) {
exec("Powershell.exe -executionpolicy remotesigned . .\\file.ps1; Write-Stuff -arg1 'My first param' -arg2 'second one here'" , function(err, stdout, stderr){
console.log(stdout);
callback(err)
});
});

更新 file.ps1 以删除

function Write-Stuff([string]$arg1, [string]$arg2){
Write-Output $arg1;
Write-Output $arg2;
}

关于node.js - 如何从 gulp 调用执行 PowerShell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com