-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit_fetch.js
More file actions
67 lines (53 loc) · 1.86 KB
/
git_fetch.js
File metadata and controls
67 lines (53 loc) · 1.86 KB
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
// from https://github.com/lethern/Bitburner_git_fetch
// if your github is https://github.com/Bob/Bitburner_lib, owner is Bob and repo is Bitburner_lib
let owner = "git-username-here";
let repo = "my-repo-name";
// if you want your files to be saved nested in a directory, type it here. Or leave it empty
let prefixDirectory = '';
// probably no changes here
let configFileName = 'git_fetch.txt';
let baseURL = 'https://raw.githubusercontent.com/';
let branch = 'main';
export async function main(ns) {
let { filesToDownload } = await fetchConfig(ns);
if (ns.getHostname() !== 'home') {
throw new Error('Run the script from home');
}
if(prefixDirectory){
if(!prefixDirectory.endsWith('/')) prefixDirectory += '/';
if(prefixDirectory[0] !== '/') prefixDirectory = '/' + prefixDirectory;
}
for (let i in filesToDownload) {
let filename = filesToDownload[i];
try {
await getFileFromGH(ns, filename);
ns.tprint(`Installed: ${filename} [${Number(i)+1}/${filesToDownload.length}]`);
} catch (e) {
ns.tprint(`ERROR: tried to download ${filename}: `, e.message);
throw e;
}
}
ns.tprint("Install complete!");
}
async function fetchConfig(ns) {
try {
await getFileFromGH(ns, configFileName);
let json = ns.read(configFileName);
return JSON.parse(json);
}catch(e){
ns.tprint(`ERROR: Downloading and reading config file failed ${configFileName}`);
throw e;
}
}
async function getFileFromGH(ns, filepath) {
let saveFilepath = prefixDirectory + filepath;
await ns.scriptKill(saveFilepath, 'home')
await ns.rm(saveFilepath)
await ns.sleep(20)
await githubReq(ns, filepath, saveFilepath);
}
async function githubReq(ns, filepath, saveFilepath) {
let url = baseURL + owner + "/" + repo + "/" + branch + "/" + filepath;
ns.print("Request to: "+url);
await ns.wget(url, saveFilepath)
}