"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewmanRunner = void 0;
const aws_cdk_lib_1 = require("aws-cdk-lib");
const aws_logs_1 = require("aws-cdk-lib/aws-logs");
class NewmanRunner {
    constructor(scope, id, props) {
        var _a;
        const environmentVars = Object.entries(props.collectionVariables).map(pair => `--env-var ${pair[0]}="${pair[1]}"`);
        const projectProps = {
            ...props,
            environment: {
                buildImage: aws_cdk_lib_1.aws_codebuild.LinuxBuildImage.STANDARD_7_0,
            },
            buildSpec: aws_cdk_lib_1.aws_codebuild.BuildSpec.fromObject({
                version: '0.2',
                LogRetention: aws_logs_1.RetentionDays.ONE_WEEK,
                phases: {
                    install: {
                        commands: [
                            'n 22',
                            'npm install --global yarn',
                            // Updated Newman command to use version 6.14 (without .0) // 6.14 is not a valid version.
                            'npm install -g newman@6.2.1',
                            'echo "Ensure that the Newman spec is readable"',
                            `chmod 755 ${props.collectionPath}`,
                        ],
                    },
                    build: {
                        commands: [`newman run ${props.collectionPath} ${environmentVars.join(' ')}`],
                    },
                },
            }),
        };
        this.project = new aws_cdk_lib_1.aws_codebuild.PipelineProject(scope, id, projectProps);
        this.action = new aws_cdk_lib_1.aws_codepipeline_actions.CodeBuildAction({
            input: props.sourceArtifact,
            project: this.project,
            actionName: props.actionName || id,
            runOrder: (_a = props.runOrder) !== null && _a !== void 0 ? _a : 98,
        });
    }
}
exports.NewmanRunner = NewmanRunner;
exports.default = NewmanRunner;
