forked from lethern/Bitburner_os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall1.js
279 lines (205 loc) · 7.71 KB
/
install1.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
__ _ __ __
/ /_ (_) /_/ /_ __ ___________ ___ _____ _ ____ _____
/ __ \/ / __/ __ \/ / / / ___/ __ \/ _ \/ ___/____| | / / / / / _ \
/ /_/ / / /_/ /_/ / /_/ / / / / / / __/ / /_____/ |/ / /_/ / __/
/_.___/_/\__/_.___/\__,_/_/ /_/ /_/\___/_/ |___/\__,_/\___/
*/ /**
* CONFIGURATION
* --------------------------------------
*/
/**
* `bitburner-vue` installs to a unique subdirectory by default. To place it somewhere other than
* your root directory in BitBurner, set the prefixDirectory config as needed. Do not use a
* relative path such as './myDirectory' - always use absolute paths like '/myDirectory'
*/
let prefixDirectory = ''
/**
* --------------------------------------
* DO NOT EDIT BELOW THIS LINE
*/
let requiredHost = 'home'
let repoRoot = 'https://raw.githubusercontent.com/smolgumball/bb-vue/main'
let manifestFile = 'installManifest.txt'
let manifestTmpPath = '/tmp/installManifest__bitburner-vue.txt'
export async function main(ns) {
if (ns.getHostname() !== requiredHost) {
throw new Error('Run this script from home')
}
let manifestPath = joinPaths(repoRoot, manifestFile)
let manifestData = await fetchConfig(ns, manifestPath)
let manifestLength = manifestData.manifestPaths.length
if (prefixDirectory) prefixDirectory = `/${trimPath(prefixDirectory)}/`
for (let i in manifestData.manifestPaths) {
let { repoPath, installPath } = manifestData.manifestPaths[i]
repoPath = joinPaths(repoRoot, repoPath)
try {
installPath = joinPaths(prefixDirectory, installPath)
await getFileFromGH(ns, repoPath, installPath)
await ns.sleep(100)
await rewriteImports(ns, installPath, manifestData.importRoot, prefixDirectory)
ns.tprint(`Installed: ${installPath} [${Number(i) + 1}/${manifestLength}]`)
} catch (e) {
ns.tprint(`ERROR: Exception while downloading ${repoPath}: `, e.message)
throw e
}
}
ns.rm(manifestTmpPath, requiredHost)
let mainJsPath = joinPaths(prefixDirectory, manifestData.entryFile)
// prettier-ignore
ns.tprint(`Install complete! 🎉
Run the following in your home terminal to launch bitburner-vue:
run ${mainJsPath}
`)
}
async function rewriteImports(ns, filePath, importRoot, prefixDirectory) {
let file = ns.read(filePath)
file = file.replaceAll(`from '${importRoot}`, `from '${joinPaths(prefixDirectory, importRoot)}`)
file = file.replaceAll(`from "${importRoot}`, `from "${joinPaths(prefixDirectory, importRoot)}`)
file = file.replaceAll(`from \`${importRoot}`, `from \`${joinPaths(prefixDirectory, importRoot)}`)
await ns.write(filePath, file, 'w')
}
async function fetchConfig(ns, manifestPath) {
try {
await getFileFromGH(ns, manifestPath, manifestTmpPath)
return JSON.parse(ns.read(manifestTmpPath))
} catch (e) {
ns.tprint(`ERROR: Downloading and reading config file failed ${manifestPath}`)
throw e
}
}
async function getFileFromGH(ns, repoPath, installPath) {
await githubReq(ns, repoPath, installPath)
}
async function githubReq(ns, repoPath, installPath) {
if (isScriptFile(installPath)) {
ns.print('Cleanup on: ' + installPath)
await ns.scriptKill(installPath, requiredHost)
await ns.rm(installPath, requiredHost)
}
ns.print('Request to: ' + repoPath)
await ns.sleep(100)
await ns.wget(repoPath, installPath, requiredHost)
}
// Path helpers
// ---
function joinPaths(pathA, pathB) {
return `${trimTrailingSlash(pathA)}/${trimLeadingSlash(pathB)}`
}
function trimPath(path) {
return `${trimTrailingSlash(trimLeadingSlash(path))}`
}
function trimLeadingSlash(path) {
if (path && path.startsWith('/')) {
return path.slice(1)
}
return path
}
function trimTrailingSlash(path) {
if (path && path.endsWith('/')) {
return path.slice(0, -1)
}
return path
}
// Reflection helpers
// ---
function isScriptFile(path) {
return path.endsWith('ns') || path.endsWith('js')
}
// Installer script forked from:
// https://github.com/lethern/Bitburner_git_fetch
//-------------------------------------------------------------------------------------------------------
// That's the code that was in the original os-install script
//let gitUsername = 'lethern';
//let repoName = 'Bitburner_os';
//let branchName = 'main';
//let json_filename = 'install_files_json.txt';
/** @param {NS} ns */
//export async function main(ns) {
// let filesToDownload = await init(ns);
// await downloadFiles(ns, filesToDownload);
// terminalCommand('alias -g bootOS="run /os/main.js"')
// ns.tprintf("Install complete! To start, type: bootOS")
//}
//async function init(ns){
// if(ns.args[0] == 'dev'){
// branchName = 'lethern-dev';
// ns.tprint("Dev branch");
// }else if(ns.args.length > 0){
// throw 'Invalid argument(s), expected "dev"';
// }
// let { welcomeLabel, filesToDownload } = await fetchConfig(ns)
// ns.tprintf("%s", welcomeLabel)
// if (ns.getHostname() !== 'home') {
// throw 'Run the script from home'
// }
// await clean(ns, filesToDownload);
// return filesToDownload;
//}
//async function downloadFiles(ns, filesToDownload){
// let count = 0;
// for (let filename of filesToDownload) {
// const path = getBaseUrl() + filename
// const save_filename = (!filename.startsWith('/') && filename.includes('/')) ? '/' + filename : filename;
// try {
// await ns.scriptKill(save_filename, 'home')
// await ns.rm(save_filename)
// await ns.sleep(20)
// ns.print('wget '+path+' -> '+save_filename);
// await ns.wget(path + '?ts=' + new Date().getTime(), save_filename)
// if (++count % 5 == 0) {
// ns.tprintf(`Installing... [${(count + '').padStart(2)}/${filesToDownload.length}]`);
// }
// } catch (e) {
// ns.tprint(`ERROR (tried to download ${path})`)
// throw e;
// }
// }
//}
//function getBaseUrl(){
// return `https://raw.githubusercontent.com/${gitUsername}/${repoName}/${branchName}/`;
//}
//async function clean(ns, filesToDownload) {
// let filesRaw = filesToDownload.map(file => file.substr(file.lastIndexOf('/') + 1))
// let allFiles = ns.ls("home");
// let toDelete = [];
// allFiles.forEach(_file => {
// let file = (_file.startsWith('/')) ? _file.substr(1) : _file;
// if (file.startsWith('os/')) {
// let file_raw = file.substr(file.lastIndexOf('/') + 1);
// if (filesRaw.includes(file_raw)) {
// if (!filesToDownload.includes(file)) {
// toDelete.push(_file);
// }
// } else {
// console.log("Install-clean: unidentified file", file);
// }
// }
// })
// if (toDelete.length) {
// if (await ns.prompt("Files have moved. Installer will clean old files. Confirm? [recommended] " + toDelete.join(", "))) {
// toDelete.forEach(file => ns.rm(file));
// }
// }
//}
//async function fetchConfig(ns) {
// try {
// let save_filename = '/os/' + json_filename;
// await ns.rm(save_filename)
// let path = getBaseUrl() + json_filename;
// ns.print('wget '+path+' -> '+save_filename);
// await ns.wget(path + '?ts=' + new Date().getTime(), save_filename)
// return JSON.parse(ns.read(save_filename));
// } catch (e) {
// ns.tprint(`ERROR: Downloading and reading config file failed ${json_filename}`);
// throw e;
// }
//}
//function terminalCommand(message) {
// const docs = globalThis['document']
// const terminalInput = /** @type {HTMLInputElement} */ (docs.getElementById("terminal-input"));
// terminalInput.value = message;
// const handler = Object.keys(terminalInput)[1];
// terminalInput[handler].onChange({ target: terminalInput });
// terminalInput[handler].onKeyDown({ key: 'Enter', preventDefault: () => null });
//}