Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
73 lines (58 loc) · 2.26 KB
/
Copy pathindex.js
File metadata and controls
73 lines (58 loc) · 2.26 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 _ = require('lodash');
const fs = require('fs');
const path = require('path');
const del = require('del');
const wrap = require('./wrap-functions');
class WrapLog {
constructor(serverless, options) {
this.sls = serverless;
this.options = options;
this.sufixFile = '-wrap-log';
this.rgxModule = /(module[\w\s\.=]*{\s*)(\w*)(\s*};)/g;
this.hooks = {
'before:package:createDeploymentArtifacts': this.run.bind(this),
'before:deploy:function:packageFunction': this.run.bind(this),
'before:invoke:local:invoke': this.run.bind(this),
'before:offline:start:init': this.run.bind(this),
'before:step-functions-offline:start': this.run.bind(this),
'after:package:createDeploymentArtifacts': this.finish.bind(this),
'after:invoke:local:invoke': this.finish.bind(this),
};
}
log(arg1, ...rest) {
const logger = this.sls.cli.log || console.log;
logger.call(this.sls.cli, `serverless-wrap-log: ${arg1}`, ...rest);
}
run() {
this.log('Wrapping your functions with Log...');
_.mapKeys( this.sls.service.functions, ({ handler, skipWrap = false }, key) => {
try{
if (skipWrap) return;
const pathArr = handler.split('.');
const pathFile = pathArr.slice(0, -1).join('.');
const nameFunction = _.last(pathArr);
this.buildFile(pathFile);
this.sls.service.functions[key].handler = pathFile + this.sufixFile + '.' + nameFunction;
} catch(e) {
this.log(`Error wrapping function: ${ handler } `);
}
});
}
buildFile(pathFile) {
const file = String(fs.readFileSync(path.join(this.sls.config.servicePath, pathFile + '.js')));
const funcWrapped = `${wrap}\n\n${this.wrapExport(file)}`;
fs.writeFileSync(path.join(this.sls.config.servicePath, pathFile + this.sufixFile +'.js'), funcWrapped);
}
wrapExport(file) {
return file.replace(this.rgxModule, (all, head, funcs, last) => {
const wrapFuncs = funcs.split(',').map(funcName => `${funcName}: WrapFunction(${funcName})`).join(',');
return `${head}${wrapFuncs}${last}`;
});
}
finish() {
del.sync(`**/*${this.sufixFile}.js`, {root: this.sls.config.servicePath });
this.log('Cleaning up extraneous Wrap files');
}
}
module.exports = WrapLog;