1 | Promise = require('bluebird')
|
2 | FS = require('fs')
|
3 |
|
4 | Base = require('../base')
|
5 | Caller = require('./caller')
|
6 |
|
7 |
|
8 | debug = Base.logger('nodejs_exports_loader')
|
9 |
|
10 |
|
11 | exports.loadIxExports = (ixApiDir) ->
|
12 | exportInfo = {}
|
13 | exportedProps = {}
|
14 | ixDirname = ixApiDir || Base.env.IX_API_DIR || 'faas-ix'
|
15 | ixDir = process.cwd()+'/'+ixDirname
|
16 | Promise.fromCallback (callback) ->
|
17 | FS.readdir(ixDir, callback)
|
18 | .then (files) ->
|
19 | debug('readdir', files)
|
20 | for file in files
|
21 | path = ixDir+'/'+file
|
22 | try
|
23 | if file.match(/\w$/)
|
24 | fexports = require(path)
|
25 | debug('file', file, fexports)
|
26 | modName = file
|
27 | modName = modName.replace(/\.(js|json|node)$/i, '')
|
28 | modName = modName.replace(/\W/g, '_')
|
29 | meta = exportInfo[modName]
|
30 | if exportInfo[modName]
|
31 | console.log("Multiple definitions found for module '"+modName+"', only using definitions from file '"+meta.file+"' (discarding '"+file+"').")
|
32 | else
|
33 | exportInfo[modName] = { file: file, exports: metaForObj(fexports) }
|
34 | exportedProps[modName] = fexports
|
35 | catch e
|
36 | console.log('Unable to load module '+path+':', e)
|
37 | debug('exportInfo', JSON.stringify(exportInfo))
|
38 | Caller.setNodejsExports(exportedProps)
|
39 | exportInfo
|
40 |
|
41 | metaForObj = (obj) ->
|
42 | if Base.utils.isFobject(obj)
|
43 | info = { isFunc: Base.utils.isFunction(obj) }
|
44 | for own attName, att of obj
|
45 | info.atts ||= {}
|
46 | metaInfo = metaForObj(att)
|
47 | info.atts[attName] = metaInfo
|
48 | else
|
49 | info = { value: obj }
|
50 |
|
51 | info
|