Browse Source

Follow up - 53fe71a47f - Maintenance: Improve Template Generator

Co-authored-by: Benjamin Scharf <bs@zammad.com>
Co-authored-by: Dusan Vuckovic <dv@zammad.com>
Benjamin Scharf 4 months ago
parent
commit
68ae60514c

+ 38 - 15
.dev/hygen/.hygen.js

@@ -4,25 +4,48 @@ const config = require('./lib.config.js')
 
 module.exports = {
   helpers: {
-    componentLibrary: (libraryName, path = true) => {
-      if (libraryName === 'Desktop') return path ? 'apps/desktop' : 'desktop'
-      if (libraryName === 'Mobile') return path ? 'apps/mobile' : 'mobile'
-      if (libraryName === 'Shared') return 'shared'
-    },
-    composableName: (name, h) => {
-      return h.changeCase.camel(`use${name}`)
+    componentLibrary: (directoryScope, path = true) => {
+      if (directoryScope === 'Desktop') return path ? 'apps/desktop' : 'desktop'
+      if (directoryScope === 'Mobile') return path ? 'apps/mobile' : 'mobile'
+      if (directoryScope === 'Shared') return 'shared'
+
+      throw new Error('Directory scope not found')
     },
-    componentGenericWitPrefix: (name, h) => {
-      const nameWithGenericPrefix = name.replace(
-        new RegExp(`${config.generic.prefix}`, 'i'),
-        '',
-      )
-      return h.changeCase.pascal(
-        `${config.generic.prefix}${h.changeCase.pascal(nameWithGenericPrefix)}`,
-      )
+    getPath(type, options) {
+      switch (type) {
+        case 'genericComponent':
+          return `../../app/frontend/${this.componentLibrary(options.directoryScope)}/components/${options.suffix}`
+        case 'composable':
+          return `../../app/frontend/${this.componentLibrary(options.directoryScope)}/composables/${options.suffix}`
+        case 'store':
+          return `../../app/frontend/${this.componentLibrary(options.directoryScope)}/stores/${options.suffix}`
+        default:
+          return type
+      }
     },
     zammadCopyright: () => {
       return `Copyright (C) 2012-${new Date().getFullYear()} Zammad Foundation, https://zammad-foundation.org/`
     },
+    usePrefix(name, type = 'use') {
+      if (type === 'use') {
+        const nameWithGenericPrefix = name.replace(
+          new RegExp(`${config.convention.vue.use}`, 'i'),
+          '',
+        )
+        return this.changeCase.camel(
+          `${config.convention.vue.use}${this.changeCase.pascal(nameWithGenericPrefix)}`,
+        )
+      }
+
+      if (type === 'generic') {
+        const nameWithGenericPrefix = name.replace(
+          new RegExp(`${config.generic.prefix}`, 'i'),
+          '',
+        )
+        return this.changeCase.pascal(
+          `${config.generic.prefix}${this.changeCase.pascal(nameWithGenericPrefix)}`,
+        )
+      }
+    },
   },
 }

+ 0 - 10
.dev/hygen/_templates/component/new/Component.vue.t

@@ -1,10 +0,0 @@
----
-to: ../../app/frontend/<%= h.componentLibrary(libraryName) %>/components/<%= h.componentGenericWitPrefix(name, h) %>/<%= h.componentGenericWitPrefix(name, h) %>.vue
----
-<!-- <%= h.zammadCopyright() %> -->
-
-<script setup lang="ts"></script>
-
-<template>
-  <div>Hello <%= h.componentGenericWitPrefix(name, h) %></div>
-</template>

+ 0 - 14
.dev/hygen/_templates/component/new/componentTest.spec.ts.t

@@ -1,14 +0,0 @@
----
-to: ../../app/frontend/<%= h.componentLibrary(libraryName) %>/components/<%= h.componentGenericWitPrefix(name, h) %>/__tests__/<%= h.componentGenericWitPrefix(name, h) %>.spec.ts
----
-// <%= h.zammadCopyright() %>
-
-import { renderComponent } from '#tests/support/components/index.ts'
-import <%= h.componentGenericWitPrefix(name, h) %> from '#<%= h.componentLibrary(libraryName, false) %>/components/<%= h.componentGenericWitPrefix(name, h) %>/<%= h.componentGenericWitPrefix(name, h) %>.vue'
-
-describe('<%= h.componentGenericWitPrefix(name, h) %>', () => {
-  it('renders <%= h.componentGenericWitPrefix(name, h) %>', () => {
-    const wrapper = renderComponent(<%= h.componentGenericWitPrefix(name, h) %>, {})
-    expect(wrapper).toBeInTheDocument()
-  })
-})

+ 0 - 6
.dev/hygen/_templates/component/new/composable.ts.t

@@ -1,6 +0,0 @@
----
-to: "../../<%= withComposable ? `app/frontend/${h.componentLibrary(libraryName, h)}/components/${h.componentGenericWitPrefix(name, h)}/${h.composableName(h.componentGenericWitPrefix(name, h), h)}.ts` : null %>"
----
-// <%= h.zammadCopyright() %>
-
-export const <%= h.composableName(h.componentGenericWitPrefix(name, h), h) %> = () => {}

+ 0 - 14
.dev/hygen/_templates/component/new/composableTest.spec.ts.t

@@ -1,14 +0,0 @@
----
-to: "../../<%= withTypeFile ? `app/frontend/${h.componentLibrary(libraryName, h)}/components/${h.componentGenericWitPrefix(name, h)}/__tests__/${h.composableName(h.componentGenericWitPrefix(name, h), h)}.spec.ts` : null %>"
----
-// <%= h.zammadCopyright() %>
-
-import { renderComponent } from '#tests/support/components/index.ts'
-import { <%= h.composableName(h.componentGenericWitPrefix(name, h), h) %> } from '#<%= h.componentLibrary(libraryName, false) %>/components/<%= h.componentGenericWitPrefix(name, h) %>/<%= h.composableName(h.componentGenericWitPrefix(name, h), h) %>.ts'
-
-describe('<%= h.composableName(h.componentGenericWitPrefix(name, h), h) %>', () => {
-  it('test <%= h.composableName(h.componentGenericWitPrefix(name, h), h) %>', () => {
-    const composable = <%= h.composableName(h.componentGenericWitPrefix(name, h), h) %>()
-    expect(composable).toHaveBeenCalledOnce()
-  })
-})

+ 0 - 8
.dev/hygen/_templates/component/new/type.ts.t

@@ -1,8 +0,0 @@
----
-to: "../../<%= withTypeFile ? `app/frontend/${h.componentLibrary(libraryName, h)}/components/${h.componentGenericWitPrefix(name, h)}/types.ts` : null %>"
----
-// Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
-
-export interface Dummy {
-  // Add your interface here
-}

+ 10 - 0
.dev/hygen/_templates/new/component/component.vue.t

@@ -0,0 +1,10 @@
+---
+to: <%- h.getPath(path) %>
+---
+<!-- <%= h.zammadCopyright() %> -->
+
+<script setup lang="ts"></script>
+
+<template>
+  <div>Hello <%= h.convertCase.pascal(componentName) %></div>
+</template>

+ 16 - 0
.dev/hygen/_templates/new/component/prompt.js

@@ -0,0 +1,16 @@
+// Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
+
+module.exports = [
+  {
+    type: 'input',
+    name: 'path',
+    // eslint-disable-next-line zammad/zammad-detect-translatable-string
+    message: 'Path from directory scope (desktop/mobile/shared)?',
+  },
+  {
+    type: 'input',
+    name: 'name',
+    // eslint-disable-next-line zammad/zammad-detect-translatable-string
+    message: 'Component name?',
+  },
+]

+ 8 - 0
.dev/hygen/_templates/new/composable/composable.ts.t

@@ -0,0 +1,8 @@
+---
+to: "<%= h.getPath('composable', {directoryScope: directoryScope, suffix: `${h.usePrefix(composableName, 'use')}.ts`}) %>"
+---
+// <%= h.zammadCopyright() %>
+
+export const <%= h.usePrefix(composableName, 'use') %> = () => {
+  return {}
+}

+ 13 - 0
.dev/hygen/_templates/new/composable/composableTest.spec.ts.t

@@ -0,0 +1,13 @@
+---
+to: "<%= h.getPath('composable', {directoryScope: directoryScope, suffix: `__tests__/${h.usePrefix(composableName, 'use')}.spec.ts`}) %>"
+---
+// <%= h.zammadCopyright() %>
+
+import { <%= h.usePrefix(composableName, 'use') %> } from '#<%= h.componentLibrary(directoryScope, false) %>/composables/<%= h.usePrefix(composableName, 'use') %>.ts'
+
+describe('<%= h.usePrefix(composableName, 'use') %>', () => {
+  it.todo('test <%= h.usePrefix(composableName, 'use') %>', () => {
+    const composable = <%= h.usePrefix(composableName, 'use') %>()
+    expect(composable)
+  })
+})

Some files were not shown because too many files changed in this diff