This repository was archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (60 loc) · 1.62 KB
/
Copy pathindex.js
File metadata and controls
73 lines (60 loc) · 1.62 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict'
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')
const flattenDeep = require('lodash.flattendeep')
const stat = promisify(fs.stat)
const mkdir = promisify(fs.mkdir)
const writeFile = promisify(fs.writeFile)
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless
this.options = options
this.path = './.ahaless'
if (this.options.gf) {
this.gfHandler().catch(err => {
this.serverless.cli.log(err.message)
})
}
}
async gfHandler() {
try {
this.serverless.cli.log('Generating functions ...')
require('ts-node').register({ module: 'commonjs' })
require('tsconfig-paths').register()
const servicePath = path.join(this.serverless.config.servicePath, '/handler.ts')
const metadata = flattenDeep(require(servicePath).metadata)
const result = metadata.reduce((acc, value) => {
const path = value.path ? value.path : value.fnName
acc[path] = {
handler: `handler.${value.fnName}`,
events: [
{
http: {
method: value.method,
path
}
}
]
}
return acc
}, {})
const data = JSON.stringify(result, null, 2)
const stats = await stat(this.path).catch(e => {
this.serverless.cli.log(e.message)
this.serverless.cli.log('Directory not found. Create one')
})
if (!stats) {
await mkdir(this.path)
}
await writeFile(
`${this.path}/functions.sls.json`,
data
)
this.serverless.cli.log('Generate functions successfully!')
} catch (e) {
this.serverless.cli.log(e.message)
}
}
}
module.exports = ServerlessPlugin