{"size": 1338, "ext": "js", "max_stars_repo_path": "src/npm-scripts/configure-role-arn.js", "max_stars_repo_name": "garethpelly/alexa-skill-serverless-starter-template", "max_stars_repo_stars_event_min_datetime": "2017-01-13T15:30:37.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-15T19:07:24.000Z", "max_issues_repo_path": "src/npm-scripts/configure-role-arn.js", "max_issues_repo_name": "garethpelly/alexa-skill-serverless-starter-template", "max_issues_repo_issues_event_min_datetime": "2017-01-28T05:39:24.000Z", "max_issues_repo_issues_event_max_datetime": "2017-12-27T02:22:47.000Z", "max_forks_repo_path": "src/npm-scripts/configure-role-arn.js", "max_forks_repo_name": "garethpelly/alexa-skill-serverless-starter-template", "max_forks_repo_forks_event_min_datetime": "2017-04-17T12:23:10.000Z", "max_forks_repo_forks_event_max_datetime": "2019-08-05T05:36:17.000Z"}
{"max_stars_count": 86, "max_issues_count": 7, "max_forks_count": 12, "avg_line_length": 26.76, "max_line_length": 100, "alphanum_fraction": 0.5986547085}
'use strict';
const AWS = require('aws-sdk');
const sync = require('synchronize');
const npmRunScript = require('npm-run-script');
const argv = require('attrs.argv');

const stage = argv.stage;
const configFilename = stage + '.skill.config.json';
const configPath = '../config/' + configFilename;
const config = require(configPath);
const helper = require('./helper');

const skillNamespace = config.skillNamespace;
const region = config.region;
const roleName = helper.getRoleName(skillNamespace, stage, region);

if (config.roleArn != 'YOUR_ROLE_ARN') {
    console.log('The configure role ARN command can only be run once and it has already been run.');
    return;
}

sync.fiber(() => {
    const iam = new AWS.IAM({ apiVersion: '2010-05-08' });
    let role = null;

    let getRoleParams = {
        RoleName: roleName
    };

    try {
        console.log('Getting role ' + getRoleParams.RoleName + '...');
        role = sync.await(iam.getRole(getRoleParams, sync.defer()));  
        console.log(role);
    } catch (err) {
        console.error(err, err.stack)
        return;
    }

    if (role) {
        helper.modifyFiles(['./config/' + configFilename],
            [
                {
                    regexp: /YOUR_ROLE_ARN/g,
                    replacement: role.Role.Arn
                }
            ]);
    }

});
