-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstorage-handler.js
99 lines (83 loc) · 2.33 KB
/
storage-handler.js
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
'use strict';
import Debug from 'debug';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { readJSON } from '../scripts/utils.js';
const debug = new Debug('mozilla-github-watcher:Storage');
const REPO_FILE = '../allRepos.json';
const WIKI_FILE = '../wikiEdits.json';
const filePath = fileURLToPath(import.meta.url);
const repoFilePath = path.resolve(path.dirname(filePath), REPO_FILE);
const wikiFilePath = path.resolve(path.dirname(filePath), WIKI_FILE);
let savedRepos = [];
try {
savedRepos = await readJSON(REPO_FILE);
} catch (error) {
debug(`Failed to open ${repoFilePath}`);
}
let savedWikiEdits = [];
try {
savedWikiEdits = await readJSON(WIKI_FILE);
} catch (error) {
debug(`Failed to open ${wikiFilePath}`);
}
const MAX_RETURN_ROWS_NEW = 200;
/**
* Saves the latest repos to the storage
*
* @param {Array} newRepositories list of newly discovered repositories
* @return {Object} saved data
*/
export function saveRepos(newRepositories) {
debug(`start saving repos to ${repoFilePath}`);
const repos = newRepositories
.slice(0, MAX_RETURN_ROWS_NEW)
.map((repo) => {
const processedRepo = {
name: repo.name,
org: repo.owner.login,
creation_date: new Date(repo.created_at),
html_url: repo.html_url,
owner_html_url: repo.owner.html_url,
};
if (repo.description) {
processedRepo.description = repo.description.replace(/[^\x00-\x7F]/g, '');
}
return processedRepo;
});
fs.writeFileSync(repoFilePath, JSON.stringify(repos));
}
/**
* Saves the latest wiki edits to the storage
*
* @param {Array} edits list of newly discovered edits
* @return {Object} saved data
*/
export function saveWikiEdits(edits) {
debug(`start saving wiki edits to ${wikiFilePath}`);
const wikiEdits = edits.map((edit) => {
return {
name: edit.title,
user: edit.user,
change_date: new Date(edit.timestamp),
};
});
fs.writeFileSync(wikiFilePath, JSON.stringify(wikiEdits))
}
/**
* Returns the saved repos
*
* @return {Array} repos repos from the db
*/
export function getRepos() {
return savedRepos;
}
/**
* Returns the saved wiki edits
*
* @return {Array} edits edits from the db
*/
export function getWikiEdits() {
return savedWikiEdits;
}