-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
101 lines (86 loc) · 2.99 KB
/
Copy pathhelpers.js
File metadata and controls
101 lines (86 loc) · 2.99 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
100
101
import { join } from "./deps.js";
import { API as StudioAPI } from "./api_studio.js";
import { Select } from "./deps.js";
export function getConfigPaths() {
const homeDir = Deno.build.os == "windows"
? Deno.env.get("USERPROFILE")
: Deno.env.get("HOME");
const configDir = Deno.env.get("DENO_DIR")
? join(Deno.env.get("DENO_DIR"), ".tictapp")
: join(homeDir, ".tictapp");
return {
configDir,
loginPath: join(configDir, "login"),
updatePath: join(configDir, "update"),
};
}
export async function getLogin() {
const { loginPath } = getConfigPaths();
// Try to read the json file.
const loginInfoJson = await Deno.readTextFile(loginPath).catch((error) => {
if (error.name == "NotFound") return null;
console.error(error);
});
if (loginInfoJson) {
return JSON.parse(loginInfoJson)
}
return null
}
export async function getJson(filePath) {
return JSON.parse(await Deno.readTextFile(filePath));
}
export async function exists(path) {
try {
return (await Deno.stat(path)).isFile;
} catch {
return false;
}
}
export async function writeJson(filePath, o) {
await Deno.writeTextFile(filePath, JSON.stringify(o, null, 4));
}
export async function selectProject() {
const TOKEN = Deno.env.get('TOKEN')
const studio_api = StudioAPI.fromToken(TOKEN)
const studio_projects = await studio_api.requestJson(`/projects`)
if (studio_projects.error) {
console.log(studio_projects)
Deno.exit()
}
// console.table(studio_projects.map(o => ({ name: o.name, ref: o.ref, url: `https://${o.ref}.tictapp.io`, updated: o.updated_at })))
// PROJECT_REF = prompt(`Enter project ref`)
return await Select.prompt({
message: "Select project to continue",
search: true,
options: studio_projects.map(o => ({ name: `${o.ref} - ${o.name} (${o.status})`, value: o.ref, url: `https://${o.ref}.tictapp.io`, updated: o.updated_at })),
});
}
export async function selectVercelProject() {
const res = await fetch(
'https://api.vercel.com/v6/projects',
{
method: 'GET',
headers: {
Authorization: `Bearer ${Deno.env.get("VERCEL_ACCESS_TOKEN")}`,
}
}
);
const result = await res.json()
//console.log(result)
const projects = result.projects.map(({ name, link, latestDeployments, updatedAt, id, teamId }) => {
const deployments = latestDeployments.map(({ url, target, alias }) => {
return {
url,
target,
alias
}
})
const latestDeployment = deployments.shift()
return { name, link, latestDeployment, updatedAt, id, teamId }
})
return await Select.prompt({
message: "Select project to continue",
search: true,
options: projects.map(o => ({ name: `${o.name} - https://${o.latestDeployment.alias.shift()}`, value: o.name })),
});
}