2020-07-22 21:15:18 +08:00
|
|
|
#! /bin/bash
|
|
|
|
|
2024-11-04 21:19:29 +08:00
|
|
|
NAME=$(echo $1 | sed -E "s/([A-Z])/-\1/g" | sed -E "s/^-//g" | sed -E "s/_/-/g" | tr "A-Z" "a-z")
|
2020-07-22 21:15:18 +08:00
|
|
|
|
2020-07-22 21:33:14 +08:00
|
|
|
FILE_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")/../packages" && pwd)
|
2020-07-22 21:15:18 +08:00
|
|
|
|
|
|
|
re="[[:space:]]+"
|
|
|
|
|
|
|
|
if [ "$#" -ne 1 ] || [[ $NAME =~ $re ]] || [ "$NAME" == "" ]; then
|
2024-11-04 21:19:29 +08:00
|
|
|
echo "Usage: pnpm gen \${name} with no space"
|
2020-07-22 21:15:18 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-08-25 00:18:06 +08:00
|
|
|
DIRNAME="$FILE_PATH/components/$NAME"
|
2020-07-22 21:33:14 +08:00
|
|
|
INPUT_NAME=$NAME
|
2020-07-22 21:15:18 +08:00
|
|
|
|
|
|
|
if [ -d "$DIRNAME" ]; then
|
|
|
|
echo "$NAME component already exists, please change it"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-08-25 00:18:06 +08:00
|
|
|
|
2024-11-04 21:19:29 +08:00
|
|
|
NAME=$(echo $NAME | awk -F'-' '{ for(i=1; i<=NF; i++) { $i = toupper(substr($i,1,1)) tolower(substr($i,2)) } print $0 }' OFS='')
|
|
|
|
PROP_NAME=$(echo "${NAME:0:1}" | tr '[:upper:]' '[:lower:]')${NAME:1}
|
2020-07-22 21:15:18 +08:00
|
|
|
|
2020-07-24 19:28:18 +08:00
|
|
|
mkdir -p "$DIRNAME"
|
|
|
|
mkdir -p "$DIRNAME/src"
|
2024-11-04 21:19:29 +08:00
|
|
|
mkdir -p "$DIRNAME/style"
|
2020-07-24 19:28:18 +08:00
|
|
|
mkdir -p "$DIRNAME/__tests__"
|
|
|
|
|
2022-11-18 10:40:52 +08:00
|
|
|
cat > $DIRNAME/src/$INPUT_NAME.vue <<EOF
|
2020-07-24 19:28:18 +08:00
|
|
|
<template>
|
|
|
|
<div>
|
2022-11-18 10:40:52 +08:00
|
|
|
<slot />
|
2020-07-24 19:28:18 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
2022-11-18 10:40:52 +08:00
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2024-11-04 21:19:29 +08:00
|
|
|
import { ${PROP_NAME}Emits, ${PROP_NAME}Props } from './$INPUT_NAME'
|
2022-11-18 10:40:52 +08:00
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
name: 'El$NAME',
|
2020-07-29 15:09:29 +08:00
|
|
|
})
|
2022-11-18 10:40:52 +08:00
|
|
|
|
2024-11-04 21:19:29 +08:00
|
|
|
const props = defineProps(${PROP_NAME}Props)
|
|
|
|
const emit = defineEmits(${PROP_NAME}Emits)
|
2022-11-18 10:40:52 +08:00
|
|
|
|
|
|
|
// init here
|
2020-07-24 19:28:18 +08:00
|
|
|
</script>
|
2022-11-18 10:40:52 +08:00
|
|
|
EOF
|
|
|
|
|
|
|
|
cat > $DIRNAME/src/$INPUT_NAME.ts <<EOF
|
|
|
|
import { buildProps } from '@element-plus/utils'
|
|
|
|
|
|
|
|
import type { ExtractPropTypes } from 'vue'
|
|
|
|
|
2024-11-04 21:19:29 +08:00
|
|
|
export const ${PROP_NAME}Props = buildProps({} as const)
|
|
|
|
export type ${NAME}Props = ExtractPropTypes<typeof ${PROP_NAME}Props>
|
|
|
|
|
|
|
|
export const ${PROP_NAME}Emits = {}
|
|
|
|
export type ${NAME}Emits = typeof ${PROP_NAME}Emits
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat > $DIRNAME/src/instance.ts <<EOF
|
|
|
|
import type $NAME from './$INPUT_NAME.vue'
|
2022-11-18 10:40:52 +08:00
|
|
|
|
|
|
|
export type ${NAME}Instance = InstanceType<typeof $NAME>
|
2020-07-24 19:28:18 +08:00
|
|
|
EOF
|
2020-07-22 21:15:18 +08:00
|
|
|
|
2020-07-24 20:23:14 +08:00
|
|
|
cat <<EOF >"$DIRNAME/index.ts"
|
2022-11-18 10:40:52 +08:00
|
|
|
import { withInstall } from '@element-plus/utils'
|
|
|
|
import $NAME from './src/$INPUT_NAME.vue'
|
2024-11-04 21:19:29 +08:00
|
|
|
import type { SFCWithInstall } from '@element-plus/utils'
|
2020-11-20 20:24:16 +08:00
|
|
|
|
2024-11-04 21:19:29 +08:00
|
|
|
export const El$NAME: SFCWithInstall<typeof $NAME> = withInstall($NAME)
|
2022-11-18 10:40:52 +08:00
|
|
|
export default El$NAME
|
2020-11-20 20:24:16 +08:00
|
|
|
|
2022-11-18 10:40:52 +08:00
|
|
|
export * from './src/$INPUT_NAME'
|
2024-11-04 21:19:29 +08:00
|
|
|
export type { ${NAME}Instance } from './src/instance'
|
2020-07-24 19:28:18 +08:00
|
|
|
EOF
|
2020-07-22 21:15:18 +08:00
|
|
|
|
2022-11-18 10:40:52 +08:00
|
|
|
cat > $DIRNAME/__tests__/$INPUT_NAME.test.tsx <<EOF
|
2020-07-29 15:09:29 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-11-18 10:40:52 +08:00
|
|
|
import { describe, expect, test } from 'vitest'
|
|
|
|
import $NAME from '../src/$INPUT_NAME.vue'
|
2020-07-24 19:28:18 +08:00
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
|
|
|
describe('$NAME.vue', () => {
|
|
|
|
test('render test', () => {
|
2022-11-18 10:40:52 +08:00
|
|
|
const wrapper = mount(() => <$NAME>{AXIOM}</$NAME>)
|
|
|
|
|
2020-07-27 16:17:23 +08:00
|
|
|
expect(wrapper.text()).toEqual(AXIOM)
|
2020-07-24 19:28:18 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
EOF
|
2024-11-04 21:19:29 +08:00
|
|
|
|
|
|
|
cat > $DIRNAME/style/index.ts <<EOF
|
|
|
|
import '@element-plus/components/base/style'
|
|
|
|
import '@element-plus/theme-chalk/src/$INPUT_NAME.scss'
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat > $DIRNAME/style/css.ts <<EOF
|
|
|
|
import '@element-plus/components/base/style/css'
|
|
|
|
import '@element-plus/theme-chalk/el-$INPUT_NAME.css'
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat > $FILE_PATH/theme-chalk/src/$INPUT_NAME.scss <<EOF
|
|
|
|
EOF
|
|
|
|
|
|
|
|
perl -0777 -pi -e "s/\n\n/\nexport * from '.\/$INPUT_NAME'\n\n/" $FILE_PATH/components/index.ts
|
|
|
|
|
|
|
|
TYPE_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")/../typings" && pwd)
|
|
|
|
|
|
|
|
perl -0777 -pi -e "s/\n\s+}/\n El$NAME: typeof import('element-plus')['El$NAME']\n }/" $TYPE_PATH/global.d.ts
|