-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
48 lines (36 loc) · 1.42 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
41
42
43
44
45
46
47
48
import { ReadableApp } from "@scramjet/types";
import { chunksToLines } from "./lib/utils";
import { GitClient } from "./lib/gitClient";
const app: ReadableApp<string> = async function* (input) {
const path = this.config.path as string;
const branch = this.config.branch as string;
const repository = this.config.repository as string;
const token = this.config.token as string;
this.logger.debug(`Creating GitClient`);
const gitClient = new GitClient(repository, token);
for await (const line of chunksToLines(input)) {
let previousVerionSHA;
try {
this.logger.debug(`Trying to get ${path} file contents`);
const previousVersion = await gitClient.getContents(path, branch);
previousVerionSHA = previousVersion?.data?.sha;
this.logger.info(`Previous version SHA ${previousVerionSHA}`);
} catch (err) {
this.logger.error(
"Error while retriving current contents. File may not exist yet. Skipping previous content SHA.",
err
);
}
try {
this.logger.debug(`Saving json to github repository file ${path}`);
const content = Buffer.from(JSON.stringify(JSON.parse(line)))
.toString("base64")
.trim();
await gitClient.putContents(path, content, previousVerionSHA, branch);
this.logger.info(`Object have been saved!`);
} catch (err) {
this.logger.error("Error while saving", err);
}
}
};
export default app;