1import chalk from 'chalk';
2import fs from 'fs-extra';
3import path from 'path';
4
5import { readPodspecAsync } from '../../../CocoaPods';
6import { toRepoPath } from '../utils';
7import { Task } from './Task';
8
9type GenerateJsonFromPodspecSettings = {
10  from: string;
11  saveTo: string;
12  transform: (map: Record<string, any>) => Promise<Record<string, any>>;
13};
14
15export class GenerateJsonFromPodspec extends Task {
16  protected readonly from: string;
17  protected readonly saveTo: string;
18  protected readonly transform: (map: Record<string, any>) => Promise<Record<string, any>>;
19
20  constructor({ from, saveTo, transform }: GenerateJsonFromPodspecSettings) {
21    super();
22    this.from = from;
23    this.saveTo = toRepoPath(saveTo);
24    this.transform = transform;
25  }
26
27  async execute() {
28    const workDirectory = this.getWorkingDirectory();
29
30    this.logSubStep(
31      `➕ generating podspec from ${chalk.green('<workingDirectory>')}/${chalk.green(this.from)}`
32    );
33    const podspec = await readPodspecAsync(path.join(workDirectory, this.from));
34    const transformedPodspec = await this.transform(podspec);
35    return await fs.writeFile(this.saveTo, JSON.stringify(transformedPodspec, null, 2));
36  }
37}
38