create-tool.mjs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { mkdir, readFile, writeFile } from 'fs/promises';
  2. import { dirname, join } from 'path';
  3. import { fileURLToPath } from 'url';
  4. const currentDirname = dirname(fileURLToPath(import.meta.url));
  5. const toolsDir = join(currentDirname, '..', 'src', 'tools');
  6. // eslint-disable-next-line no-undef
  7. const toolName = process.argv[2];
  8. if (!toolName) {
  9. throw new Error('Please specify a toolname.');
  10. }
  11. const toolNameCamelCase = toolName.replace(/-./g, (x) => x[1].toUpperCase());
  12. const toolNameTitleCase = toolName[0].toUpperCase() + toolName.slice(1).replace(/-/g, ' ');
  13. const toolDir = join(toolsDir, toolName);
  14. await mkdir(toolDir);
  15. console.log(`Directory created: ${toolDir}`);
  16. const createToolFile = async (name, content) => {
  17. const filePath = join(toolDir, name);
  18. await writeFile(filePath, content.trim());
  19. console.log(`File created: ${filePath}`);
  20. };
  21. createToolFile(
  22. `${toolName}.vue`,
  23. `
  24. <template>
  25. <div>
  26. Lorem ipsum
  27. </div>
  28. </template>
  29. <script setup lang="ts">
  30. </script>
  31. <style lang="less" scoped>
  32. </style>
  33. `,
  34. );
  35. createToolFile(
  36. `index.ts`,
  37. `
  38. import { ArrowsShuffle } from '@vicons/tabler';
  39. import { defineTool } from '../tool';
  40. export const tool = defineTool({
  41. name: '${toolNameTitleCase}',
  42. path: '/${toolName}',
  43. description: '',
  44. keywords: ['${toolName.split('-').join("', '")}'],
  45. component: () => import('./${toolName}.vue'),
  46. icon: ArrowsShuffle,
  47. createdAt: new Date('${new Date().toISOString().split('T')[0]}'),
  48. });
  49. `,
  50. );
  51. createToolFile(`${toolName}.service.ts`, ``);
  52. createToolFile(
  53. `${toolName}.service.test.ts`,
  54. `
  55. import { expect, describe, it } from 'vitest';
  56. // import { } from './${toolName}.service';
  57. //
  58. // describe('${toolName}', () => {
  59. //
  60. // })
  61. `,
  62. );
  63. createToolFile(
  64. `${toolName}.e2e.spec.ts`,
  65. `
  66. import { test, expect } from '@playwright/test';
  67. test.describe('Tool - ${toolNameTitleCase}', () => {
  68. test.beforeEach(async ({ page }) => {
  69. await page.goto('/${toolName}');
  70. });
  71. test('Has correct title', async ({ page }) => {
  72. await expect(page).toHaveTitle('${toolNameTitleCase} - IT Tools');
  73. });
  74. test('', async ({ page }) => {
  75. });
  76. });
  77. `,
  78. );
  79. const toolsIndex = join(toolsDir, 'index.ts');
  80. const indexContent = await readFile(toolsIndex, { encoding: 'utf-8' }).then((r) => r.split('\n'));
  81. indexContent.splice(3, 0, `import { tool as ${toolNameCamelCase} } from './${toolName}';`);
  82. writeFile(toolsIndex, indexContent.join('\n'));
  83. console.log(`Added import in: ${toolsIndex}`);