-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
86 lines (75 loc) · 2.94 KB
/
parse.js
File metadata and controls
86 lines (75 loc) · 2.94 KB
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
import { sep } from 'node:path'
import { parse as parseAST } from '@babel/parser'
import { compareString } from '@dr-js/core/module/common/compare.js'
import { objectSortKey } from '@dr-js/core/module/common/mutable/Object.js'
import { getPathStat, getPathTypeFromStat } from '@dr-js/core/module/node/fs/Path.js'
import { readTextSync } from '@dr-js/core/module/node/fs/File.js'
const getExportListFromParsedAST = (fileString, sourceFilename, parserPluginList) => {
const resultAST = parseAST(fileString, {
sourceFilename,
sourceType: 'module',
plugins: parserPluginList || [
'objectRestSpread',
'classProperties',
'exportDefaultFrom',
'exportNamespaceFrom',
'jsx'
]
})
const exportNodeList = resultAST.program.body.filter(({ type }) => type === 'ExportNamedDeclaration')
return [].concat(...exportNodeList.map(({ specifiers, declaration }) => !declaration ? specifiers.map(({ exported: { name } }) => name)
: declaration.declarations ? declaration.declarations.map(({ id: { name } }) => name)
: [ declaration.id.name ]
))
}
const sortSourceRouteMap = (sourceRouteMap) => {
Object.values(sourceRouteMap).forEach(({ routeList, directoryList, fileList }) => {
directoryList.sort(compareString)
fileList.sort(({ name: a }, { name: b }) => compareString(a, b))
fileList.forEach(({ exportList }) => exportList.sort(compareString))
})
objectSortKey(sourceRouteMap)
return sourceRouteMap
}
const createExportParser = ({
parserPluginList,
kit, kitLogger = kit
}) => {
let sourceRouteMap = {
// 'source/route': {
// routeList: [ 'source' ],
// directoryList: [ /* name */ ],
// fileList: [ /* { name, exportList: [ name ] } */ ]
// }
}
const getRoute = (routeList) => {
const key = routeList.join('/')
if (!sourceRouteMap[ key ]) sourceRouteMap[ key ] = { routeList, directoryList: [], fileList: [] }
return sourceRouteMap[ key ]
}
const parseExport = async (path) => {
const fileStat = await getPathStat(path)
const routeList = path.split(sep)
const name = routeList.pop()
if (fileStat.isDirectory()) {
kitLogger.devLog(`[directory] ${path}`)
getRoute(routeList).directoryList.push(name)
} else if (fileStat.isFile() && name.endsWith('.js')) {
const fileString = readTextSync(path)
const exportList = getExportListFromParsedAST(fileString, path, parserPluginList)
kitLogger.devLog(`[file] ${path}`)
if (!exportList.length) return
getRoute(routeList).fileList.push({ name: name.slice(0, -3), exportList }) // remove `.js` from name
kitLogger.devLog(` export [${exportList.length}]: ${exportList.join(', ')}`)
} else kitLogger.devLog(`[skipped] ${path} (${getPathTypeFromStat(fileStat)})`)
}
return {
parseExport,
getSourceRouteMap: () => {
const result = sortSourceRouteMap(sourceRouteMap)
sourceRouteMap = {}
return result
}
}
}
export { createExportParser }