All files / commands/template-creators/single-template index.ts

100% Statements 25/25
100% Branches 14/14
100% Functions 4/4
100% Lines 25/25

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                    2x                       2x 2x                                       16x   16x           16x 16x 16x 16x 16x   16x           16x 16x   16x 8x   8x         8x   8x             8x       24x           24x 8x 8x 16x 8x      
// Externals
import { env, window, Uri, workspace, ExtensionContext } from "vscode";
import { join } from "path";
import { promises } from "fs";
import { writeFile as writeJSONFile } from "jsonfile";
 
// Internals
import BaseCommand from "../../base-command";
import { getSingleFolder } from "../../helpers";
 
const { writeFile } = promises;
 
interface SingleTemplateParams {
  name: string;
  template: string | Record<string, any>;
  templateFileName: string;
  docsLink: string;
  isConfig: boolean;
  keybinding: string;
}
 
export default class SingleTemplate extends BaseCommand {
  static readonly VIEW_TEMPLATE = "View Template";
  static readonly VIEW_DOCS = "View Docs";
 
  // Provided instance vars
  name: string;
  templateFileName: string;
  template: string | Record<string, any>;
  docsLink: string;
  isConfig: boolean;
 
  // Constructed instance vars
  onSuccess: string;
 
  constructor(params: SingleTemplateParams) {
    const {
      name,
      templateFileName,
      template,
      docsLink,
      isConfig,
      keybinding,
    } = params;
 
    super(
      `templates.create.${name}${isConfig ? "Config" : ""}`,
      `Create ${name}${isConfig ? " Configuration" : ""} Template`,
      keybinding
    );
 
    this.name = name;
    this.templateFileName = templateFileName;
    this.template = template;
    this.docsLink = docsLink;
    this.isConfig = isConfig;
 
    this.onSuccess = `Created ${name}${
      isConfig ? " Configuration" : ""
    } Template!`;
  }
 
  async handler(context: ExtensionContext, ...args: any[]): Promise<void> {
    await super.handler(context, ...args);
    const folderResult = await getSingleFolder();
 
    if (folderResult) {
      const filePath = await this.writeTemplateFile(folderResult);
 
      await this.showSuccessMessage(filePath);
    }
  }
 
  async writeTemplateFile(folder: Uri): Promise<string> {
    const filePath = join(folder.fsPath, this.templateFileName);
 
    typeof this.template === "string"
      ? await writeFile(filePath, this.template, "utf-8")
      : await writeJSONFile(filePath, this.template, {
          EOL: "\n",
          spaces: 2,
        });
 
    return filePath;
  }
 
  async showSuccessMessage(filePath: string): Promise<void> {
    const selected = await window.showInformationMessage(
      this.onSuccess,
      SingleTemplate.VIEW_TEMPLATE,
      SingleTemplate.VIEW_DOCS
    );
 
    if (selected === SingleTemplate.VIEW_TEMPLATE) {
      const doc = await workspace.openTextDocument(filePath);
      await window.showTextDocument(doc);
    } else if (selected === SingleTemplate.VIEW_DOCS)
      await env.openExternal(Uri.parse(this.docsLink));
  }
}