/
hooks.ts
127 lines (105 loc) · 3.53 KB
/
hooks.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { getUserInfoFromToken } from "./apiUtils";
import { getAppConfig, initializeAppConfig, setAppConfig } from "./appConfig";
import { getUserInfo, setUserInfo } from "../state/userInfo";
import {
resolveBackendServerUrl,
resolveFrontendServerUrl,
} from "../utils/utils";
import axios from "axios";
import cli from "cli-ux";
import chalk from "chalk";
import { isUserLoggedIn } from ".";
/*
Crusher secret invite code. Don't share it with anyone🤫
*/
const secretInviteCode = "crush"
/*
Remove this after beta
*/
const checkForDiscord = async()=>{
const isCodeInCommandLine = process.argv.some((e)=>{
return e.includes("--code=") && !["help", "--help", "-h"].includes(e)
})
const hasLoginFlag = process.argv.some((e) => e.includes("--login"));
if(isUserLoggedIn() || isCodeInCommandLine) return;
if(!isCodeInCommandLine && !hasLoginFlag){
await cli.log(chalk.green(`New to crusher?`))
await cli.log(`Get access code - ${chalk.green("https://discord.gg/sWbWNYWv")}`)
await cli.log(`1.) Get access code on home screen`)
await cli.log(`2.) Run command with access code`)
await cli.log(`\n${chalk.yellow('Already have an account?')}
run npx crusher-cli --login \n`)
process.exit(0)
}
}
const parseDiscordFlag = (flag: string) => {
if(!flag) return null;
return flag.split("--code=")[1];
};
const waitForUserLogin = async (): Promise<string> => {
await checkForDiscord();
// ask for discord code here?
const discordCode = parseDiscordFlag(process.argv.find((i)=>{
return i.includes("--code=");
}));
const loginKey = await axios
.get(resolveBackendServerUrl("/cli/get.key"))
.then((res) => {
return res.data.loginKey;
});
const loginUrl = resolveFrontendServerUrl(`/login_sucessful?lK=${loginKey}&inviteCode=${discordCode}`);
await cli.log(
"Login or create an account to create/sync tests⚡⚡. Opening a browser to sync test.\nOr open this link:"
);
await cli.log(`${loginUrl} \n`);
await cli.action.start("Waiting for login");
await cli
.open(loginUrl)
.catch((err) => {
console.error(err);
});
const token = await new Promise((resolve) => {
const interval = setInterval(async () => {
const loginKeyStatus = await axios
.get(resolveBackendServerUrl(`/cli/status.key?loginKey=${loginKey}`))
.then((res) => res.data);
if (loginKeyStatus.status === "Validated") {
clearInterval(interval);
resolve(loginKeyStatus.userToken);
}
}, 5000);
});
await cli.action.stop();
await cli.log("\nLogin completed! Let's ship high quality software fast⚡⚡");
return token as string;
};
const loadUserInfoOnLoad = async function (options: { token?: string }) {
initializeAppConfig();
if (options.token) {
// Verify the new token and save it if valid
try {
await getUserInfoFromToken(options.token).then((userInfo) => {
setUserInfo(userInfo);
});
} catch (e) {
const userToken = await waitForUserLogin();
await getUserInfoFromToken(userToken).then((userInfo) => {
setUserInfo(userInfo);
});
}
} else {
const appConfig = getAppConfig();
// Login user to set default auth token
if (!appConfig.userInfo || !appConfig.userInfo.token) {
const userToken = await waitForUserLogin();
await getUserInfoFromToken(userToken).then((userInfo) => {
setUserInfo(userInfo);
setAppConfig({
...getAppConfig(),
userInfo: getUserInfo(),
});
});
}
}
};
export { loadUserInfoOnLoad };