-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
118 lines (93 loc) · 2.97 KB
/
index.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import nodeify from 'nodeify-ts';
import * as child_process from 'child_process';
const execFile = child_process.execFile;
const extractResult = (result: Result): Result => {
try {
result.object = JSON.parse(result.raw);
} catch (e) {
result.object = e;
}
return result;
};
export class Aws {
constructor(private options: IOptions = {
accessKey: undefined,
currentWorkingDirectory: undefined,
secretKey: undefined,
cliPath: 'aws'
}) { }
public command(command: string, callback?: (err: any, data: any) => void): Promise<any> {
const promise = Promise.resolve().then( () => {
const env_vars = ('HOME PATH AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY ' +
'AWS_SESSION_TOKEN AWS_DEFAULT_REGION ' +
'AWS_DEFAULT_PROFILE AWS_CONFIG_FILE').split(' ');
const env = env_vars.reduce((result: any, value: string) => {
if (process.env[value]) {
result[value] = process.env[value];
}
return result;
},{});
env['DEBUG'] = '';
if (this.options.accessKey) {
env['AWS_ACCESS_KEY_ID'] = this.options.accessKey;
}
if (this.options.secretKey) {
env['AWS_SECRET_ACCESS_KEY'] = this.options.secretKey;
}
if (this.options.sessionToken) {
env['AWS_SESSION_TOKEN'] = this.options.sessionToken;
}
const execOptions = {
cwd: this.options.currentWorkingDirectory,
env: env,
maxBuffer: 200 * 1024 * 1024,
};
//console.log('exec options =', execOptions);
//console.log('options.cliPath =', this.options.cliPath);
let cmd = [...command.split(' ')];
cmd = cmd.filter(v => v.length > 0);
//console.log('cmd2 = ', cmd);
return new Promise<{ stderr: string, stdout: string }>( (resolve, reject) => {
execFile(this.options.cliPath, cmd, execOptions, (error: Error | null, stdout: string, stderr: string) => {
if (error) {
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
//console.error(message);
reject(message);
}
//console.log(`stdout: ${stdout}`);
resolve({ stderr: stderr, stdout: stdout });
});
});
}).then((data: { stderr: string, stdout: string }) => {
const result: Result = {
command,
error: data.stderr,
object: null,
raw: data.stdout,
};
return extractResult(result);
});
return nodeify(promise, callback);
}
}
export interface IOptions {
accessKey?: string;
secretKey?: string;
sessionToken?: string;
currentWorkingDirectory?: string;
cliPath: string;
}
interface Result {
command: string;
error: string;
raw: string;
object: any;
}
export class Options implements IOptions {
public constructor(
public accessKey?: string,
public secretKey?: string,
public sessionToken?: string,
public currentWorkingDirectory?: string,
public cliPath = 'aws') { }
}