All files / commands/template-creators multi-template.ts

100% Statements 19/19
100% Branches 6/6
100% Functions 11/11
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                    2x     4x       4x   4x   4x 2x   2x 1x 1x 1x   1x   1x           2x 16x                         1x 1x               1x 1x            
// Externals
import { ExtensionContext, Uri, window } from "vscode";
 
// Internals
import BaseCommand from "../base-command";
import { getSingleFolder } from "../helpers";
import allTemplates from "./index";
import SingleTemplate from "./single-template";
 
class MultiTemplate extends BaseCommand {
  static readonly PLACEHOLDER = "Pick templates to create...";
 
  constructor() {
    super("templates.create", "Create Template Files", "t");
  }
 
  async handler(context: ExtensionContext, ...args: any[]): Promise<void> {
    await super.handler(context, ...args);
 
    const picked = await this.showTemplateQuickPick();
 
    if (picked && picked.length) {
      const folder = await getSingleFolder();
 
      if (folder) {
        const templates = picked
          .map((p) => allTemplates.find((t) => t.name === p))
          .filter((t): t is SingleTemplate => t !== undefined);
 
        const createdFiles = await this.createTemplateFiles(templates, folder);
 
        await this.showSuccessMessages(templates, createdFiles);
      }
    }
  }
 
  async showTemplateQuickPick(): Promise<string[] | undefined> {
    return window.showQuickPick(
      allTemplates.map((t) => t.name),
      {
        canPickMany: true,
        placeHolder: MultiTemplate.PLACEHOLDER,
        ignoreFocusOut: true,
      }
    );
  }
 
  async createTemplateFiles(
    templates: SingleTemplate[],
    folder: Uri
  ): Promise<string[]> {
    return Promise.all(
      templates.map((template) => template.writeTemplateFile(folder))
    );
  }
 
  async showSuccessMessages(
    templates: SingleTemplate[],
    files: string[]
  ): Promise<void[]> {
    return Promise.all(
      templates.map((template, i) => template.showSuccessMessage(files[i]))
    );
  }
}
 
export default MultiTemplate;