Skip to content

Commit

Permalink
Feature/tua api (#59)
Browse files Browse the repository at this point in the history
* perf(tua-mp, cli, service): add test for test/

* fix: close #58, update npm deps, fix mock

* feat(cli): update template for tua-api

* refactor: commander -> yargs
  • Loading branch information
BuptStEve authored Oct 17, 2018
1 parent 972a592 commit 878a403
Show file tree
Hide file tree
Showing 53 changed files with 375 additions and 272 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ TuaPage({

| 项目 | 状态 | 介绍 |
| --- | --- | --- |
| @tua-mp/cli | <img src="https://img.shields.io/npm/v/@tua-mp/cli.svg" alt="Version"> | 命令行工具
| @tua-mp/service | <img src="https://img.shields.io/npm/v/@tua-mp/service.svg" alt="Version"> | 项目构建工具
| tua-storage | <img src="https://img.shields.io/npm/v/tua-storage.svg" alt="Version"> | 通用存储层
| @tua-mp/api | 待整理开源 | api 工具
| @tua-mp/cli | <a href="https://tuateam.github.io/tua-mp/tua-mp-cli/"><img src="https://img.shields.io/npm/v/@tua-mp/cli.svg" alt="Version"></a> | 命令行工具
| @tua-mp/service | <a href="https://tuateam.github.io/tua-mp/tua-mp-service/"><img src="https://img.shields.io/npm/v/@tua-mp/service.svg" alt="Version"></a> | 项目构建工具
| tua-api | <a href="https://tuateam.github.io/tua-api/"><img src="https://img.shields.io/npm/v/tua-api.svg" alt="Version"></a> | api 生成工具
| tua-storage | <a href="https://tuateam.github.io/tua-mp/tua-storage/"><img src="https://img.shields.io/npm/v/tua-storage.svg" alt="Version"></a> | 通用存储层

## TODO
详见 [issues](https://github.com/tuateam/tua-mp/issues)
Expand Down
2 changes: 1 addition & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {
text: '生态系统',
items: [
{ text: '本地存储', link: '/tua-storage/' },
// { text: 'api 配置(待开源)', link: '/tua-api-wx' },
{ text: 'api 生成工具', link: 'https://tuateam.github.io/tua-api/' },
],
},
],
Expand Down
Binary file modified docs/.vuepress/public/cli/add-api.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/.vuepress/public/cli/add-comp-global.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/.vuepress/public/cli/add-comp-local.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/.vuepress/public/cli/add-page.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions docs/tua-mp-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ $ yarn global add @tua-mp/cli
$ tuamp init <template-name> <project-name>
```

### 2.2.添加接口命令 `add:api`
这个命令将添加一个新文件 `<name>.js``src/apis/` 下,并且自动添加到 `src/apis/index.js` 中导出
### 2.2.添加接口命令 `add api`
这个命令将添加一个新文件 `<name>.js``src/apis/` 下,并且如果不存在 `src/apis/index.js` 则会自动创建

```bash
# 添加小程序端 api
$ tuamp add:api <name>
$ tuamp add api <name>
```

<img :src="$withBase('/cli/add-api.gif')" alt="add:api">
<img :src="$withBase('/cli/add-api.gif')" alt="add api">

该命令会自动在 `src/apis/index.js` 中导出该 `api`,例如 `<name>``foo-bar`,那么导出的接口名称为 `fooBarApi`(已转成小驼峰,并在结尾加上 `Api`)。

### 2.3.添加页面命令 `add:page`
### 2.3.添加页面命令 `add page`
这个命令将添加一个新文件夹 `<name>``src/pages/` 下,并且自动添加 `pages/<name>/<name>``src/app/app.json` 中。

并且如果页面以**连字符**命名,那么相关的 `.vue` 文件会被转成**大驼峰**风格。
Expand All @@ -59,14 +59,14 @@ $ tuamp add:api <name>

```bash
# 添加小程序端 page
$ tuamp add:page <name>
$ tuamp add page <name>
```

<img :src="$withBase('/cli/add-page.gif')" alt="add:page">
<img :src="$withBase('/cli/add-page.gif')" alt="add page">

该命令会自动读取 `src/app/app.json` 中的 `pages` 字段,并将新页面加入。

### 2.4.添加页面命令 `add:comp`
### 2.4.添加页面命令 `add comp`
由于组件一般分为两种:

* 全局组件(位于 `src/comps/` 下)
Expand All @@ -86,17 +86,17 @@ $ tuamp add:page <name>

```bash
# 全局添加小程序端 comp(-g, --global)
$ tuamp add:comp -g <name>
$ tuamp add comp <name> -g
```

<img :src="$withBase('/cli/add-comp-global.gif')" alt="add:comp -g">
<img :src="$withBase('/cli/add-comp-global.gif')" alt="add comp -g">

```
# 局部添加小程序端 comp
$ tuamp add:comp <name>
$ tuamp add comp <name>
```

<img :src="$withBase('/cli/add-comp-local.gif')" alt="add:comp">
<img :src="$withBase('/cli/add-comp-local.gif')" alt="add comp">

::: tip
注意使用 `<Tab>` 键补全路径,输入不存在的文件夹时会自动创建。
Expand Down
2 changes: 1 addition & 1 deletion docs/tua-storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sidebar: auto
# tua-storage
这个项目希望统一封装各个端(小程序、web 端、React-Native)中对于缓存层的使用。采用 ES6+ 语法,将全部 api 使用 Promise 包裹,并采用 jest 进行了完整的单元测试。

<a href="https://circleci.com/gh/tuateam/tua-storage/tree/master"><img src="https://img.shields.io/circleci/project/tuateam/tua-storage/master.svg" alt="Build Status"></a>
<a href="https://circleci.com/gh/tuateam/tua-storage/tree/master"><img src="https://img.shields.io/circleci/project/github/tuateam/tua-storage/master.svg" alt="Build Status"></a>
<a href="https://codecov.io/github/tuateam/tua-storage?branch=master"><img src="https://img.shields.io/codecov/c/github/tuateam/tua-storage/master.svg" alt="Coverage Status"></a>
<a href="https://www.npmjs.com/package/tua-storage"><img src="https://img.shields.io/npm/v/tua-storage.svg" alt="Version"></a>
<a href="https://www.npmjs.com/package/tua-storage"><img src="https://img.shields.io/npm/l/tua-storage.svg" alt="License"></a>
Expand Down
28 changes: 28 additions & 0 deletions packages/tua-mp-cli/bin/tuamp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node

const semver = require('semver')

const packageJson = require('../package.json')

const {
name: projectName,
engines: { node: requiredVersion },
} = packageJson

// 检查 node 版本
if (!semver.satisfies(process.version, requiredVersion)) {
console.error(
`You are using Node ${process.version}, but ${projectName} ` +
`requires Node ${requiredVersion}.` +
`Please upgrade your Node version.`
)
process.exit(1)
}

require('yargs')
.alias('v', 'version')
.alias('h', 'help')
// 命令目录
.commandDir('../commands/')
.demandCommand(1)
.parse()
71 changes: 0 additions & 71 deletions packages/tua-mp-cli/bin/tuamp.js

This file was deleted.

7 changes: 7 additions & 0 deletions packages/tua-mp-cli/commands/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.desc = 'add some template code to your project'
exports.aliases = 'a'
exports.command = 'add <command>'

exports.builder = yargs => yargs.commandDir('./addCmds')

exports.handler = () => {}
12 changes: 12 additions & 0 deletions packages/tua-mp-cli/commands/addCmds/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 新增 api

exports.desc = 'add an api for your project'
exports.aliases = 'a'
exports.command = 'api <name>'
exports.builder = {}

exports.handler = (argv) => {
const name = argv.name

require('../../lib/addApi')(name)
}
19 changes: 19 additions & 0 deletions packages/tua-mp-cli/commands/addCmds/comp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 新增组件

exports.desc = 'add a component for your project'
exports.aliases = 'c'
exports.command = 'comp <name>'

exports.builder = (yargs) => yargs
.option('global', {
alias: 'g',
default: false,
describe: 'add a global component in src/comps/',
})

exports.handler = (argv) => {
const name = argv.name
const global = argv.global

require('../../lib/addComp')(name, { global })
}
13 changes: 13 additions & 0 deletions packages/tua-mp-cli/commands/addCmds/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 新增页面

exports.desc = 'add a page for your project'
exports.aliases = 'p'
exports.command = 'page <name>'

exports.builder = {}

exports.handler = (argv) => {
const name = argv.name

require('../../lib/addPage')(name)
}
8 changes: 8 additions & 0 deletions packages/tua-mp-cli/commands/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 初始化项目(代理 vue-cli v2 的 init)

exports.desc = 'init project by vue-cli v2'
exports.aliases = 'i'
exports.command = 'init <template> <name>'

exports.builder = {}
exports.handler = () => { require('@vue/cli-init') }
35 changes: 19 additions & 16 deletions packages/tua-mp-cli/lib/addApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
log,
info,
exists,
copyFile,
appendFile,
catchAndThrow,
compileTmplToTarget,
Expand Down Expand Up @@ -43,15 +44,16 @@ const addApi = (name) => {
// 检查父文件夹是否存在
if (!exists(targetDir)) {
return catchAndThrow(
`请检查以下文件夹是否存在!\n` +
`\t- src/apis/\n`
`请检查以下文件夹是否存在!\n\t- src/apis/\n`
)
}

// src
const templateSrc = process.env.TUA_CLI_TEST_SRC ||
const templateDir = process.env.TUA_CLI_TEST_SRC ||
/* istanbul ignore next */
path.resolve(__dirname, '../templates/api.js')
path.resolve(__dirname, '../templates/api/')
const srcIdx = path.join(templateDir, 'index.js')
const srcApi = path.join(templateDir, 'api.js')

// dist
const targetPath = process.env.TUA_CLI_TEST_DIST ||
Expand All @@ -60,30 +62,31 @@ const addApi = (name) => {
const targetIndex = path.join(targetDir, `index.js`)

// 写入 index.js 中的代码
const exportStr = `export ${ccName}Api from './${name}'\n`
const exportStr = `export const ${ccName}Api = tuaApi.getApi(require('./${name}').default)\n`

// 编译模板
const compileParams = {
src: templateSrc,
src: srcApi,
dist: targetPath,
meta: { name },
}

return compileTmplToTarget(compileParams)
.then(({ isCancel, isCover }) => isCancel
const tasks = [ compileTmplToTarget(compileParams) ]

// 复制 index.js
if (!exists(targetIndex)) {
tasks.push(copyFile(srcIdx, targetIndex))
}

return Promise.all(tasks)
.then(([{ isCancel, isCover }]) => isCancel
? info(`取消添加${outputStr}`)
: isCover
// 已有该 api,不重复添加 到 api/index.js 中
? log(
`成功覆盖${outputStr}\n` +
treeLog
)
? log(`成功覆盖${outputStr}\n${treeLog}`)
// 将该 api 添加到 apis/index.js 中
: appendFile(targetIndex, exportStr)
.then(() => log(
`成功添加${outputStr}\n` +
treeLog
))
.then(() => log(`成功添加${outputStr}\n${treeLog}`))
)
.catch(catchAndThrow)
}
Expand Down
Loading

0 comments on commit 878a403

Please sign in to comment.