-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
exec.js
72 lines (58 loc) · 1.71 KB
/
exec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const Promise = require('bluebird')
const execa = require('execa')
const shellEnv = require('shell-env')
const _ = require('lodash')
const log = require('./log')
const utils = require('./util/shell')
const pickMainProps = (val) => _.pick(val, ['stdout', 'stderr', 'code'])
const trimStdio = (val) => {
const result = { ...val }
if (_.isString(val.stdout)) {
result.stdout = val.stdout.trim()
}
if (_.isString(val.stderr)) {
result.stderr = val.stderr.trim()
}
return result
}
module.exports = {
run (projectRoot, options) {
let {
cmd,
} = options
const shellCommand = function (cmd, cwd, env, shell) {
log('cy.exec found shell', shell)
log('and is running command:', options.cmd)
log('in folder:', projectRoot)
return execa.shell(cmd, { cwd, env, shell })
.then((result) => {
// do we want to return all fields returned by execa?
result.shell = shell
result.cmd = cmd
return result
}).then(pickMainProps)
.catch(pickMainProps) // transform rejection into an object
.then(trimStdio)
}
const run = () => {
return shellEnv()
.then((shellVariables) => {
const env = _.merge({}, shellVariables, process.env, options.env)
return utils.getShell(env.SHELL)
.then((shell) => {
cmd = utils.sourceShellCommand(options.cmd, shell)
return shellCommand(cmd, projectRoot, env, shell)
})
})
}
return Promise
.try(run)
.timeout(options.timeout)
.catch(Promise.TimeoutError, () => {
const msg = `Process timed out\ncommand: ${options.cmd}`
const err = new Error(msg)
err.timedOut = true
throw err
})
},
}