forked from yodaos-project/yoda.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-api-json.js
52 lines (46 loc) · 1.36 KB
/
generate-api-json.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
var fs = require('fs')
var path = require('path')
var _ = require('../packages/@yoda/util/_')
var nodeRequire = require
var descriptorFiles = fs.readdirSync(path.join(__dirname, '../runtime/descriptor'))
var api = descriptorFiles
.filter(it => !it.endsWith('descriptor.js'))
.map(it => ({
name: path.basename(it, '.js'),
descriptor: generate(it)
}))
.reduce((accu, it) => {
if (it.name === 'activity') {
Object.assign(accu, it.descriptor)
return accu
}
accu.namespaces[_.camelCase(it.name)] = it.descriptor
return accu
}, { namespaces: {} })
fs.writeFileSync(path.join(__dirname, '../runtime/client/api/default.json'), JSON.stringify(api, null, 2))
function generate (filename) {
var dirname = path.join(__dirname, '../runtime/descriptor')
var module = { exports: {} } //eslint-disable-line
var require = (id) => { //eslint-disable-line
if (id === 'logger') {
return function () {}
}
var ret = function () {}
if (id.startsWith('.')) {
id = path.join(dirname, id)
}
try {
ret = nodeRequire(id)
} catch (err) {
}
return ret
}
var file = fs.readFileSync(path.join(dirname, filename))
var content = file.toString()
eval(content) //eslint-disable-line
return {
values: module.exports.values,
events: module.exports.events,
methods: module.exports.methods
}
}