-
Notifications
You must be signed in to change notification settings - Fork 2
/
credentialStore.ts
41 lines (35 loc) · 1.27 KB
/
credentialStore.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
import chalk from "chalk";
import * as keytar from 'keytar';
export interface Credentials {
account: string;
password: string;
}
const SERVICE_NAME = 'youtrack-cli';
export class CredentialStore {
public static get(): Promise<Credentials | null> {
return keytar.findCredentials(SERVICE_NAME).then((credentials) => {
if (credentials.length > 0) {
return credentials[0];
}
return null;
});
}
public static set(username: string, password: string): Promise<void> {
return keytar.findCredentials(SERVICE_NAME).then((credentials) => {
return Promise.all(credentials.map((credential) => {
keytar.deletePassword(SERVICE_NAME, credential.account);
})).then(() => {
return keytar.setPassword(SERVICE_NAME, username, password);
});
}).catch(() => {
return keytar.setPassword(SERVICE_NAME, username, password);
});
}
public static async ensureCredentialsPresent() {
const credentials = await CredentialStore.get();
if (!credentials) {
console.error('please setup youtrack-cli first, run: ' + chalk.yellow('$ youtrack setup'));
process.exit(1);
}
}
}