@routineless/nx-aws-cdk
Version:
Nx plugin for AWS CDK
138 lines • 5.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const client_sts_1 = require("@aws-sdk/client-sts");
const credential_providers_1 = require("@aws-sdk/credential-providers");
const devkit_1 = require("@nx/devkit");
const shared_ini_file_loader_1 = require("@smithy/shared-ini-file-loader");
const executors_1 = require("../../utils/executors");
const logger_1 = require("../../utils/logger");
const localstack_1 = require("../localstack");
const executors_2 = require("./executors");
const executorPropKeys = ['cwd', 'env', 'account', 'region', 'watch', 'resolve'];
const normalizeOptions = async (options, context) => {
if (!context.projectName) {
throw new Error(`Cdk executor should be executed on cdk project. Provided project name: ${context.projectName}`);
}
const projectConfig = context.projectsConfigurations?.projects[context.projectName];
if (!projectConfig) {
throw new Error(`Cdk executor failed. Failed to read project configuration for ${context.projectName}`);
}
const { sourceRoot, root } = projectConfig;
if (!sourceRoot) {
throw new Error(`Cdk executor failed. Failed to read source root for ${context.projectName}`);
}
const { command, parsedArgs } = parseArgs(options);
if (!command) {
throw new Error(`Cdk executor failed. Command is not provided`);
}
let finalCommand = command;
if (command == 'watch') {
finalCommand = 'deploy';
options.watch = true;
}
parsedArgs['profile'] = parsedArgs['profile'] || process.env['AWS_PROFILE'];
let resolvedAccount = options.account || process.env['AWS_ACCOUNT'];
let resolvedRegion = options.region || process.env['AWS_REGION'];
if (options.resolve) {
const profile = parsedArgs['profile'];
if (!resolvedRegion) {
let regionResolutionError;
try {
const awsConfig = await (0, shared_ini_file_loader_1.loadSharedConfigFiles)();
resolvedRegion =
profile && typeof profile === 'string'
? awsConfig.configFile?.[profile]?.['region'] || awsConfig.configFile?.['default']?.['region']
: awsConfig.configFile?.['default']?.['region'];
}
catch (e) {
regionResolutionError = e;
logger_1.logger.warn(`Cannot resolve region. Failed to load aws config for profile ${profile}: ${e}`);
}
finally {
if (!resolvedRegion && !regionResolutionError) {
console.warn(`Failed to resolve region for profile ${profile}. Region is not configured in aws config`);
}
}
}
if (!resolvedAccount) {
if (resolvedRegion) {
const init = {};
if (profile && typeof profile === 'string') {
init.profile = profile;
}
const awsCredentials = (0, credential_providers_1.fromNodeProviderChain)(init);
const stsClient = new client_sts_1.STSClient({ credentials: awsCredentials, region: resolvedRegion });
try {
const callerIdentity = await stsClient.send(new client_sts_1.GetCallerIdentityCommand({}));
resolvedAccount = callerIdentity.Account;
}
catch (e) {
logger_1.logger.warn(`Cannot resolve account. Failed to get caller identity for profile ${profile}: ${e}`);
}
}
else {
logger_1.logger.warn('Cannot resolve account. Region is not provided and cannot be resolved from aws config');
}
}
}
return {
...options,
command: finalCommand,
env: options.env || process.env['AWS_ENV'] || 'local',
account: resolvedAccount,
region: resolvedRegion,
sourceRoot,
root,
parsedArgs,
projectName: context.projectName,
};
};
const cdkExecutor = async (options, context) => {
const normalizedOptions = await normalizeOptions(options, context);
const commands = (0, executors_2.createCommands)(normalizedOptions, context);
if (normalizedOptions.env === 'local') {
const localstackStartResult = await (0, devkit_1.runExecutor)({ project: normalizedOptions.projectName, target: localstack_1.TARGET_NAME }, { command: 'start' }, context);
for await (const resolut of localstackStartResult) {
if (!resolut.success) {
logger_1.logger.error('Failed to start localstack');
return {
success: false,
};
}
}
}
try {
await (0, executors_1.runCommandsInParralel)(commands);
return {
success: true,
};
}
catch (e) {
logger_1.logger.error(`Failed to execute commands ${JSON.stringify(commands)}: ${e}`);
return {
success: false,
};
}
};
const parseArgs = (options) => {
let command;
const keys = Object.keys(options);
const parsedArgs = keys
.filter((p) => executorPropKeys.indexOf(p) === -1)
.reduce((acc, key) => {
const optionValue = options[key];
if ((optionValue && typeof optionValue !== 'object') || Array.isArray(optionValue)) {
if (key === '_' && Array.isArray(optionValue) && optionValue.length) {
command = optionValue[0];
acc[key] = optionValue.slice(1);
}
else {
acc[key] = optionValue;
}
}
return acc;
}, {});
return { command, parsedArgs };
};
exports.default = cdkExecutor;
//# sourceMappingURL=index.js.map