-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
105 lines (87 loc) · 2.77 KB
/
utils.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
import {join} from "https://deno.land/std/path/mod.ts"
import {ensureDirSync} from "https://deno.land/std@0.224.0/fs/ensure_dir.ts"
const CONFIG_DIR = join(Deno.env.get("HOME") || ".", 'serverlessScout')
const CONFIG_FILE = join(Deno.env.get("HOME") || ".", "serverlessScout", "config.json")
const LOG_FILE = join(Deno.env.get("HOME") || ".", "serverlessScout", "log.json")
export async function getStringFromStringIterator(iterator: AsyncIterable<string>) {
let data = ""
for await (const chunk of iterator) {
data += chunk
}
return data
}
export function RunCommand(command: string): void {
console.log(`Executing command: ${command}`)
// Here you would implement the actual command execution logic
// For now, we'll just log the command
}
export type CurrentChat = {
systemPrompt: string
modelId: string
}
interface Config {
anthropicKey: string
awsProfile: string
awsRegion: string,
currentChat?: CurrentChat
}
export async function WriteConfig(config: Config): Promise<void> {
try {
// await Deno.mkdir(CONFIG_DIR, {recursive: true})
ensureDirSync(CONFIG_DIR)
await Deno.writeTextFile(CONFIG_FILE, JSON.stringify(config, null, 2))
} catch (error) {
console.log(error)
throw new Error(`Failed to write config: ${error instanceof Error ? error.message : String(error)}`)
}
}
export function UpdateCurrentChat(chat: CurrentChat) {
const config = ReadConfig()
config.currentChat = chat
WriteConfig(config)
}
export function ReadConfig(): Config {
try {
const configStr = Deno.readTextFileSync(CONFIG_FILE)
return JSON.parse(configStr) as Config
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
throw new Error("Config file not found. Please run writeConfig first.")
}
throw new Error(`Failed to read config: ${error instanceof Error ? error.message : String(error)}`)
}
}
export async function listStackResources(
stackName: string,
profile: string,
region: string,
): Promise<any> {
const command = new Deno.Command("aws", {
args: [
"cloudformation",
"list-stack-resources",
"--stack-name",
stackName,
"--profile",
profile,
"--region",
region,
],
stdout: "piped",
stderr: "piped",
})
const {code, stdout, stderr} = await command.output()
if (code !== 0 || stderr.length > 0) {
throw new Error(new TextDecoder().decode(stderr))
}
return JSON.parse(new TextDecoder().decode(stdout))
}
export function writeJsonToFile(directory: string, fileName: string, data: any): void {
const projectDir = `${CONFIG_DIR}/${directory}`
ensureDirSync(projectDir)
Deno.writeTextFileSync(`${projectDir}/${fileName}`, JSON.stringify(data, null, 2))
}
export function getLogFileName(): string {
ensureDirSync(CONFIG_DIR)
return LOG_FILE
}