-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
serverless.js
executable file
·772 lines (691 loc) · 29.2 KB
/
serverless.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/usr/bin/env node
'use strict';
require('essentials');
// global graceful-fs patch
// https://github.com/isaacs/node-graceful-fs#global-patching
require('graceful-fs').gracefulify(require('fs'));
// Setup log writing
require('@serverless/utils/log-reporters/node');
const { log, progress } = require('@serverless/utils/log');
const handleError = require('../lib/cli/handle-error');
const {
storeLocally: storeTelemetryLocally,
send: sendTelemetry,
} = require('../lib/utils/telemetry');
const generateTelemetryPayload = require('../lib/utils/telemetry/generatePayload');
const isTelemetryDisabled = require('../lib/utils/telemetry/areDisabled');
let command;
let options;
let commandSchema;
let serviceDir = null;
let configuration = null;
let serverless;
const commandUsage = {};
const variableSourcesInConfig = new Set();
let hasTelemetryBeenReported = false;
// Inquirer async operations do not keep node process alive
// We need to issue a keep alive timer so process does not die
// to properly handle e.g. `SIGINT` interrupt
const keepAliveTimer = setTimeout(() => {}, 60 * 60 * 1000);
process.once('uncaughtException', (error) => {
clearTimeout(keepAliveTimer);
progress.clear();
const cachedHasTelemetryBeenReported = hasTelemetryBeenReported;
hasTelemetryBeenReported = true;
log.error('Uncaught exception');
handleError(error, {
command,
options,
commandSchema,
serviceDir,
configuration,
serverless,
hasTelemetryBeenReported: cachedHasTelemetryBeenReported,
commandUsage,
variableSourcesInConfig,
}).then(() => {
process.exit();
});
});
require('signal-exit/signals').forEach((signal) => {
process.once(signal, () => {
clearTimeout(keepAliveTimer);
progress.clear();
// If there's another listener (e.g. we're in deamon context or reading stdin input)
// then let the other listener decide how process will exit
const isOtherSigintListener = Boolean(process.listenerCount(signal));
if (!hasTelemetryBeenReported) {
hasTelemetryBeenReported = true;
if (commandSchema && !isTelemetryDisabled) {
const telemetryPayload = generateTelemetryPayload({
command,
options,
commandSchema,
serviceDir,
configuration,
serverless,
commandUsage,
variableSources: variableSourcesInConfig,
});
storeTelemetryLocally({
...telemetryPayload,
outcome: 'interrupt',
interruptSignal: signal,
});
}
}
if (isOtherSigintListener) return;
// Follow recommendation from signal-exit:
// https://github.com/tapjs/signal-exit/blob/654117d6c9035ff6a805db4d4acf1f0c820fcb21/index.js#L97-L98
if (process.platform === 'win32' && signal === 'SIGHUP') signal = 'SIGINT';
process.kill(process.pid, signal);
});
});
const humanizePropertyPathKeys = require('../lib/configuration/variables/humanize-property-path-keys');
const processBackendNotificationRequest = require('../lib/utils/processBackendNotificationRequest');
const logDeprecation = require('../lib/utils/logDeprecation');
const rewriteDeployFunctionParam = () => {
const isParamName = RegExp.prototype.test.bind(require('../lib/cli/param-reg-exp'));
const args = process.argv.slice(2);
const firstParamIndex = args.findIndex(isParamName);
const commands = args.slice(0, firstParamIndex === -1 ? Infinity : firstParamIndex);
if (commands.join(' ') !== 'deploy') return;
if (!args.includes('-f') && !args.includes('--function')) return;
logDeprecation(
'CLI_DEPLOY_FUNCTION_OPTION_V3',
'Starting with v4.0.0, `--function` or `-f` option for `deploy` command will no longer be supported. In order to deploy a single function, please use `deploy function` command instead.'
);
process.argv.splice(3, 0, 'function');
};
const processSpanPromise = (async () => {
try {
const wait = require('timers-ext/promise/sleep');
await wait(); // Ensure access to "processSpanPromise"
rewriteDeployFunctionParam();
const resolveInput = require('../lib/cli/resolve-input');
let commands;
let isHelpRequest;
// Parse args against schemas of commands which do not require to be run in service context
({ command, commands, options, isHelpRequest, commandSchema } = resolveInput(
require('../lib/cli/commands-schema/no-service')
));
// If version number request, show it and abort
if (options.version) {
await require('../lib/cli/render-version')();
await logDeprecation.printSummary();
return;
}
const ServerlessError = require('../lib/serverless-error');
// Abort if command is not supported in this environment
if (commandSchema && commandSchema.isHidden && commandSchema.noSupportNotice) {
throw new ServerlessError(
`Cannot run \`${command}\` command: ${commandSchema.noSupportNotice}`,
'NOT_SUPPORTED_COMMAND'
);
}
const path = require('path');
const uuid = require('uuid');
const _ = require('lodash');
const clear = require('ext/object/clear');
const Serverless = require('../lib/Serverless');
const resolveVariables = require('../lib/configuration/variables/resolve');
const isPropertyResolved = require('../lib/configuration/variables/is-property-resolved');
const eventuallyReportVariableResolutionErrors = require('../lib/configuration/variables/eventually-report-resolution-errors');
const filterSupportedOptions = require('../lib/cli/filter-supported-options');
let configurationPath = null;
let providerName;
let variablesMeta;
let resolverConfiguration;
let isInteractiveSetup;
const ensureResolvedProperty = (propertyPath) => {
if (isPropertyResolved(variablesMeta, propertyPath)) return true;
variablesMeta = null;
if (isHelpRequest) return false;
const humanizedPropertyPath = humanizePropertyPathKeys(propertyPath.split('\0'));
throw new ServerlessError(
`Cannot resolve ${path.basename(
configurationPath
)}: "${humanizedPropertyPath}" property is not accessible ` +
'(configured behind variables which cannot be resolved at this stage)',
'INACCESSIBLE_CONFIGURATION_PROPERTY'
);
};
if (!commandSchema || commandSchema.serviceDependencyMode) {
// Command is potentially service specific, follow up with resolution of service config
isInteractiveSetup = !isHelpRequest && command === '';
const resolveConfigurationPath = require('../lib/cli/resolve-configuration-path');
const readConfiguration = require('../lib/configuration/read');
const resolveProviderName = require('../lib/configuration/resolve-provider-name');
// Resolve eventual service configuration path
configurationPath = await resolveConfigurationPath();
// If service configuration file is found, load its content
configuration = configurationPath
? await (async () => {
try {
return await readConfiguration(configurationPath);
} catch (error) {
// Configuration syntax error should not prevent help from being displayed
// (if possible configuration should be read for help request as registered
// plugins may introduce new commands to be listed in help output)
if (isHelpRequest) return null;
throw error;
}
})()
: null;
if (configuration) {
serviceDir = process.cwd();
if (!commandSchema) {
// If command was not recognized in first resolution phase
// parse args again also against schemas of commands which require service context
resolveInput.clear();
({ command, commands, options, isHelpRequest, commandSchema } = resolveInput(
require('../lib/cli/commands-schema/service')
));
}
// IIFE for maintanance convenience
await (async () => {
const resolveVariablesMeta = require('../lib/configuration/variables/resolve-meta');
variablesMeta = resolveVariablesMeta(configuration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
// Variable syntax errors, abort
variablesMeta = null;
return;
}
if (!ensureResolvedProperty('disabledDeprecations')) return;
if (!ensureResolvedProperty('deprecationNotificationMode')) return;
if (isPropertyResolved(variablesMeta, 'provider\0name')) {
providerName = resolveProviderName(configuration);
if (providerName == null) {
variablesMeta = null;
return;
}
}
if (!commandSchema && providerName === 'aws') {
// If command was not recognized in previous resolution phases
// parse args again also against schemas commands which require AWS service context
resolveInput.clear();
({ command, commands, options, isHelpRequest, commandSchema } = resolveInput(
require('../lib/cli/commands-schema/aws-service')
));
}
let envVarNamesNeededForDotenvResolution;
if (variablesMeta.size) {
// Some properties are configured with variables
// Resolve eventual variables in `provider.stage` and `useDotEnv`
// (required for reliable .env resolution)
resolverConfiguration = {
serviceDir,
configuration,
variablesMeta,
sources: {
env: require('../lib/configuration/variables/sources/env'),
file: require('../lib/configuration/variables/sources/file'),
opt: require('../lib/configuration/variables/sources/opt'),
self: require('../lib/configuration/variables/sources/self'),
strToBool: require('../lib/configuration/variables/sources/str-to-bool'),
sls: require('../lib/configuration/variables/sources/instance-dependent/get-sls')(),
},
options: filterSupportedOptions(options, { commandSchema, providerName }),
fulfilledSources: new Set(['file', 'self', 'strToBool']),
propertyPathsToResolve: new Set(['provider\0name', 'provider\0stage', 'useDotenv']),
variableSourcesInConfig,
};
if (isInteractiveSetup) resolverConfiguration.fulfilledSources.add('opt');
await resolveVariables(resolverConfiguration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
// Unrecoverable resolution errors, abort
variablesMeta = null;
return;
}
if (!providerName && isPropertyResolved(variablesMeta, 'provider\0name')) {
providerName = resolveProviderName(configuration);
if (providerName == null) {
variablesMeta = null;
return;
}
if (!commandSchema && providerName === 'aws') {
// If command was not recognized in previous resolution phases
// Parse args again also against schemas of commands which work in context of an AWS
// service
resolveInput.clear();
({ command, commands, options, isHelpRequest, commandSchema } = resolveInput(
require('../lib/cli/commands-schema/aws-service')
));
if (commandSchema) {
resolverConfiguration.options = filterSupportedOptions(options, {
commandSchema,
providerName,
});
await resolveVariables(resolverConfiguration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
variablesMeta = null;
return;
}
}
}
}
resolverConfiguration.fulfilledSources.add('env');
if (
!isPropertyResolved(variablesMeta, 'provider\0stage') ||
!isPropertyResolved(variablesMeta, 'useDotenv')
) {
// Assume "env" source fulfilled for `provider.stage` and `useDotenv` resolution.
// To pick eventual resolution conflict, track what env variables were reported
// misssing when applying this resolution
const envSource = require('../lib/configuration/variables/sources/env');
envSource.missingEnvVariables.clear();
await resolveVariables({
...resolverConfiguration,
propertyPathsToResolve: new Set(['provider\0stage', 'useDotenv']),
});
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
// Unrecoverable resolution errors, abort
variablesMeta = null;
return;
}
if (
!ensureResolvedProperty('provider\0stage', {
shouldSilentlyReturnIfLegacyMode: true,
})
) {
return;
}
if (!ensureResolvedProperty('useDotenv')) return;
envVarNamesNeededForDotenvResolution = envSource.missingEnvVariables;
}
}
// Load eventual environment variables from .env files
await require('../lib/cli/conditionally-load-dotenv')(options, configuration);
if (envVarNamesNeededForDotenvResolution) {
for (const envVarName of envVarNamesNeededForDotenvResolution) {
if (process.env[envVarName]) {
throw new ServerlessError(
'Cannot reliably resolve "env" variables due to resolution conflict.\n' +
`Environment variable "${envVarName}" which influences resolution of ` +
'".env" file were found to be defined in resolved ".env" file.' +
'DOTENV_ENV_VAR_RESOLUTION_CONFLICT'
);
}
}
}
if (!variablesMeta.size) return; // No properties configured with variables
if (isHelpRequest || commands[0] === 'plugin') {
// We do not need full config resolved, we just need to know what
// provider is service setup with, and with what eventual plugins Framework is extended
// as that influences what CLI commands and options could be used,
resolverConfiguration.propertyPathsToResolve.add('plugins');
} else {
delete resolverConfiguration.propertyPathsToResolve;
}
await resolveVariables(resolverConfiguration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
variablesMeta = null;
return;
}
if (!providerName) {
if (!ensureResolvedProperty('provider\0name')) return;
providerName = resolveProviderName(configuration);
if (providerName == null) {
variablesMeta = null;
return;
}
if (!commandSchema && providerName === 'aws') {
resolveInput.clear();
({ command, commands, options, isHelpRequest, commandSchema } = resolveInput(
require('../lib/cli/commands-schema/aws-service')
));
if (commandSchema) {
resolverConfiguration.options = filterSupportedOptions(options, {
commandSchema,
providerName,
});
await resolveVariables(resolverConfiguration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
variablesMeta = null;
return;
}
}
}
}
if (!variablesMeta.size) return; // All properties successuflly resolved
if (!ensureResolvedProperty('plugins')) return;
if (!ensureResolvedProperty('package\0path')) return;
if (!ensureResolvedProperty('frameworkVersion')) return;
if (!ensureResolvedProperty('app')) return;
if (!ensureResolvedProperty('org')) return;
if (!ensureResolvedProperty('service')) return;
if (configuration.org) {
// Dashboard requires AWS region to be resolved upfront
ensureResolvedProperty('provider\0region');
}
})();
// Ensure to have full AWS commands schema loaded if we're in context of AWS provider
// It's not the case if not AWS service specific command was resolved
if (configuration && resolveProviderName(configuration) === 'aws') {
resolveInput.clear();
({ command, commands, options, isHelpRequest, commandSchema } = resolveInput(
require('../lib/cli/commands-schema/aws-service')
));
}
} else {
// In non-service context we recognize all AWS service commands
resolveInput.clear();
({ command, commands, options, isHelpRequest, commandSchema } = resolveInput(
require('../lib/cli/commands-schema/aws-service')
));
// Validate result command and options
require('../lib/cli/ensure-supported-command')();
}
} else {
require('../lib/cli/ensure-supported-command')();
}
const configurationFilename = configuration && configurationPath.slice(serviceDir.length + 1);
// Names of the commands which are configured independently in root `commands` folder
// and not in Serverless class internals
const notIntegratedCommands = new Set(['doctor', 'plugin install', 'plugin uninstall']);
const isStandaloneCommand = notIntegratedCommands.has(command);
if (!isHelpRequest && (isInteractiveSetup || isStandaloneCommand)) {
if (configuration) require('../lib/cli/ensure-supported-command')(configuration);
if (isInteractiveSetup) {
if (!process.stdin.isTTY && !process.env.SLS_INTERACTIVE_SETUP_ENABLE) {
throw new ServerlessError(
'Attempted to run an interactive setup in non TTY environment.\n' +
"If that's intended, run with the SLS_INTERACTIVE_SETUP_ENABLE=1 environment variable",
'INTERACTIVE_SETUP_IN_NON_TTY'
);
}
const interactiveContext = await require('../lib/cli/interactive-setup')({
configuration,
serviceDir,
configurationFilename,
options,
commandUsage,
});
if (interactiveContext.configuration) {
configuration = interactiveContext.configuration;
}
if (interactiveContext.serverless) {
serverless = interactiveContext.serverless;
}
} else {
await require(`../commands/${commands.join('-')}`)({
configuration,
serviceDir,
configurationFilename,
options,
});
}
progress.clear();
await logDeprecation.printSummary();
if (!hasTelemetryBeenReported) {
hasTelemetryBeenReported = true;
if (!isTelemetryDisabled) {
storeTelemetryLocally({
...generateTelemetryPayload({
command,
options,
commandSchema,
serviceDir,
configuration,
commandUsage,
variableSources: variableSourcesInConfig,
serverless,
}),
outcome: 'success',
});
await sendTelemetry({ serverlessExecutionSpan: processSpanPromise });
}
}
return;
}
serverless = new Serverless({
configuration,
serviceDir,
configurationFilename,
isConfigurationResolved:
commands[0] === 'plugin' || Boolean(variablesMeta && !variablesMeta.size),
commands,
options,
});
try {
serverless.onExitPromise = processSpanPromise;
serverless.invocationId = uuid.v4();
await serverless.init();
// IIFE for maintanance convenience
await (async () => {
if (!configuration) return;
let hasFinalCommandSchema = false;
if (configuration.plugins) {
// After plugins are loaded, re-resolve CLI command and options schema as plugin
// might have defined extra commands and options
if (serverless.pluginManager.externalPlugins.size) {
const commandsSchema = require('../lib/cli/commands-schema/resolve-final')(
serverless.pluginManager.externalPlugins,
{ providerName: providerName || 'aws', configuration }
);
resolveInput.clear();
({ command, commands, options, isHelpRequest, commandSchema } =
resolveInput(commandsSchema));
serverless.processedInput.commands = serverless.pluginManager.cliCommands = commands;
serverless.processedInput.options = options;
Object.assign(clear(serverless.pluginManager.cliOptions), options);
hasFinalCommandSchema = true;
}
}
if (!providerName && !hasFinalCommandSchema) {
// Invalid configuration, ensure to recognize all AWS commands
resolveInput.clear();
({ command, commands, options, isHelpRequest, commandSchema } = resolveInput(
require('../lib/cli/commands-schema/aws-service')
));
}
hasFinalCommandSchema = true;
// Validate result command and options
if (hasFinalCommandSchema) require('../lib/cli/ensure-supported-command')(configuration);
if (isHelpRequest) return;
if (!_.get(variablesMeta, 'size')) return;
// Resolve remaininig service configuration variables
if (providerName === 'aws') {
// Ensure properties which are crucial to some variable source resolvers
// are actually resolved.
if (
!ensureResolvedProperty('provider\0credentials') ||
!ensureResolvedProperty('provider\0deploymentBucket\0serverSideEncryption') ||
!ensureResolvedProperty('provider\0profile') ||
!ensureResolvedProperty('provider\0region')
) {
return;
}
}
if (commandSchema) {
resolverConfiguration.options = filterSupportedOptions(options, {
commandSchema,
providerName,
});
}
resolverConfiguration.fulfilledSources.add('opt');
// Register serverless instance and AWS provider specific variable sources
resolverConfiguration.sources.sls =
require('../lib/configuration/variables/sources/instance-dependent/get-sls')(serverless);
resolverConfiguration.fulfilledSources.add('sls');
if (providerName === 'aws') {
Object.assign(resolverConfiguration.sources, {
cf: require('../lib/configuration/variables/sources/instance-dependent/get-cf')(
serverless
),
s3: require('../lib/configuration/variables/sources/instance-dependent/get-s3')(
serverless
),
ssm: require('../lib/configuration/variables/sources/instance-dependent/get-ssm')(
serverless
),
aws: require('../lib/configuration/variables/sources/instance-dependent/get-aws')(
serverless
),
});
resolverConfiguration.fulfilledSources.add('cf').add('s3').add('ssm');
}
// Register dashboard specific variable source resolvers
if (serverless.pluginManager.dashboardPlugin) {
if (configuration.org) {
for (const [sourceName, sourceConfig] of Object.entries(
serverless.pluginManager.dashboardPlugin.configurationVariablesSources
)) {
resolverConfiguration.sources[sourceName] = sourceConfig;
resolverConfiguration.fulfilledSources.add(sourceName);
}
} else {
resolverConfiguration.sources.param =
serverless.pluginManager.dashboardPlugin.configurationVariablesSources.param;
resolverConfiguration.fulfilledSources.add('param');
}
}
// Register variable source resolvers provided by external plugins
const resolverExternalPluginSources = require('../lib/configuration/variables/sources/resolve-external-plugin-sources');
resolverExternalPluginSources(
configuration,
resolverConfiguration,
serverless.pluginManager.externalPlugins
);
// Having all source resolvers configured, resolve variables
await resolveVariables(resolverConfiguration);
if (!variablesMeta.size) return;
if (
eventuallyReportVariableResolutionErrors(configurationPath, configuration, variablesMeta)
) {
return;
}
// Do not confirm on unresolved sources with partially resolved configuration
if (resolverConfiguration.propertyPathsToResolve) return;
// Report unrecognized variable sources found in variables configured in service config
const unresolvedSources =
require('../lib/configuration/variables/resolve-unresolved-source-types')(variablesMeta);
const recognizedSourceNames = new Set(Object.keys(resolverConfiguration.sources));
const unrecognizedSourceNames = Array.from(unresolvedSources.keys()).filter(
(sourceName) => !recognizedSourceNames.has(sourceName)
);
if (unrecognizedSourceNames.includes('output')) {
throw new ServerlessError(
'"Cannot resolve configuration: ' +
'"output" variable can only be used in ' +
'services deployed with Serverless Dashboard (with "org" setting configured)',
'DASHBOARD_VARIABLE_SOURCES_MISUSE'
);
}
throw new ServerlessError(
`Unrecognized configuration variable sources: "${unrecognizedSourceNames.join('", "')}"`,
'UNRECOGNIZED_VARIABLE_SOURCES'
);
})();
if (isHelpRequest && serverless.pluginManager.externalPlugins) {
// Show help
require('../lib/cli/render-help')(serverless.pluginManager.externalPlugins);
} else {
// Run command
await serverless.run();
}
progress.clear();
await logDeprecation.printSummary();
if (!hasTelemetryBeenReported) {
hasTelemetryBeenReported = true;
if (!isTelemetryDisabled && !isHelpRequest) {
storeTelemetryLocally({
...generateTelemetryPayload({
command,
options,
commandSchema,
serviceDir,
configuration,
serverless,
variableSources: variableSourcesInConfig,
}),
outcome: 'success',
});
let backendNotificationRequest;
if (commands.join(' ') === 'deploy') {
backendNotificationRequest = await sendTelemetry({
serverlessExecutionSpan: processSpanPromise,
});
}
if (backendNotificationRequest) {
await processBackendNotificationRequest(backendNotificationRequest);
}
}
}
} catch (error) {
// If Dashboard Plugin, capture error
const dashboardPlugin =
serverless.pluginManager.dashboardPlugin ||
serverless.pluginManager.plugins.find((p) => p.enterprise);
const dashboardErrorHandler = _.get(dashboardPlugin, 'enterprise.errorHandler');
if (!dashboardErrorHandler) throw error;
try {
await dashboardErrorHandler(error, serverless.invocationId);
} catch (dashboardErrorHandlerError) {
const tokenizeException = require('../lib/utils/tokenize-exception');
const exceptionTokens = tokenizeException(dashboardErrorHandlerError);
log.warning(
`Publication to Serverless Dashboard errored with:\n${' '.repeat('Serverless: '.length)}${
exceptionTokens.isUserError || !exceptionTokens.stack
? exceptionTokens.message
: exceptionTokens.stack
}`
);
}
throw error;
}
} catch (error) {
progress.clear();
const cachedHasTelemetryBeenReported = hasTelemetryBeenReported;
hasTelemetryBeenReported = true;
handleError(error, {
command,
options,
commandSchema,
serviceDir,
configuration,
serverless,
hasTelemetryBeenReported: cachedHasTelemetryBeenReported,
commandUsage,
variableSources: variableSourcesInConfig,
});
} finally {
clearTimeout(keepAliveTimer);
}
})();