UNPKG

932 BJavaScriptView Raw
1const fs = require('fs')
2const path = require('path')
3const { findModuleDir, findHandler } = require('./finders')
4
5module.exports = {
6 getFunctions(dir) {
7 const functions = {}
8 if (fs.existsSync(dir)) {
9 fs.readdirSync(dir).forEach(file => {
10 if (dir === 'node_modules') {
11 return
12 }
13 const functionPath = path.resolve(path.join(dir, file))
14 const handlerPath = findHandler(functionPath)
15 if (!handlerPath) {
16 return
17 }
18 if (path.extname(functionPath) === '.js') {
19 functions[file.replace(/\.js$/, '')] = {
20 functionPath,
21 moduleDir: findModuleDir(functionPath)
22 }
23 } else if (fs.lstatSync(functionPath).isDirectory()) {
24 functions[file] = {
25 functionPath: handlerPath,
26 moduleDir: findModuleDir(functionPath)
27 }
28 }
29 })
30 }
31 return functions
32 }
33}