Skip to content

Commit

Permalink
Merge branch 'feat_cli_template'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Jul 10, 2019
2 parents 297841e + c227ab6 commit 710b37a
Show file tree
Hide file tree
Showing 107 changed files with 628 additions and 3,222 deletions.
1 change: 1 addition & 0 deletions packages/taro-cli/bin/taro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ program
.version(getPkgVersion())
.usage('<command> [options]')
.command('init [projectName]', 'Init a project with default templete')
.command('config <cmd>', 'Taro config')
.command('create [type]', 'Create page or component for project')
.command('build', 'Build a project with options')
.command('update', 'Update packages of taro')
Expand Down
42 changes: 42 additions & 0 deletions packages/taro-cli/bin/taro-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env node

const program = require('commander')
const helper = require('../dist/taro-config')

program
.option('--json', '以 JSON 形式输出')
.on('--help', function () {
console.log('')
console.log('Synopsis:')
console.log(' $ taro config set <key> <value>')
console.log(' $ taro config get <key>')
console.log(' $ taro config delete <key>')
console.log(' $ taro config list [--json]')
})
.parse(process.argv)

const args = program.args
const { json } = program

const [cmd, key, value] = args

switch (cmd) {
case 'get':
if (!key) return
helper.get(key)
break
case 'set':
if (!key || !value) return
helper.set(key, value)
break
case 'delete':
if (!key) return
helper.deleteKey(key)
break
case 'list':
case 'ls':
helper.list(json)
break
default:
break
}
3 changes: 1 addition & 2 deletions packages/taro-cli/bin/taro-create
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ const type = args[0] || 'page'
const resultName = args[1] || name
if (type === 'page') {
const page = new Page({
name: resultName,
pageName: resultName,
projectDir: process.cwd(),
description
})

page.create()
}

6 changes: 4 additions & 2 deletions packages/taro-cli/bin/taro-init
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ program
.option('--description [description]', '项目介绍')
.option('--typescript', '使用TypeScript')
.option('--no-typescript', '不使用TypeScript')
.option('--template [template]', '项目模板(default/redux/mobx/wxcloud/wxplugin)')
.option('--template-source [templateSource]', '项目模板源')
.option('--template [template]', '项目模板')
.option('--css [css]', 'CSS预处理器(sass/less/stylus/none)')
.parse(process.argv)

const args = program.args
const { template, description, name, css } = program
const { template, templateSource, description, name, css } = program
let typescript

/**
Expand All @@ -32,6 +33,7 @@ const projectName = args[0] || name
const project = new Project({
projectName,
projectDir: process.cwd(),
templateSource,
template,
description,
typescript,
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"@tarojs/taroize": "1.3.8",
"@tarojs/transformer-wx": "1.3.8",
"@types/request": "^2.48.1",
"adm-zip": "^0.4.13",
"autoprefixer": "^8.4.1",
"axios": "^0.19.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-generator": "^6.26.1",
Expand Down
5 changes: 3 additions & 2 deletions packages/taro-cli/src/create/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export default class Creator {
protected _rootPath: string
private _destinationRoot: string

constructor () {
constructor (sourceRoot?: string) {
const store = memFs.create()
this.fs = editor.create(store)
this.sourceRoot(path.join(getRootPath()))
this.sourceRoot(sourceRoot || path.join(getRootPath()))
this.init()
}

Expand All @@ -65,6 +65,7 @@ export default class Creator {
if (!fs.existsSync(this._rootPath)) {
fs.ensureDirSync(this._rootPath)
}
console.log('this._rootPath: ', this._rootPath)
return this._rootPath
}

Expand Down
101 changes: 101 additions & 0 deletions packages/taro-cli/src/create/fetchTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as fs from 'fs-extra'
import * as path from 'path'
import { exec } from 'child_process'
import chalk from 'chalk'
import * as ora from 'ora'
import axios from 'axios'
import * as AdmZip from 'adm-zip'
import Project from './project'
import { TemplateSourceType } from '../util'

const TEMP_DOWNLOAD_FLODER = 'taro-temp'

export default function fetchTemplate (creater: Project, type: TemplateSourceType): Promise<any> {
const { templateSource } = creater.conf
const templateRootPath = creater.templatePath('')
const tempPath = path.join(templateRootPath, TEMP_DOWNLOAD_FLODER)
let name: string

return new Promise(async (resolve, reject) => {
// 下载文件的缓存目录
if (fs.existsSync(tempPath)) await fs.remove(tempPath)
await fs.mkdir(tempPath)

const spinner = ora(`正在从 ${templateSource} 拉取远程模板...`).start()

if (type === 'git') {
// git clone
name = path.basename(templateSource, '.git')
exec(`git clone ${templateSource} ${path.join(tempPath, name)}`, async error => {
if (error) {
spinner.color = 'red'
spinner.fail(chalk.red('拉取远程模板仓库失败!'))
await fs.remove(tempPath)
return resolve()
}
spinner.color = 'green'
spinner.succeed(`${chalk.grey('拉取远程模板仓库成功!')}`)
resolve()
})
} else if (type === 'url') {
axios.get(templateSource, {
responseType: 'stream'
})
.then(response => {
const zipPath = path.join(tempPath, 'temp.zip')
const ws = fs.createWriteStream(zipPath)
response.data.pipe(ws)
ws.on('close', () => {
// unzip
const zip = new AdmZip(zipPath)
zip.extractAllTo(tempPath, true)
const files = fs.readdirSync(tempPath, { withFileTypes: true })
.filter((file: fs.Dirent) => !file.name.startsWith('.') && file.isDirectory())
if (files.length !== 1) {
spinner.color = 'red'
spinner.fail(chalk.red(`拉取远程模板仓库失败!\n${new Error('远程模板源组织格式错误')}`))
return resolve()
}

name = files[0].name
spinner.color = 'green'
spinner.succeed(`${chalk.grey('拉取远程模板仓库成功!')}`)
resolve()
})
})
.catch(async err => {
spinner.color = 'red'
spinner.fail(chalk.red(`拉取远程模板仓库失败!\n${err}`))
await fs.remove(tempPath)
return resolve()
})
}
})
.then(async () => {
const templateFloder = name ? path.join(tempPath, name) : ''

// 下载失败,只显示默认模板
if (!fs.existsSync(templateFloder)) return Promise.resolve([])

const isTemplateGroup = !fs.existsSync(path.join(templateFloder, 'package.json'))

if (isTemplateGroup) {
// 模板组
const files: string[] = fs.readdirSync(templateFloder, { withFileTypes: true })
.filter((file: fs.Dirent) => !file.name.startsWith('.') && file.isDirectory())
.map((file: fs.Dirent) => file.name)
await Promise.all(files.map(file => {
const src = path.join(templateFloder, file)
const dest = path.join(templateRootPath, file)
return fs.move(src, dest, { overwrite: true })
}))
await fs.remove(tempPath)
return Promise.resolve(files)
} else {
// 单模板
await fs.move(templateFloder, path.join(templateRootPath, name), { overwrite: true })
await fs.remove(tempPath)
return Promise.resolve([name])
}
})
}
Loading

0 comments on commit 710b37a

Please sign in to comment.