-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
print.js
135 lines (123 loc) · 3.99 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
'use strict';
const os = require('os');
const _ = require('lodash');
const jc = require('json-cycle');
const yaml = require('js-yaml');
const ServerlessError = require('../serverless-error');
const cliCommandsSchema = require('../cli/commands-schema');
const { writeText, legacy } = require('@serverless/utils/log');
class Print {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.cache = {};
this.commands = {
print: {
...cliCommandsSchema.get('print'),
},
};
this.hooks = {
'print:print': this.print.bind(this),
};
}
adorn(serviceConfig) {
// service object treatment
this.cache.serviceService = serviceConfig.service;
if (_.isObject(this.cache.serviceService)) {
serviceConfig.service = serviceConfig.service.name;
serviceConfig.serviceObject = this.cache.serviceService;
}
// provider treatment and defaults
this.cache.serviceProvider = serviceConfig.provider;
if (typeof this.cache.serviceProvider === 'string') {
serviceConfig.provider = { name: this.cache.serviceProvider };
}
const serviceProvider = this.serverless.service.provider;
serviceConfig.provider = _.merge(
{
stage: serviceProvider.stage,
region: serviceProvider.region,
variableSyntax: serviceProvider.variableSyntax,
},
serviceConfig.provider
);
}
strip(serviceConfig) {
if (_.isObject(this.cache.serviceService)) {
serviceConfig.service = this.cache.serviceService;
delete serviceConfig.serviceObject;
}
if (typeof this.cache.serviceProvider === 'string') {
serviceConfig.provider = this.cache.serviceProvider;
} else {
// is object
if (!this.cache.serviceProvider.stage) {
delete serviceConfig.provider.stage;
}
if (!this.cache.serviceProvider.region) {
delete serviceConfig.provider.region;
}
if (this.cache.serviceProvider.variableSyntax) {
serviceConfig.provider.variableSyntax = this.cache.serviceProvider.variableSyntax;
}
}
}
async print() {
const svc = this.serverless.configurationInput;
const service = svc;
this.adorn(service);
// Need to delete variableSyntax to avoid self-matching errors
this.serverless.variables.loadVariableSyntax();
delete service.provider.variableSyntax; // cached by adorn, restored by strip
this.serverless.variables.service = service;
const populated = await this.serverless.variables.populateObject(service);
let conf = populated;
this.strip(conf);
// dig into the object
if (this.options.path) {
const steps = this.options.path.split('.');
for (const step of steps) {
conf = conf[step];
if (!conf) {
throw new ServerlessError(
`Path "${this.options.path}" not found`,
'INVALID_PATH_ARGUMENT'
);
}
}
}
// apply an optional filter
if (this.options.transform) {
if (this.options.transform === 'keys') {
conf = Object.keys(conf);
} else {
throw new ServerlessError('Transform can only be "keys"', 'INVALID_TRANSFORM');
}
}
// print configuration in the specified format
const format = this.options.format || 'yaml';
let out;
if (format === 'text') {
if (Array.isArray(conf)) {
out = conf.join(os.EOL);
} else {
if (_.isObject(conf)) {
throw new ServerlessError(
'Cannot print an object as "text"',
'PRINT_INVALID_OBJECT_AS_TEXT'
);
}
out = String(conf);
}
} else if (format === 'json') {
out = jc.stringify(conf, null, ' ');
} else if (format === 'yaml') {
out = yaml.dump(JSON.parse(jc.stringify(conf)), { noRefs: true });
} else {
throw new ServerlessError('Format must be "yaml", "json" or "text"', 'PRINT_INVALID_FORMAT');
}
legacy.consoleLog(out);
writeText(out);
}
}
module.exports = Print;