Search code examples
node.jspowershellgulp

How to invoke Powershell script in a different folder using Gulp


I was able to implement Splitting gulpfile into multiple file without any issues. Now, my task (below) is moved from ./gulpfile.js to ./tasks/database-publish.js. In this task, I'm using var exec = require('child_process').exec to run a Powershell (PS1) I wrote that lives in ../DbDeploy.ps1 relative to the new gulp-task file.

In this task, I'm executing my ps1 file via gulp

gulp.task("zzz_Run-DB-Deply",
        function(callback) {
            console.log('running db deploy');
            plugins.exec('Powershell.exe  -executionpolicy remotesigned -File  ..\DbDeploy.ps1',
                function (err, stdout, stderr) {
                    console.log(stdout);
                    callback(err);
                });
        });

If I run Powershell.exe -executionpolicy remotesigned -File ..\DbDeploy.ps1 in Powershell from the task directory, it works fine. However, when I run the zzz_Run-DB-Deploy task, I keep getting an exception:

Process terminated with code 0

My suspicion is with ..\DbDeploy.ps1. I think \D is being used as escape character. But I'm not sure. What am I doing wrong?


Solution

  • Found the solution. The issue was with transfer of the plugins from the gulpfile.js to tasks/gulp-task.js

    In order to give everyone some context, it'd be best to paste the tasks here. I though that I could send in all of the referenced plugins to my tasks folder using a plugins variable. Unfortunately, I was wrong.

    module.exports = function (gulp, config, plugins) {
        'use strict';
        console.log('Starting the database migration.');
        var exec = require('child_process').exec;
    
        gulp.task("zzz_Run-DB-Deply",
            function(callback) {
                console.log('running db deploy');
                exec("Powershell.exe  -executionpolicy remotesigned -File  ..\DbDeploy.ps1",
                    function (err, stdout, stderr) {
                        console.log(stderr);
                        console.log(stdout);
                        console.log(err);
                        callback(err);
                    });
            });
        gulp.task("Build-DB-Project",
            function (callback) {
                console.log('running build DB project');
                var targets = ["Build"];
                if (config.runCleanBuilds) {
                    targets = ["Clean", "Build"];
                }
                return gulp.src("./Project/*.csproj")
                    .pipe(plugins.foreach(function (stream, file) {
                        return stream
                            .pipe(plugins.debug({ title: "Building" }))
                            .pipe(plugins.msbuild({
                                targets: targets,
                                configuration: config.buildConfiguration,
                                logCommand: false,
                                verbosity: "minimal",
                                stdout: true,
                                errorOnFail: true,
                                maxcpucount: config.maxCpuCount,
                                toolsVersion: config.toolsVersion
                            }));
                    }));
            });
    
    }
    

    In order to fix this issue, I added a new exe variable referencing var exec = require('child_process').exec; At which point, running exec("Powershell.exe pwd") showed me that my working directory was in fact the root of my project where gulpfile.js and DbDeploy.ps1 both live. Thus, I knew to change the file path from ../DbDeploy.ps1 to DbDeploy.ps.

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