UNPKG

2.83 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/* eslint-disable quotes */
4
5'use strict';
6
7const program = require('commander');
8const getVisitor = require('../lib/visitor').getVisitor;
9const notifier = require('../lib/update-notifier');
10
11
12program
13 .name('fun edge invoke')
14 .description(
15 `Execute your function in a local Link IoT Edge environment which replicates the release
16 environment almost identically. You can pass in the event body via stdin or by using
17 the -e (--event) parameter.`)
18 .usage('[options] <[service/]function>')
19 .option('--debug',
20 `Specify your function executes in debug mode, and listens
21 on port 5700 at localhost for debuggers to connect`)
22 .option('-e, --event <path>',
23 `A file containing event data passed to the function during
24 invoke, If this option is not specified, it defaults to
25 reading event from stdin`)
26 .option('-c, --config <ide/debugger>',
27 `Output configurations for the specified ide/debugger, where
28 the ide/debugger can currently only be vscode`)
29 .option('--output-debugger-configs',
30 `Output configurations for all debuggers. It will override
31 the behavior of --config option`)
32 .parse(process.argv);
33
34if (!program.args.length) {
35 console.error();
36 console.error(" error: missing required argument `%s'", '[service/]function');
37 program.help();
38}
39
40if (!program.args.length > 1) {
41 console.error();
42 console.error(" error: unexpected argument `%s'", program.args[1]);
43 program.help();
44}
45
46// The debug port can't be configured at present since the debugging container is
47// pre-created. It will be supported when the debugging container is created according
48// to the template.yaml.
49if (program.debug) {
50 program.debugPort = '5700';
51}
52if (program.debugPort) {
53 const debugPort = parseInt(program.debugPort);
54 if (Number.isNaN(debugPort)) {
55 throw new Error(`\n error: not a number '${program.debugPort}'\n`);
56 }
57 program.debugPort = debugPort;
58}
59
60// Check config values.
61if (program.config) {
62 if (program.config !== 'vscode') {
63 throw new Error(`\n error: invalid value '${program.config}'\n`);
64 }
65 program.outputDebuggerConfigs = true;
66}
67
68program.event = program.event || '-';
69
70notifier.notify();
71
72getVisitor().then(visitor => {
73 visitor.pageview('/fun/edge/invoke').send();
74
75 require('../lib/commands/edge/invoke')(program.args[0], program)
76 .then(() => {
77 visitor.event({
78 ec: 'edge',
79 ea: 'invoke',
80 el: 'success',
81 dp: '/fun/edge'
82 }).send();
83 })
84 .catch(error => {
85 visitor.event({
86 ec: 'edge',
87 ea: 'invoke',
88 el: 'error',
89 dp: '/fun/edge'
90 }).send();
91
92 require('../lib/exception-handler')(error);
93 });
94});
95