gloriaJun / til

Lessoned Learned
3 stars 0 forks source link

Typescript with array map and reduce with promise type #95

Open gloriaJun opened 3 years ago

gloriaJun commented 3 years ago
interface IGeneralObject<T> {
  [index: string]: T;
}

export interface IBranchPrInfo {
  isCreate: boolean;
  isMerge: boolean;
}

await targetBranchList.reduce(
    async (promise: Promise<IGeneralObject<IBranchPrInfo>>, branch: string) => {
      const result = await promise.then();

      const { isCreate } = (await inquirer.prompt([
        {
          type: 'confirm',
          name: 'isCreate',
          message: `Create PR to '${branch}' branch`,
          default: [branchInfo.master].includes(branch),
        },
      ])) as inquirer.Answers;

      const { isMerge } =
        isCreate &&
        ((await inquirer.prompt([
          {
            type: 'confirm',
            name: 'isCreate',
            message: `Merge PR to '${branch}' branch`,
            default: [branchInfo.master].includes(branch),
          },
        ])) as inquirer.Answers);

      result[branch] = {
        isCreate: isCreate as boolean,
        isMerge: isMerge as boolean,
      };

      return Promise.resolve(result);
    },
    Promise.resolve({}),
  );