-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
68 lines (56 loc) · 2.34 KB
/
setup.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
const fs = require('fs');
function execShellCommand(cmd) {
const exec = require('child_process').exec;
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.warn(error);
}
resolve(stdout? stdout : stderr);
});
});
}
async function main(){
const _setupFile = './setup.demo.json';
const _frontendEndpointFolder = './frontend/src/assets/js/';
const _spMetadataFolder = './docs/';
const _dataPermissionsFolder = './data/';
let setup = fs.readFileSync(_setupFile, 'utf8');
if(setup){
try{
setup = JSON.parse(setup)
}catch(e){
setup = null;
console.error("Error processing file ", _setupFile);
}
if(!setup){
return;
}
const serviceName = setup.serviceName;
const dataBucket = setup.dataBucket;
const stage = "demo";
const api_id = await execShellCommand("sls info | grep GET -m 1 | awk -F[/:] '{printf \$4}'");
if(!api_id){
console.log("Review your AWS Credentials");
return;
}
//endpoint.js
let sampleEndpoint = fs.readFileSync(_frontendEndpointFolder+"sample-endpoint.js", 'utf8');
sampleEndpoint = sampleEndpoint.replace("{api_id}",api_id).replace("{stage}",stage);
fs.writeFileSync(_frontendEndpointFolder+"endpoint.js",sampleEndpoint);
console.log(_frontendEndpointFolder+"endpoint.js successfully generated");
//permissions.csv
let sampleDataPermissions = fs.readFileSync(_dataPermissionsFolder+"sample-permissions.csv", 'utf8');
sampleDataPermissions = sampleDataPermissions.replace(/{serviceName}/g,serviceName).replace(/{dataBucket}/g,dataBucket);
fs.writeFileSync(_dataPermissionsFolder+"permissions.csv",sampleDataPermissions);
console.log(_dataPermissionsFolder+"permissions.csv successfully generated");
//sp-metadata.xml
let metadataFile = fs.readFileSync(_spMetadataFolder+"sample-sp-metadata.xml", 'utf8');
metadataFile = metadataFile.replace("{api_id}",api_id).replace("{stage}",stage).replace("{issuer}",serviceName);
fs.writeFileSync(_spMetadataFolder+"sp-metadata.xml",metadataFile);
if((await execShellCommand("curl -F userfile=@docs/sp-metadata.xml https://samltest.id/upload.php")).indexOf("successfully")>-1){
console.log(_spMetadataFolder+"sp-metadata.xml uploaded successfully to samltest.id");
}
}
}
main();