UNPKG

@routineless/nx-aws-cdk

Version:
86 lines 3.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createCommands = void 0; const tslib_1 = require("tslib"); const path = tslib_1.__importStar(require("path")); const logger_1 = require("../../utils/logger"); const optionsShortNemingMapping = { a: 'app', c: 'context', p: 'plugin', j: 'json', v: 'verbose', i: 'ec2creds', r: 'role-arn', o: 'output', h: 'help', }; const createCommands = (options, context) => { logger_1.logger.debug('Cdk executor options %s', JSON.stringify(options)); const NX_WORKSPACE_ROOT = process.env['NX_WORKSPACE_ROOT']; if (!NX_WORKSPACE_ROOT) { throw new Error('CDK not Found'); } let baseExecutionCommand = options.env !== 'local' ? `AWS_ENV=${options.env} node ${NX_WORKSPACE_ROOT}/node_modules/aws-cdk/bin/cdk.js ${options.command}` : `AWS_ENV=${options.env} node ${NX_WORKSPACE_ROOT}/node_modules/aws-cdk-local/bin/cdklocal ${options.command}`; if (options.account) { baseExecutionCommand = `AWS_ACCOUNT=${options.account} ${baseExecutionCommand}`; } if (options.region) { baseExecutionCommand = `AWS_REGION=${options.region} ${baseExecutionCommand}`; } const commandOptions = getCommandOptions(options); const executionCommand = { cdkCommand: options.command, command: [baseExecutionCommand, ...commandOptions].join(' '), cwd: options.cwd || path.join(context.root, options.root), }; const resultCommands = []; if (options.watch) { if (executionCommand.cdkCommand === 'deploy') { executionCommand.command = `${executionCommand.command} --watch`; let providedStacks = options.parsedArgs['_']; providedStacks = Array.isArray(providedStacks) ? providedStacks : []; //Watch command will just hang without warnings if no stacks provided and --all flag is not provided //so setting --all flag by default in this case if (!providedStacks.length && !options.parsedArgs['all']) { executionCommand.command = `${executionCommand.command} --all`; } } resultCommands.push(createProjectWatchCommand(context, options, executionCommand)); } resultCommands.push(executionCommand); return resultCommands; }; exports.createCommands = createCommands; const createProjectWatchCommand = (context, options, executionCommand) => { return { command: `npx nx watch --projects=${context.projectName} -d -- "nx build ${context.projectName}${executionCommand.cdkCommand !== 'deploy' ? ` && (cd ${context.root}/${options.root} && ${executionCommand.command})` : ''}"`, }; }; const getCommandOptions = (options) => { const commandArgs = []; for (const argKey in options.parsedArgs) { const argValue = options.parsedArgs[argKey]; if (Array.isArray(argValue)) { argValue.forEach((value) => { commandArgs.push(parsedArgToString(argKey, value)); }); } else if (argValue) { commandArgs.push(parsedArgToString(argKey, argValue)); } } return commandArgs; }; const parsedArgToString = (key, value) => { if (key === '_') { return `${value}`; } const mappedKey = optionsShortNemingMapping[key] || key; return `--${mappedKey} ${value}`; }; //# sourceMappingURL=executors.js.map