UNPKG

1.75 kBtext/coffeescriptView Raw
1Promise = require('bluebird')
2FS = require('fs')
3
4Base = require('../base')
5Caller = require('./caller')
6
7
8debug = Base.logger('nodejs_exports_loader')
9
10
11exports.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$/) # Don't load auto-save files.
24 fexports = require(path) # Supports files and module directories.
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
41metaForObj = (obj) ->
42 if Base.utils.isFobject(obj)
43 info = { isFunc: Base.utils.isFunction(obj) } # Top-level function.
44 for own attName, att of obj
45 info.atts ||= {}
46 metaInfo = metaForObj(att)
47 info.atts[attName] = metaInfo
48 else # Non-object property.
49 info = { value: obj }
50 # TODO: How to tell if it needs to be invoked with a callback? sync or async?
51 info