1 | const fs = require('fs')
|
2 | const path = require('path')
|
3 | const { findModuleDir, findHandler } = require('./finders')
|
4 |
|
5 | module.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 | }
|