-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (55 loc) · 1.51 KB
/
index.js
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
'use strict';
const _ = {
get: require('lodash.get'),
set: require('lodash.set')
};
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
class PrintResolved {
constructor(serverless) {
this.provider = serverless.getProvider('aws');
this.serverless = serverless.service;
this.hooks = {
'after:package:finalize': this.writeResolved.bind(this)
};
}
getOriginal() {
const filePath = path.join(
this.serverless.serverless.config.servicePath,
'serverless.yml'
);
const stringified = fs.readFileSync(filePath, 'utf-8');
return yaml.safeLoad(stringified);
}
writeResolved() {
const { paths } = _.get(this.serverless, 'custom.print-resolved', {});
const keysToInclude = new Set(
Object.keys(
this.getOriginal()
)
);
const base = Object.entries(this.serverless)
.filter(([key])=> keysToInclude.has(key))
.reduce(
(acc, [key, value]) => Object.assign(acc, { [key]: value }),
{}
);
const merged = paths.reduce(
(acc, path) => {
const value = _.get(this.serverless, path);
_.set(acc, path, value);
return acc;
},
base
);
const stringified = yaml.safeDump(merged, { skipInvalid: true });
const filePath = path.join(
this.serverless.serverless.config.servicePath,
'.serverless',
'serverless-resolved.yml'
);
fs.writeFileSync(filePath, stringified, 'utf-8');
}
}
module.exports = PrintResolved;