Skip to content

Commit

Permalink
feat(cli): 增加 taro convert 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Nov 19, 2018
1 parent 1acf465 commit 8d7fb7e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/taro-cli/bin/taro
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ program
.command('init [projectName]', 'Init a project with default templete')
.command('build', 'Build a project with options')
.command('update', 'Update packages of taro')
.command('convert', 'Convert weapp to taro')
.parse(process.argv)
1 change: 0 additions & 1 deletion packages/taro-cli/bin/taro-convert
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const Convertor = require('../src/convertor')
program
.parse(process.argv)


const convertor = new Convertor()

convertor.run()
3 changes: 2 additions & 1 deletion packages/taro-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"author": "O2Team",
"license": "MIT",
"dependencies": {
"@tarojs/transformer-wx": "1.1.9",
"@tarojs/taroize": "1.1.0-beta.10",
"@tarojs/transformer-wx": "1.1.0-beta.10",
"autoprefixer": "^8.4.1",
"babel-core": "^6.26.3",
"babel-generator": "^6.26.1",
Expand Down
11 changes: 0 additions & 11 deletions packages/taro-cli/src/convert.js

This file was deleted.

105 changes: 105 additions & 0 deletions packages/taro-cli/src/convertor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const taroize = require('@tarojs/taroize')

const {
BUILD_TYPES,
MINI_APP_FILES
} = require('./util')

class Convertor {
constructor () {
this.root = process.cwd()
this.convertDir = 'taroConvert'
this.fileTypes = MINI_APP_FILES[BUILD_TYPES.WEAPP]
this.pages = new Set()
this.components = new Set()
this.init()
}

init () {
this.getApp()
this.getPages()
this.getSubPackages()
}

getApp () {
this.entryJSPath = path.join(this.root, 'app.js')
this.entryJSONPath = path.join(this.root, 'app.json')
try {
this.entryJSON = JSON.parse(String(fs.readFileSync(this.entryJSONPath)))
} catch (err) {
this.entryJSON = {}
console.log(chalk.red(`app.json 读取失败,请检查!`))
process.exit(1)
}
}

getPages () {
const pages = this.entryJSON['pages']
if (!pages || !pages.length) {
console.log(chalk.red(`app.json 配置有误,缺少页面相关配置`))
return
}
this.pages = new Set(pages)
}

getSubPackages () {
const subPackages = this.entryJSON['subpackages'] || this.entryJSON['subPackages']
if (!subPackages || !subPackages.length) {
return
}
subPackages.forEach(item => {
if (item.pages && item.pages.length) {
const root = item.root
item.pages.forEach(page => {
let pagePath = `${root}/${page}`
pagePath = pagePath.replace(/\/{2,}/g, '/')
this.pages.add(pagePath)
})
}
})
}

traversePages () {
this.pages.forEach(page => {
const pagePath = path.join(this.root, page)
const pageJSPath = pagePath + this.fileTypes.SCRIPT
const pageConfigPath = pagePath + this.fileTypes.CONFIG
const pageStylePath = pagePath + this.fileTypes.STYLE
const pageTemplPath = pagePath + this.fileTypes.TEMPL
try {
const param = {}
if (fs.existsSync(pageConfigPath)) {
const pageConfigStr = String(fs.readFileSync(pageConfigPath))
const pageConfig = JSON.parse(pageConfigStr)
const pageUsingComponnets = pageConfig.usingComponents
if (pageUsingComponnets) {
// 页面依赖组件
Object.keys(pageUsingComponnets).forEach(component => {
this.components.add(path.resolve(pageConfigPath, '..', pageUsingComponnets[component]))
})
}
param.json = pageConfigStr
}
if (fs.existsSync(pageJSPath)) {
param.script = String(fs.readFileSync(pageJSPath))
}
if (fs.existsSync(pageTemplPath)) {
param.wxml = String(fs.readFileSync(pageTemplPath))
}
const taroizeResult = taroize(param)
console.log(taroizeResult)
} catch (err) {
console.log(err)
}
})
}

run () {
this.traversePages()
}
}

module.exports = Convertor
2 changes: 1 addition & 1 deletion packages/taroize/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/taroize",
"version": "0.0.1",
"version": "1.1.0-beta.10",
"description": "转换原生微信小程序代码为 Taro 代码",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 8d7fb7e

Please sign in to comment.