MCPcopy Create free account
hub / github.com/Botloader/botloader / syncWorkspace

Method syncWorkspace

botloader-vscode/src/guildspace.ts:341–421  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

339 // tries to be as non destructive as possible
340 // will only overwrite files unchaged against the old index
341 async syncWorkspace() {
342 console.log("Syncing workspace");
343 let index = await this.readIndexFile();
344
345 // first make a list of files identical to their index variant
346 let filesWorking = await vscode.workspace.fs.readDirectory(this.folder);
347 filesWorking = filesWorking.filter(f => f[0].endsWith(".ts"));
348
349 // holds the current working files identical to the index files
350 // will are safe to overwrite these
351 let identicalIndexFiles = await this.getIdenticalIndexFiles(filesWorking.map(e => e[0]));
352 // strip .ts suffix for convenience
353 let identicalIndexNames = identicalIndexFiles.map(e => e.slice(0, e.length - 3));
354
355 let resp = await this.apiClient.getAllScripts(index.guild.id);
356 if (isErrorResponse(resp)) {
357 throw new Error("failed fetching scripts");
358 }
359
360 // last resort security check, see checkValidName for more info
361 const wsFolder = this.folder;
362 resp = resp.filter(script => checkValidName(wsFolder, script.name));
363
364 // nuke old index
365 await vscode.workspace.fs.delete(vscode.Uri.joinPath(this.folder, "/.botloader/scripts"), {
366 recursive: true,
367 useTrash: false,
368 });
369
370 // create new scripts index
371 let textEncoder = new TextEncoder();
372 for (let script of resp) {
373 await vscode.workspace.fs.writeFile(vscode.Uri.joinPath(this.folder, `/.botloader/scripts/${script.name}.ts.bloader`), textEncoder.encode(script.original_source));
374 }
375
376 if (resp.length < 1) {
377 // ensure the script folder is there
378 await vscode.workspace.fs.createDirectory(vscode.Uri.joinPath(this.folder, "/.botloader/scripts"));
379 }
380
381 let newOpenScripts = resp.map(s => {
382 return {
383 id: s.id,
384 name: s.name,
385 };
386 });
387
388 // write the new index
389 await vscode.workspace.fs.writeFile(vscode.Uri.joinPath(this.folder, `/.botloader/index.json`), textEncoder.encode(JSON.stringify({
390 guild: index.guild,
391 openScripts: newOpenScripts,
392 })));
393
394 // create new files
395 let newScripts = resp.filter(e => !index.openScripts.some(os => e.id === os.id));
396 for (let script of newScripts) {
397 let uri = vscode.Uri.joinPath(this.folder, `/${script.name}.ts`);
398 try {

Callers 5

constructorMethod · 0.95
pushUriMethod · 0.95
pushAllMethod · 0.95
pushScmGroupMethod · 0.95

Calls 8

readIndexFileMethod · 0.95
initialScanMethod · 0.95
isErrorResponseFunction · 0.85
checkValidNameFunction · 0.85
logMethod · 0.80
getAllScriptsMethod · 0.80
deleteMethod · 0.45

Tested by

no test coverage detected