1 | /*
|
2 | Licensed to the Apache Software Foundation (ASF) under one
|
3 | or more contributor license agreements. See the NOTICE file
|
4 | distributed with this work for additional information
|
5 | regarding copyright ownership. The ASF licenses this file
|
6 | to you under the Apache License, Version 2.0 (the
|
7 | "License"); you may not use this file except in compliance
|
8 | with the License. You may obtain a copy of the License at
|
9 |
|
10 | http://www.apache.org/licenses/LICENSE-2.0
|
11 |
|
12 | Unless required by applicable law or agreed to in writing,
|
13 | software distributed under the License is distributed on an
|
14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15 | KIND, either express or implied. See the License for the
|
16 | specific language governing permissions and limitations
|
17 | under the License.
|
18 | */
|
19 |
|
20 | var child_process = require('child_process'),
|
21 | Q = require('q');
|
22 |
|
23 | /**
|
24 | * Executes the command specified.
|
25 | * @param {string} cmd Command to execute
|
26 | * @param {[string]} opt_cwd Current working directory
|
27 | * @return {Q} promise a promise that either resolves with the stdout, or rejects with an error message and the stderr.
|
28 | */
|
29 | module.exports = function (cmd, opt_cwd) {
|
30 | var d = Q.defer();
|
31 | try {
|
32 | child_process.exec(cmd, {cwd: opt_cwd, maxBuffer: 1024000}, function (err, stdout, stderr) {
|
33 | if (err) {
|
34 | d.reject(new Error('Error executing "' + cmd + '": ' + stderr));
|
35 | }
|
36 | else {
|
37 | d.resolve(stdout);
|
38 | }
|
39 | });
|
40 | } catch (e) {
|
41 | console.error('error caught: ' + e);
|
42 | d.reject(e);
|
43 | }
|
44 | return d.promise;
|
45 | };
|
46 |
|