I want to run a couple of SQL scripts from Gulp:
const gulp = require('gulp');
const dotenv = require('dotenv')
const child_process = require('child_process');
function runSQLScript(script) {
child_process.execFile('sqlcmd', [
'-S', process.env.DB_SERVER,
'-U', process.env.DB_USER,
'-P', process.env.DB_PASSWORD,
'-d', process.env.DB_DATABASE,
'-f', 'i:65001',
'-i', script
], {
cwd: '../sql/sqlcmd'
}, function(error, stdout, stderr){
if (error !== null) {
console.log("" + stdout);
console.error("" + error);
}
if (stderr.length>0) {
console.error('' + stderr);
}
});
}
gulp.task('sql', ['install-app', 'load-data']);
gulp.task('install-app', function(){
runSQLScript('install-app.sql');
});
gulp.task('load-data', function(){
runSQLScript('load-data.sql');
});
Then I run:
gulp sql
In an earlier version I was using child_process.execFileSync()
and my scripts were running in sequence as expected. I switched to child_process.execFile()
to be able to report errors but now load-data.sql
fails because it no longer waits for install-app.sql
to finish.
What can I do to make the task wait until runSQLScript()
finishes? Did I choose a wrong approach to print errors?