-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
54 lines (53 loc) · 1.63 KB
/
index.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
'use strict';
/**
* This is now only responsible for checking the options
* and call the correct provider
*/
const provider = require('./lib/providers');
const defaultOptions = require('./lib/option');
const debug = require('debug')('git-webhook-ci:main');
const { spawn } = require('child_process');
// Create a callback to execute
const createCallback = function(cmd) {
return function(payload, opt) {
const process = spawn(cmd[0], cmd.filter((c, i) => i > 0), opt);
process.stdout.on('data', data => {
debug(`cmd stdout: ${data}`);
});
process.stderr.on('data', data => {
debug(`cmd stderr: ${data}`);
});
process.on('close', code => {
debug(`cmd exited with ${code}`);
});
};
};
/**
* @param {object} config configuration for the function return
* @return {function} for calls
*/
const serve = options => {
if (typeof options !== 'object') {
throw new Error('Expecting options to be an object');
}
const config = Object.assign({}, defaultOptions, options);
if (!config.secret || config.secret === '') {
throw new Error('You must provide the secret!');
}
if (typeof config.cmd !== 'string' && typeof config.cmd !== 'function') {
throw new Error('Cmd must be a string or a function!');
}
debug(config);
const createHandler = provider(config.provider);
// Return without Promise, because there is no need to
return createHandler(
config,
{
env: Object.assign({}, process.env),
cwd: config.dir ? config.dir : process.cwd()
},
typeof config.cmd === 'function' ? config.cmd : createCallback(config.cmd.split(' '))
);
};
// Export
module.exports = serve;