-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
40 lines (35 loc) · 1.11 KB
/
index.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
import fs from "fs";
import path from "path";
import { prepareToRecordData } from "./lib/spotify/recordData";
import { CallbackServer } from "./lib/spotifyAuth/callbackServer";
import {
createAuthUrl,
getAccessTokenAndSave,
} from "./lib/spotifyAuth/spotifyAuth";
import { urlToQueryStrings } from "./lib/utils/parseUrl";
const server = new CallbackServer();
let spotifyKeysJsonPath;
if (process.env.TS_NODE_DEV == "true") {
spotifyKeysJsonPath = "./spotifyKeys.json";
} else {
spotifyKeysJsonPath = path.join(__dirname, "..", "spotifyKeys.json");
}
if (fs.existsSync(spotifyKeysJsonPath)) {
prepareToRecordData();
} else {
console.log(createAuthUrl().toString());
server.startServer(async (response) => {
if (response.url) {
const queryStrings = urlToQueryStrings(response.url);
if (queryStrings["/callback?code"] && queryStrings["state"]) {
await getAccessTokenAndSave(
queryStrings["/callback?code"].toString(),
queryStrings["state"].toString()
);
prepareToRecordData();
} else {
console.error("Callback URL is invalid");
}
}
});
}