-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport-vars.js
executable file
·68 lines (55 loc) · 1.6 KB
/
export-vars.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
var Promise = require("bluebird");
var yaml = require('js-yaml');
var fs = Promise.promisifyAll(require("fs"));
var crypto = require('crypto');
var algorithm = 'aes-256-ctr';
var homeDir = process.env.HOME;
var args = process.argv.slice(2);
var password = args[0];
if (!password) {
throw new Error('Private key not passed.');
}
function encrypt(text){
var cipher = crypto.createCipher(algorithm, password);
var crypted = cipher.update(text, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function decrypt(text){
var decipher = crypto.createDecipher(algorithm, password);
var dec = decipher.update(text, 'hex', 'utf8');
dec += decipher.final('utf8');
return dec;
}
fs.readFileAsync(homeDir + '/build/.shoov.yml')
.then(function(data) {
return yaml.safeLoad(data);
})
.then(function(data) {
var variables = [];
data.env = data.env || [];
data.env.forEach(function(row) {
var keyName = Object.keys(row)[0];
var variableValue;
if (keyName == 'secure') {
var decryptArr = decrypt(row[keyName]).split(':');
if (decryptArr.length != 2) {
throw new Error('Wrong secure key.');
}
keyName = decryptArr[0];
variableValue = decryptArr[1];
}
else {
variableValue = row[keyName];
}
// Export value as a bash variable.
variables.push('export ' + keyName + '=' + variableValue);
});
return variables.join('\n');
})
.then(function(data) {
return fs.writeFileAsync(homeDir + '/build/export.sh', data);
})
.catch(function(err) {
console.log(err);
});