-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdk.ts
63 lines (57 loc) · 1.72 KB
/
sdk.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
import { exec } from 'child_process';
import { createSDK, encodeCommitChanges } from 'filemap/sdk';
import keytar from "keytar"
const APP_NAME = "fm-server-driver";
const STANDALONE_KEYTAR_KEY = "keykey";
const { sdk, options, waitForAuth } = createSDK({
enableOperationQueue: true,
fetchTokens: async () => {
const accessToken = await keytar.getPassword(
APP_NAME,
STANDALONE_KEYTAR_KEY,
);
return { accessToken: accessToken || undefined };
},
afterInit: async () => {
if (!options.accessToken) {
return;
}
try {
await keytar.setPassword(
APP_NAME,
STANDALONE_KEYTAR_KEY,
options.accessToken,
);
} catch {
console.warn(
'keychain error while setting access token in keytar',
);
}
},
});
async function initStandaloneSDK() {
try {
await sdk.fetchAuthenticatedUser();
} catch {
const { loginUrl, wait } = await sdk.loginWithChallenge(
'desktop_browser',
100_000,
);
console.log('Please click url', loginUrl);
const cmd =
process.platform === 'darwin'
? 'open'
: process.platform === 'win32'
? 'start'
: 'xdg-open';
const url =
process.platform === 'darwin'
? loginUrl.replace('&', '\\&')
: process.platform === 'win32'
? loginUrl.replace('&', '^&')
: loginUrl;
exec(`${cmd} ${url}`);
await wait();
}
}
export{ initStandaloneSDK as initSdk, sdk, encodeCommitChanges };