-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.ts
More file actions
99 lines (93 loc) · 2.91 KB
/
client.ts
File metadata and controls
99 lines (93 loc) · 2.91 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
import { Command, flags } from "@oclif/command";
import chalk = require("chalk");
import {
existsSync,
readFileSync,
writeJsonSync,
} from "fs-extra";
import { startSpinner} from "../lib/spinner";
import * as validUrl from "valid-url";
import { API, APITYPE } from "../utils/constants";
import open = require("open");
import inquirer = require("inquirer");
const express = require("express");
export default class Open extends Command {
static description = "describe the command here";
static flags = {
help: flags.help({ char: "h" }),
};
static args = [{ name: "file" }];
async run() {
const {stages} = JSON.parse(
readFileSync("./editable_src/panacloudconfig.json").toString()
)
const userInput = await inquirer.prompt([
{
type: "list",
name: "stage",
message: "Select Stage",
choices: [...stages],
default: stages[0],
validate: Boolean,
},
])
const { apiType, apiName }: API = JSON.parse(
readFileSync("./codegenconfig.json").toString()
).api;
if (apiType === APITYPE.graphql) {
let API_URL;
let API_KEY;
if (!existsSync(`./cdk-${userInput.stage}-outputs.json`)) {
this.log(
chalk.red(
`${apiName}'s ${userInput.stage} stage is currently not deployed client cannot connect to API, give the command npm run deploy-${userInput.stage} to deploy it.`
)
);
} else {
let data = JSON.parse(readFileSync(`./cdk-${userInput.stage}-outputs.json`).toString());
const values: string[] = Object.values(
Object.entries(data)[0][1] as Record<string,string>
);
if (values.length === 0) {
this.log(
chalk.red(
`${apiName}'s ${userInput.stage} stage is currently not deployed client cannot connect to API, give the npm run deploy-${userInput.stage} to deploy it.`
)
);
return;
} else {
let urlPresent = false
values.forEach((val: string) => {
if (validUrl.isUri(val)) {
urlPresent = true
API_URL = val;
} else {
API_KEY = val;
}
});
if(urlPresent === false){
this.log(chalk.red("Correct url is not provided!!"))
}
if (API_URL && API_KEY) {
writeJsonSync(`./graphqlClient/data.json`, {
API_URL,
API_KEY,
});
this.runGraphqlClient();
}
}
}
}
}
async runGraphqlClient() {
const graphqlSpinner = startSpinner("Starting Grphql Client");
let app = express();
app.use(express.static(process.cwd() + "/graphqlClient/"));
let port = 8080;
app.listen(port);
open(`http://localhost:${port}`)
graphqlSpinner.stopAndPersist({
text: `Graphql client is running on http://localhost:${port} 🚀`,
});
}
}