-
Notifications
You must be signed in to change notification settings - Fork 6
/
timeout.ts
102 lines (87 loc) · 2.73 KB
/
timeout.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { Command, flags } from "@oclif/command";
import {
writeJsonSync,
readJsonSync,
readFileSync,
writeFileSync,
} from "fs-extra";
import { startSpinner, stopSpinner } from "../../lib/spinner";
import { PanacloudconfigFile } from "../../utils/constants";
const prettier = require("prettier");
export default class Timeout extends Command {
static description = "Upate panacloudconfig.json";
static args = [{ name: "query_name" }, { name: "timeout" }];
static flags = {
help: flags.help({ char: "h" }),
};
checkValExists(
panacloudConfig: PanacloudconfigFile,
keys: string[],
query_name: string
): boolean {
let res = false;
keys.forEach((e) => {
if (panacloudConfig.lambdas[e].is_mock === undefined) {
let obj = Object.keys(panacloudConfig.lambdas[e]).filter(
(v) => v === query_name
);
if (obj.length !== 0) res = true;
}
});
return res;
}
async run() {
const { flags, args } = this.parse(Timeout);
const spinner = startSpinner("Updating Panacloud Config");
if (
(!args.timeout && !args.query_name) ||
(!args.timeout && args.query_name)
) {
stopSpinner(
spinner,
"Please use panacloud config:timeout -h to get more info about the command",
true
);
process.exit(1);
}
const panacloudConfig: PanacloudconfigFile = readJsonSync(
"editable_src/panacloudconfig.json"
);
const keys = Object.keys(panacloudConfig.lambdas);
if (!keys.includes(args.query_name)) {
if (!this.checkValExists(panacloudConfig, keys, args.query_name)) {
stopSpinner(spinner, `${args.query_name} Not Exists`, true);
process.exit(1);
}
}
if (args.timeout <= 0) {
stopSpinner(spinner, "Invalid Timeout", true);
process.exit(1);
}
if (panacloudConfig.lambdas[args.query_name]?.is_mock === undefined) {
keys.forEach((e) => {
if (panacloudConfig.lambdas[e].is_mock === undefined) {
Object.keys(panacloudConfig.lambdas[e]).forEach((v) => {
if (v === args.query_name) {
panacloudConfig.lambdas[e][v].timeout = parseInt(args.timeout);
}
});
}
});
} else {
panacloudConfig.lambdas[args.query_name].timeout = parseInt(args.timeout);
}
writeJsonSync("editable_src/panacloudconfig.json", panacloudConfig);
// Formating Data
const data = readFileSync("editable_src/panacloudconfig.json", "utf8");
const formattedConfigFile = prettier.format(data, {
parser: "json",
});
writeFileSync(
"editable_src/panacloudconfig.json",
formattedConfigFile,
"utf8"
);
stopSpinner(spinner, "Updated Panacloud Config", false);
}
}