forked from dcloudio/uni-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
209 lines (185 loc) · 6.61 KB
/
upload.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// console.error('-------- upload.js start--------');
const path = require('path')
const fs = require('fs')
const util = require('../build/util.js')
const buildReadme = require('../build/build-readme.js')
const root = path.join(__dirname,'..')
const modulesId = process.env.UNI_MODULES_ID
// const modulesId = 'uni-test'
const comName = modulesId.replace(/uni-/, '')
const comPath = path.join(root, 'uni_modules')
// console.error('upload.js - modulesId :' + modulesId);
const packageJson = getPackage(modulesId, comPath)
const examplePath = path.join(root, 'temps')
// 同步 readme.md
// md 地址
// const readmePath = path.join(root, 'docs', 'components', comName + '.md')
// const mdExists = fs.existsSync(readmePath)
// if (mdExists) {
// const content = handleReadme(readmePath,comName)
// util.write(path.join(root, 'uni_modules', modulesId, 'readme.md'), content)
// }
let relationComponents = []
// 将示例拷贝到临时目录
const tempExamplePath = path.join(examplePath, 'example_temps')
const exampleExists = fs.existsSync(tempExamplePath)
if (exampleExists) {
// 删除临时目录
util.deleteFolder(tempExamplePath)
}
// 同步临时项目目录
util.copyDir(path.join(examplePath, 'example'), tempExamplePath)
if (modulesId === 'uni-ui') {
// buildReadme()
// util.copyFile(path.join(root, 'README.md'),path.join(root, 'uni_modules', modulesId, 'readme.md'))
// 同步 uni-ui 示例
util.copyDir(comPath, path.join(tempExamplePath, 'uni_modules'))
} else {
// 将组件拷贝到临时目录
util.copyDir(getModulesPath(modulesId), path.join(tempExamplePath, 'uni_modules', modulesId))
handlePageJson(comName, tempExamplePath)
// 同步页面使用的组件
setPageComponents(modulesId, comName)
// 同步组件依赖的组间
syncRelyOn(modulesId)
}
function setPageComponents(modulesId, comName) {
let pagePath = path.join(root, 'pages', 'vue', comName)
let pageVue = path.join(pagePath,comName + '.vue')
let pageNvue = path.join(pagePath,comName + '.nvue')
if(fs.existsSync(pageVue)){
pagePath = pageVue
}else if(fs.existsSync(pageNvue)){
pagePath = pageNvue
}else{
return
}
const pageContent = fs.readFileSync(pagePath).toString()
const pageContents = getComName(pageContent)
if (pageContents.length > 0) {
pageContents.reduce((promise, item) => {
const inputPath = getModulesPath(item)
const exists = fs.existsSync(inputPath)
if (item === modulesId || !exists) return promise
return new Promise((resolve, reject) => {
util.copyDir(inputPath, path.join(tempExamplePath, 'uni_modules', item))
resolve()
}).then(()=>{
syncRelyOn(item)
})
}, Promise.resolve([])).then(res => {
// console.log('所有依赖组件同步完成');
})
}
}
function syncRelyOn(modulesName){
const packageJson = getPackage(modulesName, comPath)
let relationComponents = []
// 获取关联组件
if (packageJson && packageJson.uni_modules && packageJson.uni_modules.dependencies.length > 0) {
relationComponents = packageJson.uni_modules.dependencies
}
// 同步依赖组件
if (relationComponents && relationComponents.length > 0) {
return relationComponents.reduce((promise, item) => {
return new Promise((resolve, reject) => {
util.copyDir(getModulesPath(item), path.join(tempExamplePath, 'uni_modules', item))
resolve()
}).then(()=>{
syncRelyOn(item)
})
}, Promise.resolve([])).then(res => {
// console.error('所有依赖组件同步完成');
// setPageComponents(modulesId, comName)
}).catch((err)=>{
// console.log('error',err);
})
}
return Promise.resolve()
}
/**
* 获取当前组件 package.json
* @param {Object} modulesId
*/
function getPackage(modulesId, url) {
const comPath = path.join(url, modulesId, 'package.json')
// 获取所有组件
const exists = fs.existsSync(comPath)
if (!exists) return {}
return JSON.parse(fs.readFileSync(comPath, 'utf8'))
}
/**
* 获取页面依赖组件信息
* @param {Object} dataFile
*/
function getComName(dataFile) {
let uniNameArray = dataFile.match(/<uni-\S+?(?=\s|\/?>)/ig)
let newUniNameArray = []
if (!uniNameArray) return []
uniNameArray.forEach(value => {
value = value.replace(/>|</ig, '')
if (!~newUniNameArray.indexOf(value)) {
newUniNameArray.push(value)
}
})
return newUniNameArray
}
function handlePageJson(comName, tempExamplePath) {
// 处理 pages.json 文件
const uniUiPagesJson = path.join(root, 'pages.json')
let pageJsonData = util.read(uniUiPagesJson)
// 避免unicode转为实体字符
pageJsonData = pageJsonData.replace(/\\u/, '\\\\u')
const pagesJson = JSON.parse(pageJsonData)
let examplePages = pagesJson.pages
let jsonData = examplePages.find(item => {
return item.path === `pages/vue/${comName}/${comName}`
})
let pagePaths = []
if (jsonData) {
jsonData.path = `pages/${comName}/${comName}`
pagePaths.push(jsonData)
}
let pageJson = JSON.stringify({
'pages': pagePaths,
'globalStyle': {
'navigationBarTextStyle': 'black',
'navigationBarTitleText': 'uni-app',
'navigationBarBackgroundColor': '#F8F8F8',
'backgroundColor': '#F8F8F8'
}
}, '', 2)
// 转换回对应的字符
pageJson = pageJson.replace(/\\\\u/, '\\u')
// 同步 json 文件
util.write(path.join(tempExamplePath, 'pages.json'), pageJson)
const outPath = path.join(tempExamplePath, 'pages')
const exists = fs.existsSync(outPath)
if (exists) {
util.deleteFolder(outPath)
}
util.copyDir(path.join(root, 'pages', 'vue', comName), path.join(tempExamplePath, 'pages', comName))
util.copyDir(path.join(root, 'common'), path.join(tempExamplePath, 'common'))
}
function getModulesPath(name) {
return path.join(comPath, name)
}
/**
* 处理 readme.md
* @param {Object} readmePath
*/
function handleReadme(readmePath,comName) {
let content = util.read(readmePath)
// 兼容 windows ,将 \r\n 全部替换成 \n
content = content.replace(/\r\n/ig, '\n')
// 删除头部额外信息,在其他平台不支持,只在 uni ui 中支持
content = content.replace(/---([\s\S]*?)---/ig, '').replace(/## 图标示例([\s\S]*?)\<\/icons-layouts\>/ig, '')
// 转换 ::: 语法
content = content.replace(/::: (.*?)\n([\s\S]*?):::/ig, function(_, $1, $2) {
$1 = $1.replace(/(tip|danger|warning)+ /,'')
return '> **'+ $1 +'**\n'+ $2.split('\n').filter(item => item !== '').map(item => `> ${item}\n`).join('')
})
content += `\n\n## 组件示例\n\n点击查看:[https://hellouniapp.dcloud.net.cn/pages/extUI/${comName}/${comName}](https://hellouniapp.dcloud.net.cn/pages/extUI/${comName}/${comName})`
return content
}
// console.error('-------- upload.js end --------');