-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrunfaast.ts
80 lines (71 loc) · 2.12 KB
/
runfaast.ts
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
import { faast } from "faastjs";
import { existsSync } from "fs";
import { resolve } from "path";
import * as funcs from "./functions";
function help() {
console.log(`Usage: node dist/runfaast.js [options] <command> [args...]
Run a command on lambda using faast.js
Options:
-d, --dir <dir> Send a directory to the lambda function and make it available
in the current working directory in the cloud function.
-l, --local Run the command locally instead of on AWS.
-h, --help Output usage information`);
process.exit(0);
}
(async () => {
const [, , ...args] = process.argv;
let dir = undefined;
let local = false;
if (args.length === 0) {
help();
}
while (args.length > 0 && args[0].startsWith("-")) {
switch (args[0]) {
case "-d":
case "--dir":
args.shift();
dir = args.shift()!;
break;
case "-l":
case "--local":
args.shift();
local = true;
break;
case "-h":
case "--help":
help();
}
}
if (args.length === 0) {
help();
}
const command = args.join(" ");
let resolvedDir = undefined;
if (dir) {
resolvedDir = resolve(dir);
if (dir && !existsSync(dir)) {
console.log(`Could not find directory '${dir}'`);
process.exit(-1);
}
}
console.log(`Command: '${command}'`);
console.log(`Dir: ${resolvedDir}`);
console.log(`Cwd: ${process.cwd()}`);
console.log(`Creating cloud function.`);
const provider = local ? "local" : "aws";
const m = await faast(provider, funcs, {
memorySize: 1728,
timeout: 600,
include: [resolvedDir!]
});
console.log(`Logs: ${m.logUrl()}`);
try {
const result = await m.functions.exec(command);
console.log(`Output:`);
console.log(result);
const cost = await m.costSnapshot();
console.log(`Cost: $${cost.total().toFixed(8)}`);
} finally {
await m.cleanup();
}
})();