override synth()

in packages/blueprints/launch-blueprint/src/blueprint.ts [120:181]


  override synth(): void {
    const pathToRepository = path.join(this.context.durableStoragePath, this.state.repository.title);

    if (!fs.existsSync(pathToRepository)) {
      let cloneOptions = ['clone', '--depth', '1'].concat(this.state.options.sourceBranch ? ['--branch', this.state.options.sourceBranch] : [], [
        this.state.options.sourceRepository,
        this.state.repository.title,
      ]);

      cp.spawnSync('git', cloneOptions, {
        cwd: this.context.durableStoragePath,
        stdio: [0, 1, 1],
        timeout: GIT_CLONE_TIMEOUT,
      });

      // remove .git - we have no use for it and the large number of objects
      // contained within it could slow down the copying of sources to our workspace
      fs.rmSync(path.join(pathToRepository, '.git'), { recursive: true, force: true });
    }

    fs.cpSync(pathToRepository, this.state.repository.path, { recursive: true });

    //register options to blueprint
    const embeddedOptionsPath = path.join(this.state.repository.path, '.codecatalyst', 'launch-options.yaml');
    if (fs.existsSync(embeddedOptionsPath)) {
      const embeddedOptions = yaml.parse(fs.readFileSync(embeddedOptionsPath).toString()) as { options: KVSchema };
      new OptionsSchema(this, 'launch-options', embeddedOptions.options);
    }

    //map options and environments to workflows
    const workflowPath = path.join(this.state.repository.path, '.codecatalyst', 'workflows');
    if (fs.existsSync(workflowPath)) {
      const workflowFiles = fs.readdirSync(workflowPath);

      //load each workflow from the cloned repository
      for (const workflowFile of workflowFiles.filter(name => name.match(/^(.*)(\.yaml|\.yml)$/i))) {
        const workflowFilePath = path.join(workflowPath, workflowFile);

        const substitutions: { [key: string]: any } = {};
        this.state.options.launchOptions?.forEach(option => {
          substitutions[option.key] = option.value;
        });
        const workflowYaml = Mustache.render(fs.readFileSync(workflowFilePath).toString('utf-8'), substitutions);

        const workflow = yaml.parse(workflowYaml) as WorkflowDefinition;
        for (const actionName of Object.keys(workflow.Actions ?? [])) {
          const action = workflow.Actions?.[actionName];
          this.mapParametersToAction(action);
          for (const childActionName of Object.keys(action.Actions ?? [])) {
            const childAction = action.Actions?.[childActionName];
            this.mapParametersToAction(childAction);
          }
        }

        //overwrite existing workflow
        fs.writeFileSync(workflowFilePath, yaml.stringify(workflow));
      }
    }

    this.addExcludeFromCleanup(path.join(this.outdir, 'src', this.state.repository.title, '**'));
    super.synth();
  }