-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
99 lines (94 loc) · 2.28 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
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
const ora = require('ora');
const spinner = ora('Loading Params').start();
const AWS = require("aws-sdk");
AWS.config.update({region: 'us-east-1'});
const ssm = new AWS.SSM();
const fs = require('fs');
const args = require('yargs/yargs')(process.argv.slice(2))
.option('n', {
alias: 'name',
describe: 'Names of configurations to deploy',
type: 'array'
})
.option('s', {
alias: 'stage',
describe: 'stages of selected configurations to deploy',
type: 'array'
})
.option('p', {
alias: 'path',
describe: 'path to json params',
type: 'string'
}).demandOption(
['path'],
'Please provide path argument to work with this tool'
).argv;
const prepDeploy = (names, stages, path) => {
let obj;
try {
if (fs.existsSync(path)) {
//file exists
obj = JSON.parse(fs.readFileSync(path));
}
} catch(err) {
console.error(err);
return err;
}
let results = [];
let params = filterObject(names, stages, obj);
params.forEach(x => {
results.push(ssm.putParameter(x));
});
return results;
}
const filterObject = (names, stages, obj) => {
let tempArray = [];
let returnArray = []
// filter the names
if(!names) {
tempArray = obj
} else {
names.forEach(x => {
const params = obj.filter(param => param.name === x);
if(!params.length) {
console.warn(`No names found by: ${x}`);
return;
};
tempArray.push(params[0])
});
}
if(!stages){
// build array of params
tempArray.forEach(y => {
for (const key in y) {
if(Array.isArray(y[key])) {
returnArray = returnArray.concat(y[key]);
}
}
});
return returnArray;
} else {
//filter the stages
stages.forEach(x => {
tempArray.forEach(y => {
if(y[x] === undefined || !y[x].length) {
console.warn(`No stages found by: ${x}`)
return;
}
returnArray = returnArray.concat(y[x]);
})
});
return returnArray;
}
}
async function doAllSequentually(arrFnPromiseList) {
if(!arrFnPromiseList || !Array.isArray(arrFnPromiseList) || !arrFnPromiseList.length) {
spinner.fail();
console.error('Promise list doesnt exist or isnt an array. please check your json config file and ensure it is valid')
return;
}
arrFnPromiseList.forEach(async (x)=> {await x.promise();});
}
doAllSequentually(prepDeploy(args.name, args.stage, args.path))
.catch(console.error)
.then(() => spinner.succeed());