feat: add rapper

pull/103/head
bigfengyu 5 years ago
parent 86b3e4284a
commit 4735914244

@ -1,6 +1,7 @@
import React, { useState } from 'react'
import { createStyles, makeStyles, Theme } from '@material-ui/core/styles'
import Dialog from '@material-ui/core/Dialog'
import TextField from '@material-ui/core/TextField'
import config from '../../config'
import AppBar from '@material-ui/core/AppBar'
import Toolbar from '@material-ui/core/Toolbar'
@ -13,17 +14,65 @@ import { DialogContent } from '@material-ui/core'
import Radio from '@material-ui/core/Radio'
import RadioGroup from '@material-ui/core/RadioGroup'
import FormControlLabel from '@material-ui/core/FormControlLabel'
import FormControl from '@material-ui/core/FormControl'
import FormLabel from '@material-ui/core/FormLabel'
import { Repository } from 'actions/types'
// import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter'
// import docco from 'react-syntax-highlighter/dist/esm/styles/hljs/docco'
// import bash from 'react-syntax-highlighter/dist/esm/languages/prism/bash'
type RapperType = 'requester' | 'redux'
// SyntaxHighlighter.registerLanguage('bash', bash)
const Transition = React.forwardRef<unknown, TransitionProps>((props, ref) => {
return <Slide direction="up" ref={ref} {...props} />
})
type RapperType = 'requester' | 'redux'
const codeTmpl = ({ projectId, token, rapperType, rapperPath }: {
projectId: number
token: string
rapperType: RapperType
rapperPath: string
}) => {
const apiUrl = `${config.serve}/repository/get?id=${projectId}&token=${token}`
const rapUrl = window.location.origin
return `"rapper": "rapper --type ${rapperType} --rapperPath ${rapperPath} --apiUrl ${apiUrl} --rapUrl ${rapUrl}"`
}
const useReadmeStyles = makeStyles({
root: {
'&': {
fontSize: 16,
},
},
})
function Readme() {
const classes = useReadmeStyles()
return (
<div className={classes.root}>
<p style={{ textAlign: 'center' }}>
<a href="https://github.com/thx/rapper" target="_blank" rel="noopener noreferrer">
<img src="https://img.alicdn.com/tfs/TB1SlW9lQT2gK0jSZPcXXcKkpXa-1138-220.png" alt="Logo" width="250" />
</a>
<h3></h3>
<p>
<a href="https://www.yuque.com/rap/rapper/readme" target="_blank" rel="noopener noreferrer"></a>
</p>
</p>
<h2>Rapper </h2>
<p>Rapper TypeScript </p>
<ul>
<li> HTTP </li>
<li>/</li>
<li> React/Redux hooks 使</li>
</ul>
<p><a href="https://www.yuque.com/rap/rapper/readme" target="_blank" rel="noopener noreferrer"></a></p>
<p>使21912534</p>
{/* <div style={{ textAlign: 'center' }}> */}
<img src="https://img.alicdn.com/tfs/TB1mLzfnF67gK0jSZPfXXahhFXa-828-1068.png" alt="dingtalk" width="200" />
{/* </div> */}
<h2></h2>
</div>
)
}
const useStyles = makeStyles(({ spacing }: Theme) =>
createStyles({
@ -39,57 +88,25 @@ const useStyles = makeStyles(({ spacing }: Theme) =>
marginTop: spacing(2),
},
content: {
width: '70vw',
margin: '0 auto',
paddingTop: 30,
padding: '30px 15vw 40vh 15vw',
},
formControl: {
margin: spacing(3),
margin: 0,
},
formItem: {
marginBottom: 16,
},
formLabel: {
fontSize: 16,
marginBottom: 5,
},
step: {
fontSize: 16,
marginBottom: 10,
},
})
)
const Transition = React.forwardRef<unknown, TransitionProps>((props, ref) => {
return <Slide direction="up" ref={ref} {...props} />
})
// /** 同步 rap 接口到本地 */
// const { rapper } = require('@ali/rapper');
// const { resolve } = require('path');
// rapper({
// /** 生成的请求类型 */
// type: 'requester',
// /** 必须配置rap 仓库 id */
// projectId: ${projectId},
// /** 可选,输出文件的目录,默认是 ./src/rapper/ */
// rapperPath: resolve(process.cwd(), './src/rapper/'),
// /** 可选rap 前端地址,默认是 http://rap2.taobao.org */
// rapUrl: '${window.location.origin}',
// /** 可选rap 后端 api 地址,默认是 http://rap2api.taobao.org */
// apiUrl: '${config.serve}/repository/get?id=${projectId}'
// /** 可选,输出模板代码的格式,具体见 prettier的配置规则 https://prettier.io/docs/en/options.html */
// codeStyle: {
// /** 默认单引号 */
// singleQuote: true,
// /** 默认2个空格 */
// tabWidth: 2,
// /** 分号结尾默认false */
// semi: false,
// /** 逗号 */
// trailingComma: 'es5',
// },
// });
const codeTmpl = ({ projectId, token, rapperType }: {
projectId: number
token: string
rapperType: RapperType
}) => {
const apiUrl = `${config.serve}/repository/get?id=${projectId}&token=${token}`
const rapUrl = window.location.origin
return `npx rap --type ${rapperType} --apiUrl ${apiUrl} --rapUrl ${rapUrl}`
}
function RapperInstallerModal({
open,
handleClose,
@ -101,8 +118,12 @@ function RapperInstallerModal({
}) {
const classes = useStyles()
/** rapper 类型 requester redux */
const [rapperType, setRapperType] = useState<RapperType>('requester')
/** rapper 生成目录地址 */
const [rapperPath, setRapperPath] = useState<string>('src/rapper')
function handleRapperTypeChange(_event: React.ChangeEvent<HTMLInputElement>, value: RapperType) {
setRapperType(value)
}
@ -125,21 +146,42 @@ function RapperInstallerModal({
<CloseIcon />
</IconButton>
<Typography variant="h6" className={classes.title}>
Rapper
Rapper
</Typography>
</Toolbar>
</AppBar>
<DialogContent className={classes.content}>
<FormControl component="fieldset" className={classes.formControl}>
<FormLabel component="legend"></FormLabel>
<Readme />
<div className={classes.formItem}>
<FormLabel component="legend" className={classes.formLabel}></FormLabel>
<RadioGroup aria-label="rapperType" row={true} name="rapperType" value={rapperType} onChange={handleRapperTypeChange}>
<FormControlLabel value="requester" control={<Radio />} label="请求函数" />
<FormControlLabel value="requester" control={<Radio />} label="通用请求函数/类型" />
<FormControlLabel value="redux" control={<Radio />} label="React/Redux 集成" />
</RadioGroup>
</FormControl>
</div>
<div className={classes.formItem}>
<FormLabel component="legend" className={classes.formLabel}>Rapper </FormLabel>
<TextField
placeholder="src/rapper"
fullWidth={true}
margin="normal"
variant="outlined"
value={rapperPath}
onChange={(event) => setRapperPath(event.target.value)}
/>
</div>
<p className={classes.step}>1. rapper </p>
<pre>npm install rap --save-dev</pre>
<p className={classes.step}>2. package.json scripts </p>
<pre>
{codeTmpl({ projectId: repository.id, token: repository.token, rapperType })}
{codeTmpl({ projectId: repository.id, token: repository.token, rapperType, rapperPath })}
</pre>
<p className={classes.step}>3. rapper </p>
<pre>
npm run rapper
</pre>
<p className={classes.step}>4. <a href="https://www.yuque.com/rap/rapper/use" target="_blank" rel="noopener noreferrer">使</a></p>
</DialogContent>
</Dialog>
)

@ -126,6 +126,8 @@ class RepositoryEditor extends Component<Props, States> {
auth,
} = this.props
const { repository: repositoryAsync } = this.props
const idStr = auth.id.toString()
const canTestRapper = (idStr[idStr.length - 1] === '2') || idStr === '128226'
if (!repositoryAsync.fetching && !repositoryAsync.data) {
return <div className="p100 fontsize-30 text-center"></div>
}
@ -240,13 +242,13 @@ class RepositoryEditor extends Component<Props, States> {
handleClose={() => this.setState({ defaultValuesModalOpen: false })}
repositoryId={repository.id}
/>
<span
{canTestRapper && <span
className="fake-link edit"
style={{color: '#f95e49'}}
onClick={() => this.setState({ rapperInstallerModalOpen: true })}
>
<GoCode /> TS
</span>
<GoCode /> TS
</span>}
<RapperInstallerModal
open={this.state.rapperInstallerModalOpen}
handleClose={() => this.setState({ rapperInstallerModalOpen: false })}

Loading…
Cancel
Save