-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathgenHarmonyExtApiExport.js
69 lines (66 loc) · 2.18 KB
/
genHarmonyExtApiExport.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
const fs = require('fs')
const path = require('path')
const parser = require("@babel/parser")
const BLACKLIST = [
'UniExtApi',
'initUniExtApi'
]
function initAutoImportMap (isUniAppX = false) {
const harmonyDistDir = path.resolve(__dirname, '../packages/uni-app-harmony', isUniAppX ? 'dist-x' : 'dist')
const autoImportMap = isUniAppX ? {
[path.resolve(harmonyDistDir, 'uni.api.ets')]: '@dcloudio/uni-app-x-runtime'
} : {
[path.resolve(harmonyDistDir, 'uni.api.ets')]: '@dcloudio/uni-app-runtime'
}
const ohpmPageckageDir = path.resolve(harmonyDistDir, 'packages')
if (!fs.existsSync(ohpmPageckageDir)) {
return autoImportMap
}
const packages = fs.readdirSync(ohpmPageckageDir)
packages.forEach(package => {
const packageEntryFilePath = path.resolve(ohpmPageckageDir, package, 'utssdk/app-harmony/index.ets')
if (!fs.existsSync(packageEntryFilePath)) {
return
}
autoImportMap[packageEntryFilePath] = `@uni_modules/${package.toLowerCase()}`
})
return autoImportMap
}
function getExportList (filePath) {
const file = fs.readFileSync(filePath, 'utf-8')
const ast = parser.parse(file, {
sourceType: 'module',
plugins: ['typescript'],
})
const exportNames = []
ast.program.body.forEach(node => {
if (node.type === 'ExportNamedDeclaration') {
if (node.specifiers && node.specifiers.length) {
node.specifiers.forEach(specifier => {
if (specifier.exported) {
const exportName = specifier.exported.name
if (!BLACKLIST.includes(exportName)) {
exportNames.push([exportName])
}
}
})
} else if (node.declaration) {
const exportName = node.declaration.id.name
if (!BLACKLIST.includes(exportName)) {
exportNames.push([exportName])
}
}
}
})
return exportNames
}
exports.genHarmonyExtApiExport = function (isUniAppX = false) {
const autoImportMap = initAutoImportMap(isUniAppX)
const result = {}
for (const fileName in autoImportMap) {
const packageName = autoImportMap[fileName]
result[packageName] = result[packageName] || []
result[packageName].push(...getExportList(fileName))
}
return result
}