index.ts 764 B

12345678910111213141516171819202122232425262728293031323334
  1. import { createWriteStream } from "fs";
  2. import prettier from "prettier";
  3. import { join } from "path";
  4. import { mkdirs } from "../../utils";
  5. // 创建文件
  6. export async function createMenu(options: { viewPath: string; code: string }) {
  7. // 格式化内容
  8. const content = prettier.format(options.code, {
  9. parser: "vue",
  10. useTabs: true,
  11. tabWidth: 4,
  12. endOfLine: "lf",
  13. semi: true,
  14. jsxBracketSameLine: true,
  15. singleQuote: false,
  16. printWidth: 100,
  17. trailingComma: "none"
  18. });
  19. // 目录路径
  20. const dir = (options.viewPath || "").split("/");
  21. // 文件名
  22. const fname = dir.pop();
  23. // 创建目录
  24. const path = mkdirs(`./src/${dir.join("/")}`);
  25. // 创建文件
  26. createWriteStream(join(path, fname || "demo"), {
  27. flags: "w"
  28. }).write(content);
  29. }