-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
111 lines (96 loc) · 2.89 KB
/
index.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
103
104
105
106
107
108
109
110
111
import { Flags } from "@oclif/core";
import { BaseCommand } from "../../base";
import { Deepgram } from "@deepgram/sdk";
import { select, confirm } from "@inquirer/prompts";
import { homedir } from "os";
import { open } from "fs/promises";
export default class Setup extends BaseCommand<typeof Setup> {
static description = "Setup the CLI using a Deepgram API key.";
static flags = {
key: Flags.string({
char: "k",
env: "DEEPGRAM_API_KEY",
description: "An API key provided by Deepgram.",
summary: "https://dpgr.am/api-key",
required: false,
prompt: true,
inquirer: "password",
}),
scopes: Flags.string({
char: "s",
env: "DEEPGRAM_API_SCOPES",
description: "Comma separated string of Deepgram API scopes.",
default: "member",
summary: "https://dpgr.am/scopes",
required: false,
}),
ttl: Flags.integer({
char: "t",
env: "DEEPGRAM_API_TTL",
description:
"How many seconds you should remain logged in with the Deepgram CLI. Default: 86400",
default: 86400,
summary: "https://dpgr.am/ttl",
required: false,
}),
};
public async run(): Promise<void> {
let { key: auth, scopes, ttl } = this.parsedFlags;
const dg = new Deepgram(auth);
const {
projects: [project],
} = await dg.projects.list().catch((err) => this.error(err));
if (!scopes) {
scopes = ["member"];
}
if (typeof scopes === "string") {
scopes = this.parsedFlags["scopes"].split(",");
}
const { key } = await dg.keys.create(
project.project_id,
"Deepgram CLI",
scopes,
{
timeToLive: ttl,
tags: ["cli"],
}
);
const filePath = `${homedir()}/.deepgramrc`;
let file;
/**
* Open a file handler if one doesn't exist.
*/
file = await open(filePath, "wx").catch((err) => {
if (err.code === "EEXIST") {
this.log(`Existing config file '${filePath}' detected.`);
return; // Return without killing the process.
}
this.error(err);
});
/**
* If one does exist, prompt the user to overwrite it.
*/
if (!file) {
const overwritePrompt = await confirm({
message: `Overwrite it existing config file?`,
});
if (!overwritePrompt) {
this.error(
`Config file '${filePath}' already existed. Use cancelled overwrite.`
);
}
this.log(`Overwriting the existing config file '${filePath}'`);
file = await open(filePath, "w").catch((err) => this.error(err));
}
const now = Math.floor(Date.now() / 1000);
const configBody = {
key,
project: project.project_id,
scopes,
expires: now + ttl,
};
const data = Buffer.from(JSON.stringify(configBody));
await file.write(data).catch((err) => this.error(err));
this.log(`Config file created at '${filePath}'`);
}
}