-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprint.js
146 lines (119 loc) · 4.64 KB
/
print.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
const chalk = require('chalk');
const YAML = require('js-yaml');
const { diff } = require('deep-diff');
class Printer {
constructor(serverless) {
this.serverless = serverless;
}
printStackPolicy(stackName, stackPolicy) {
this.serverless.cli.log(`[serverless-plugin-bootstrap] Updating the stack policy for stack '${stackName}' to:`);
this.printObj(chalk.white, stackPolicy);
}
printChanges(stackName, changeSetName, changes) {
this.serverless.cli.log(`[serverless-plugin-bootstrap] The stack '${stackName}' differs from your local infrastructure code.`);
this.serverless.cli.log(`[serverless-plugin-bootstrap] Make sure your code is up to date and review the following changes.`);
const { Parameter, Condition, Resource, Output } = changes.reduce((memo, change) => {
memo[change.Type] = memo[change.Type] || [];
memo[change.Type].push(change);
return memo;
}, {});
this.printSection(Parameter, 'Parameter');
this.printSection(Condition, 'Condition');
this.printSection(Resource, 'Resource');
this.printSection(Output, 'Output');
this.print(chalk.white, '');
this.serverless.cli.log(`[serverless-plugin-bootstrap] If the above changes look appropriate, you can run 'serverless bootstrap execute --change-set ${changeSetName}'`);
}
printSection(values = [], type) {
if (values.length === 0) {
return;
}
const title = `${chalk.dim('>>')} ${chalk.cyan(type.toUpperCase() + 'S')}`;
const field = `${type}Change`;
const label = `${type}Key`;
this.print(chalk.white, '');
this.print(chalk.white, title);
values.forEach(item => {
const change = item[field];
const { Action, From, To, References = [] } = change;
const key = change[label];
this.print(chalk.white, '');
switch (Action) {
case 'Add':
this.print(chalk.green, `+ ${key}`);
this.printObj(chalk.green, To, 6);
if (change.Template) {
this.print(chalk.dim, '<Template Content>', 6);
this.printObj(chalk.green, change.Template.To, 8);
}
break;
case 'Remove':
this.print(chalk.red, `- ${key}`);
this.printObj(chalk.red, From, 4);
break;
case 'Modify':
this.print(chalk.yellow, `~ ${key}`);
if (References.length > 0) {
this.print(chalk.white, 'References changed:', 4);
References.forEach(reference => {
const key = Object.keys(reference)[0];
const value = reference[key];
this.print(chalk.white, '↪ ' + chalk.yellow(value) + ` (${key})`, 6);
});
}
this.printDiff(From, To, 4);
if (change.Template) {
this.print(chalk.dim, '<Template Content>', 6);
this.printDiff(change.Template.From, change.Template.To, 8);
}
break;
default:
throw new Error(`Unknown Action: ${Action}`);
}
});
}
printDiff(From, To, indentation) {
const differences = diff(From, To);
if (differences) {
differences.forEach(({ kind, path = [], lhs, rhs }) => {
this.print(chalk.white, '');
this.print(chalk.white, path.join('.'), indentation);
switch (kind) {
case 'N':
this.printObj(chalk.green, rhs, indentation + 2);
return;
case 'D':
this.printObj(chalk.red, lhs, indentation + 2);
return;
case 'E':
this.printObj(chalk.dim, lhs, indentation + 2);
this.printObj(chalk.magenta, rhs, indentation + 2);
return;
}
});
}
}
printObj(colour, obj, indentation) {
if (typeof obj === 'object' || Array.isArray(obj)) {
if (Object.keys(obj).length > 0) {
const str = YAML.safeDump(obj);
this.print(colour, str, indentation);
}
} else {
this.print(colour, obj.toString(), indentation);
}
}
print(colour, str, indentation = 1) {
const indented = str.replace(/^(?!\s*$)/mg, ' '.repeat(indentation));
this.serverless.cli.consoleLog(colour(indented));
}
}
module.exports = {
printChanges: (serverless, stackName, changeSetName, changes) => {
return new Printer(serverless).printChanges(stackName, changeSetName, changes);
},
printStackPolicy: (serverless, stackName, stackPolicy) => {
return new Printer(serverless).printStackPolicy(stackName, stackPolicy);
},
}