gc.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #! /bin/bash
  2. NAME=$1
  3. FILE_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")/../packages" && pwd)
  4. re="[[:space:]]+"
  5. if [ "$#" -ne 1 ] || [[ $NAME =~ $re ]] || [ "$NAME" == "" ]; then
  6. echo "Usage: pnpm gc \${name} with no space"
  7. exit 1
  8. fi
  9. DIRNAME="$FILE_PATH/components/$NAME"
  10. INPUT_NAME=$NAME
  11. if [ -d "$DIRNAME" ]; then
  12. echo "$NAME component already exists, please change it"
  13. exit 1
  14. fi
  15. NORMALIZED_NAME=""
  16. for i in $(echo $NAME | sed 's/[_|-]\([a-z]\)/\ \1/;s/^\([a-z]\)/\ \1/'); do
  17. C=$(echo "${i:0:1}" | tr "[:lower:]" "[:upper:]")
  18. NORMALIZED_NAME="$NORMALIZED_NAME${C}${i:1}"
  19. done
  20. NAME=$NORMALIZED_NAME
  21. mkdir -p "$DIRNAME"
  22. mkdir -p "$DIRNAME/src"
  23. mkdir -p "$DIRNAME/__tests__"
  24. cat > $DIRNAME/src/$INPUT_NAME.vue <<EOF
  25. <template>
  26. <div>
  27. <slot />
  28. </div>
  29. </template>
  30. <script lang="ts" setup>
  31. import { ${INPUT_NAME}Props } from './$INPUT_NAME'
  32. defineOptions({
  33. name: 'El$NAME',
  34. })
  35. const props = defineProps(${INPUT_NAME}Props)
  36. // init here
  37. </script>
  38. EOF
  39. cat > $DIRNAME/src/$INPUT_NAME.ts <<EOF
  40. import { buildProps } from '@element-plus/utils'
  41. import type { ExtractPropTypes } from 'vue'
  42. import type $NAME from './$INPUT_NAME.vue'
  43. export const ${INPUT_NAME}Props = buildProps({})
  44. export type ${NAME}Props = ExtractPropTypes<typeof ${INPUT_NAME}Props>
  45. export type ${NAME}Instance = InstanceType<typeof $NAME>
  46. EOF
  47. cat <<EOF >"$DIRNAME/index.ts"
  48. import { withInstall } from '@element-plus/utils'
  49. import $NAME from './src/$INPUT_NAME.vue'
  50. export const El$NAME = withInstall($NAME)
  51. export default El$NAME
  52. export * from './src/$INPUT_NAME'
  53. EOF
  54. cat > $DIRNAME/__tests__/$INPUT_NAME.test.tsx <<EOF
  55. import { mount } from '@vue/test-utils'
  56. import { describe, expect, test } from 'vitest'
  57. import $NAME from '../src/$INPUT_NAME.vue'
  58. const AXIOM = 'Rem is the best girl'
  59. describe('$NAME.vue', () => {
  60. test('render test', () => {
  61. const wrapper = mount(() => <$NAME>{AXIOM}</$NAME>)
  62. expect(wrapper.text()).toEqual(AXIOM)
  63. })
  64. })
  65. EOF