binhonglee/GlobeTrotte

View on GitHub
src/cockpit/scripts/genComponent.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { existsSync, writeFileSync } from "fs";

const componentFolder = "src/cockpit/components/";
const componentFileTemplate = `<template lang="pug">
.COMPONENT_NAME_SNAKE
  //- TODO
</template>

<script lang="ts">
import { defineComponent } from "vue";

interface Data {}

export default defineComponent({
  data: (): Data => ({}),
  // TODO
});
</script>

<style scoped>
</style>
`;

const componentTestFolder = "src/cockpit/tests/components/";
const testFileTemplate = `import COMPONENT_NAME from "components/COMPONENT_NAME.vue";

import { mount } from "@vue/test-utils";
import { expect, test } from "ava";

test("renders empty component", () => {
  const wrapper = mount(COMPONENT_NAME, {});
});
`;

const args = process.argv.slice(2);

args.forEach((componentName) => {
  const snake_case_arr = componentName.slice(1).split("");
  snake_case_arr.forEach((c: string, index: number) => {
    if (c === "-") {
      process.exit();
    }

    if (index === 0) {
      snake_case_arr[index] = snake_case_arr[index].toLocaleLowerCase();
    } else if (
      snake_case_arr[index].toLocaleLowerCase() !== snake_case_arr[index]
    ) {
      snake_case_arr[index] = "_" + snake_case_arr[index].toLocaleLowerCase();
    }
  });

  const snake_case = snake_case_arr.join("");
  const componentFile = componentFolder + componentName + ".vue";
  const testFile = componentTestFolder + componentName + ".spec.ts";

  if (existsSync(componentFile)) {
    console.log("File " + componentFile + " already exists. Skipping...");
  } else {
    writeFileSync(
      componentFile,
      componentFileTemplate
        .split("COMPONENT_NAME_SNAKE")
        .join(snake_case)
        .split("COMPONENT_NAME")
        .join(componentName),
    );
    console.log("File " + componentFile + " created.");
  }

  if (existsSync(testFile)) {
    console.log("File " + testFile + " already exists. Skipping...");
  } else {
    writeFileSync(
      testFile,
      testFileTemplate.split("COMPONENT_NAME").join(componentName),
    );
    console.log("File " + testFile + " created.");
  }
});