Skip to content

Commit 719d68f

Browse files
committed
refactor
1 parent 685b783 commit 719d68f

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ gulp.task("default", function() {
88
gutil.log(chalk.yellow("Make sure binaries have execute permission before packaging."));
99
gutil.log(chalk.yellow("This should be run from a Linux shell so that permissions are properly copied into the package."));
1010

11-
return gulp.src(["index.js", "package.json", "package-lock.json", "bin/*"], {base: "."})
11+
return gulp.src(["*.js", "package.json", "package-lock.json", "bin/*"], {base: "."})
1212
.pipe(zip("package.zip"))
1313
.pipe(gulp.dest("dist"))
1414
})

index.js

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,8 @@
11

2-
const childProc = require("child_process");
3-
const Readable = require("stream").Readable;
4-
5-
process.env.PATH += ":" + require("path").join(__dirname, "bin");
2+
const shellExec = require("./shell-exec.js");
63

74
exports.handler = (event, context, callback) => {
8-
const child = childProc.spawn(event.command, event.args, {shell: true});
9-
let hasError = false;
10-
child.on("error", err => {
11-
hasError = true;
12-
callback(err);
13-
})
14-
const getExitCode = new Promise(function(fulfill) {
15-
child.on("exit", (code, signal) => fulfill(signal || code));
16-
})
17-
Promise.all([getExitCode, getStream(child.stdout), getStream(child.stderr)])
18-
.then(function([exitCode, stdout, stderr]) {
19-
if (!hasError)
20-
callback(null, {
21-
exitCode,
22-
stdout: stdout.toString(event.stdoutEncoding || "utf8"),
23-
stderr: stderr.toString(event.stderrEncoding || "utf8")
24-
})
25-
})
26-
if (event.stdin) {
27-
const stdin = new Readable();
28-
stdin.push(Buffer.from(event.stdin, event.stdinEncoding || "utf8"));
29-
stdin.push(null);
30-
stdin.pipe(child.stdin);
31-
}
5+
shellExec(event.command, event.args, event.stdin, event.stdinEncoding, event.stdoutEncoding)
6+
.then(result => callback(null, result))
7+
.catch(err => callback(err))
328
};
33-
34-
function getStream(stream) {
35-
return new Promise(function(fulfill) {
36-
const chunks = [];
37-
stream.on("data", chunk => chunks.push(chunk));
38-
stream.on("end", () => fulfill(Buffer.concat(chunks)));
39-
})
40-
}

shell-exec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
process.env.PATH = require("path").join(__dirname, "bin") + ":" + process.env.PATH;
3+
module.exports = shellExec;
4+
5+
function shellExec(command, args, stdin, stdinEncoding, stdoutEncoding) {
6+
const child = require("child_process").spawn(command, args, {shell: true});
7+
if (stdin) {
8+
const Readable = require("stream").Readable;
9+
const str = new Readable();
10+
str.push(Buffer.from(stdin, stdinEncoding || "utf8"));
11+
str.push(null);
12+
str.pipe(child.stdin);
13+
}
14+
return Promise.all([
15+
getExitCode(child),
16+
getStream(child.stdout, stdoutEncoding || "utf8"),
17+
getStream(child.stderr, "utf8")
18+
])
19+
.then(([exitCode, stdout, stderr]) => ({exitCode, stdout, stderr}))
20+
};
21+
22+
function getExitCode(child) {
23+
return new Promise(function(fulfill, reject) {
24+
child.on("error", reject);
25+
child.on("exit", (code, signal) => fulfill(signal || code));
26+
})
27+
}
28+
29+
function getStream(stream, encoding) {
30+
return new Promise(function(fulfill) {
31+
const chunks = [];
32+
stream.on("data", chunk => chunks.push(chunk));
33+
stream.on("end", () => fulfill(Buffer.concat(chunks)));
34+
})
35+
.then(buf => encoding ? buf.toString(encoding) : buf)
36+
}

0 commit comments

Comments
 (0)