-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
135 lines (131 loc) · 4.27 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @file 主函数
*/
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const config = require('./lib')
const util = require('./lib/common/util')
const excludeDir = ['convertDist', 'node_modules']
class Convert {
constructor (platform) {
process.env.TARGETPLATFORM = platform
process.env.SRCPLATFORM = 'wx'
this.tarConfig = config[platform]
this.srcConfig = {}
this.root = process.cwd()
this.convertRoot = path.join(this.root, 'convertDist', platform)
this.components = new Set()
this.templSet = new Set()
this.styleSet = new Set()
this.scriptSet = new Set()
this.jsonSet = new Set()
this.excludeDir = excludeDir.map(dir => {
return path.join(this.root, dir)
})
this.init()
}
init () {
this.clearDistDir()
this.getApp()
}
clearDistDir () {
}
getApp () {
this.entryJSONPath = path.join(this.root, 'app.json')
try {
this.entryJSON = JSON.parse(fs.readFileSync(this.entryJSONPath))
let pages = this.entryJSON['pages']
if (!pages || !pages.length) {
throw new Error('invalid pages config...')
}
process.env.SRCPLATFORM = util.getSourcePlatform(this.root, pages)
this.pages = new Set(pages)
this.srcConfig = config[process.env.SRCPLATFORM]
this.entryJSPath = path.join(this.root, `app.${this.srcConfig.fileTypes.SCRIPT}`)
this.entryStylePath = path.join(this.root, `app.${this.srcConfig.fileTypes.STYLE}`)
if (fs.existsSync(this.entryStylePath)) {
this.entryStyle = String(fs.readFileSync(this.entryStylePath))
}
} catch(e) {
console.log(e)
this.entryJSON = {}
process.exit(1)
}
}
getSubPackages () {
}
traverseAndConvert(filePath, platform) {
let files = fs.readdirSync(filePath)
files.forEach(filename => {
let absFilePath = path.join(filePath, filename)
let stats = fs.statSync(absFilePath)
let isFile = stats.isFile()
let isDir = stats.isDirectory()
if (this.excludeDir.includes(absFilePath)) {
return
}
if (isFile) {
let ext = path.extname(absFilePath) && path.extname(absFilePath).substr(1)
let extReg = new RegExp('(\\.)(' + ext + ')$', 'g')
let relativePath = path.relative(this.root, absFilePath)
if (ext && util.PLATFORMMAPS[ext]) {
this.templSet.add(relativePath.replace(extReg, ''))
return
}
if (ext && util.STYLESMAPS[ext]) {
this.styleSet.add(relativePath.replace(extReg, ''))
return
}
if (ext && ext === 'js') {
this.scriptSet.add(relativePath.replace(extReg, ''))
return
}
if (ext && ext === 'json' && relativePath !== 'app.json') {
this.jsonSet.add(relativePath.replace(extReg, ''))
return
}
if (relativePath === 'app.json'
&& (process.env.SRCPLATFORM === 'my'
|| process.env.TARGETPLATFORM === 'my')
) {
return
}
let distPath = path.join(this.root, 'convertDist', process.env.TARGETPLATFORM, relativePath)
fs.ensureDirSync(path.dirname(distPath))
fs.createReadStream(absFilePath).pipe(fs.createWriteStream(distPath))
}
if (isDir) {
this.traverseAndConvert(absFilePath, platform)
}
})
}
run () {
if (process.env.SRCPLATFORM === process.env.TARGETPLATFORM) {
console.log(chalk.red('目标平台与当前平台相同,请选择其它目标平台...'))
return
}
console.log(chalk.green('开始遍历文件...'))
this.traverseAndConvert(this.root, process.env.SRCPLATFORM)
const baseOptions = {
root: this.root,
srcConfig: this.srcConfig,
tarConfig: this.tarConfig
}
console.log(chalk.green('开始转换文件...'))
util.templConvert(Object.assign(baseOptions, {
pages: this.templSet
}))
util.styleConvert(Object.assign(baseOptions, {
pages: this.styleSet
}))
util.scriptConvert(Object.assign(baseOptions, {
pages: this.scriptSet
}))
util.jsonConvert(Object.assign(baseOptions, {
pages: this.jsonSet,
entryJSON: (process.env.SRCPLATFORM === 'my' || process.env.TARGETPLATFORM === 'my') ? this.entryJSON : ''
}))
}
}
module.exports = Convert;