gulp.task('exec', () => { const exec = { exec(command, options) { return new Promise((resolve, reject) => { cp.spawn(command, options, {stdio: 'inherit'}).on('error', reject).on('exit', resolve); }); }, local(commands) { return this.exec(process.env.SHELL, ['-c', commands.join('&&')]); }, remote(commands) { return this.exec('ssh', [process.env.REMOTE_HOST, commands.join('&&')]); }, }; exec.local([ 'pwd', 'ls', ]).then(() => { return exec.remote([ 'pwd', 'ls', ]); }).catch(err => { console.error(err.stack); }); });