3

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?

5
  • 2
    Javascript requires that you escape the backslash in strings. Commented Jan 24, 2018 at 20:03
  • Like this? Powershell.exe -executionpolicy remotesigned -File ..\\DbDeploy.ps1 Commented Jan 24, 2018 at 20:11
  • 1
    Yes, exactly! Does it work? Commented Jan 24, 2018 at 20:40
  • It did, but I realized that wasn't my issue. Posted the answer below. Commented Jan 24, 2018 at 20:50
  • 1
    Using an unescaped backslash in a string literal is still wrong. You should fix that as well. Commented Jan 24, 2018 at 20:52

1 Answer 1

2

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);
                });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.