This repository was archived by the owner on Sep 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwilio-runtime-utils.js
More file actions
127 lines (112 loc) · 3.61 KB
/
twilio-runtime-utils.js
File metadata and controls
127 lines (112 loc) · 3.61 KB
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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var ArgumentParser = require('argparse').ArgumentParser;
var Descriptor = require('./Descriptor.js');
var FunctionDescriptor = require('./FunctionDescriptor.js');
var Context = require('./Context.js');
var TwilioRuntimeHelper = require("./runtime/TwilioRuntimeHelper.js");
function extend(obj, src) {
Object.keys(src).forEach(function(key) { obj[key] = src[key]; });
return obj;
}
////////////////////////////////////////////////////////////
// argument parser
////////////////////////////////////////////////////////////
var parser = new ArgumentParser({
addHelp: true,
description: 'Twilio Runtime Utilities'
});
parser.addArgument(
['-c', '--context'],
{
help: 'global context for data that are deployment specific',
required: true
}
);
var subparsers = parser.addSubparsers({
title: 'commands',
dest: 'command_name'
});
var runCommandParser = subparsers.addParser('run', {
addHelp: true,
description: 'run function locally with test data files'
});
runCommandParser.addArgument(
['descriptor'],
{
help: 'function descriptor yaml'
}
);
runCommandParser.addArgument(
['event'],
{
help: 'test event yaml'
}
);
var runCommandParser = subparsers.addParser('runjson', {
addHelp: true,
description: 'run function locally with test data json'
});
runCommandParser.addArgument(
['descriptor'],
{
help: 'function descriptor json'
}
);
runCommandParser.addArgument(
['event'],
{
help: 'test event json'
}
);
var deployCommandParser = subparsers.addParser('deploy', {
addHelp: true,
description: 'deploy single function or whole runtime functions set to the runtime deployment'
});
deployCommandParser.addArgument(
['descriptor'],
{
help: 'function or runtime descriptor yaml'
}
);
////////////////////////////////////////////////////////////
// command: deploy
////////////////////////////////////////////////////////////
function deployFunction(functionDescriptor) {
let spliter = "================================================================================\n";
process.stdout.write(spliter);
process.stdout.write("== create new function if needed\n");
process.stdout.write("== set function path to: " + functionDescriptor.path + "\n");
process.stdout.write("== switch off \"access control\"\n");
process.stdout.write("== copy paste the script to function code area\n");
process.stdout.write(spliter);
let bundle = FunctionDescriptor.createBundle(functionDescriptor);
process.stdout.write(bundle);
process.stdout.write(spliter);
var context = Context.load(args.context);
Context.deploy(context);
}
////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////
var args = parser.parseArgs();
function runResultHandler(err, result) {
if (err) {
console.error(err);
} else {
console.log(result);
}
}
/*****/if ('run' === args.command_name) {
TwilioRuntimeHelper.runTestDataFile(args.context, args.descriptor, args.event == null ? "" : args.event, runResultHandler);
} else if ('runjson' === args.command_name) {
TwilioRuntimeHelper.runTestDataJSON(args.context, args.descriptor, JSON.parse(args.event), runResultHandler);
} else if ('deploy' === args.command_name) {
var descriptor = Descriptor.load(args.descriptor);
if (descriptor.type === "function") {
deployFunction(descriptor);
} else {
console.error("descriptor is of unknown type: " + args.descriptor.type);
}
}