MCPcopy
hub / github.com/serverless/serverless / print

Method print

packages/serverless/lib/plugins/print.js:26–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

24 }
25
26 async print() {
27 let conf = this.serverless.configurationInput
28
29 // dig into the object
30 if (this.options.path) {
31 const steps = this.options.path.split('.')
32 for (const step of steps) {
33 conf = conf[step]
34
35 if (!conf) {
36 throw new ServerlessError(
37 `Path "${this.options.path}" not found`,
38 'INVALID_PATH_ARGUMENT',
39 )
40 }
41 }
42 }
43
44 // apply an optional filter
45 if (this.options.transform) {
46 if (this.options.transform === 'keys') {
47 conf = Object.keys(conf)
48 } else {
49 throw new ServerlessError(
50 'Transform can only be "keys"',
51 'INVALID_TRANSFORM',
52 )
53 }
54 }
55
56 // print configuration in the specified format
57 const format = this.options.format || 'yaml'
58 let out
59
60 if (format === 'text') {
61 if (Array.isArray(conf)) {
62 out = conf.join(os.EOL)
63 } else {
64 if (_.isObject(conf)) {
65 throw new ServerlessError(
66 'Cannot print an object as "text"',
67 'PRINT_INVALID_OBJECT_AS_TEXT',
68 )
69 }
70
71 out = String(conf)
72 }
73 } else if (format === 'json') {
74 out = jc.stringify(conf, null, ' ')
75 } else if (format === 'yaml') {
76 out = yaml.dump(JSON.parse(jc.stringify(conf)), { noRefs: true })
77 } else {
78 throw new ServerlessError(
79 'Format must be "yaml", "json" or "text"',
80 'PRINT_INVALID_FORMAT',
81 )
82 }
83

Callers 2

_renderCodeChangeSummaryFunction · 0.80
nestedSectionFormatterFunction · 0.80

Calls 1

writeTextFunction · 0.90

Tested by

no test coverage detected