-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathgen-dynamic.js
More file actions
106 lines (89 loc) · 3.33 KB
/
Copy pathgen-dynamic.js
File metadata and controls
106 lines (89 loc) · 3.33 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
/**
* Scans all exported methods from src/platform/api and generates
* src/common/js/dynamic.js with lazy-loading require() wrappers.
*
* Run: node scripts/gen-dynamic.js
*/
const fs = require('fs')
const path = require('path')
const srcDir = path.resolve(__dirname, '../src')
const platformIndexPath = path.join(srcDir, 'platform/index.js')
const outputPath = path.join(srcDir, 'common/js/dynamic.js')
const commonJsDir = path.join(srcDir, 'common/js')
if (!fs.existsSync(platformIndexPath)) {
console.error('platform/index.js not found:', platformIndexPath)
process.exit(1)
}
const platformContent = fs.readFileSync(platformIndexPath, 'utf-8')
// Extract all "export * from './api/xxx'" paths
const exportMatches = [...platformContent.matchAll(/export \* from '(.+?)'/g)]
if (exportMatches.length === 0) {
console.error('No export * from entries found in platform/index.js')
process.exit(1)
}
// name -> requirePath (relative to common/js/)
const apiEntries = []
const seenNames = new Set()
for (const [, relPath] of exportMatches) {
// relPath is like './api/system' or './api/device/network'
const apiDir = path.join(srcDir, 'platform', relPath)
let indexFilePath = path.join(apiDir, 'index.js')
if (!fs.existsSync(indexFilePath)) {
// fallback: relPath might point directly to a .js file
indexFilePath = apiDir + '.js'
if (!fs.existsSync(indexFilePath)) {
console.warn(' [skip] index.js not found for:', relPath)
continue
}
}
const content = fs.readFileSync(indexFilePath, 'utf-8')
// Match all export { name1, name2, ... } blocks (may span multiple lines)
const exportBlockRegex = /export\s*\{([^}]+)\}/g
let blockMatch
while ((blockMatch = exportBlockRegex.exec(content)) !== null) {
const names = blockMatch[1]
.split(',')
.map(n => {
// Handle "localName as exportedName" — take the exported name
const parts = n.trim().split(/\s+as\s+/)
return (parts[1] || parts[0]).trim()
})
.filter(n => n && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(n))
// require path: relative from common/js/ to the api dir's index (no .js extension)
const requireTarget = path.join(apiDir, 'index')
let requirePath = path.relative(commonJsDir, requireTarget).replace(/\\/g, '/')
if (!requirePath.startsWith('.')) requirePath = './' + requirePath
for (const name of names) {
if (seenNames.has(name)) {
console.warn(' [dup] skipping duplicate export:', name)
continue
}
seenNames.add(name)
apiEntries.push({ name, requirePath })
}
}
}
if (apiEntries.length === 0) {
console.error('No exported names found — aborting to avoid writing an empty file')
process.exit(1)
}
// Build dynamic.js content
const methodLines = apiEntries.map(({ name, requirePath }, i) => {
const comma = i < apiEntries.length - 1 ? ',' : ''
return [
` ${name} (...args) {`,
` const ${name} = require('${requirePath}').${name}`,
` return ${name}(...args)`,
` }${comma}`
].join('\n')
})
const output = [
'// This file is auto-generated by scripts/gen-dynamic.js — do not edit manually',
'export default {',
methodLines.join('\n'),
'}',
''
].join('\n')
fs.writeFileSync(outputPath, output, 'utf-8')
console.log(`Generated ${path.relative(process.cwd(), outputPath)} with ${apiEntries.length} methods`)