Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,34 @@ export const activate: (context: ExtensionContext) => any = (context: ExtensionC
copyFileSync(workbench, workbench_backup);
copyFileSync(product, product_backup);
}catch(err: any){
window.showWarningMessage("Failed to backup files, run command as administrator?", {detail: `The Background extension does not have permission to backup to the VSCode folder, run command using administrator permissions?\n\n${err.message}`, modal: true}, "Yes").then((value?: string) => {
if(value === "Yes"){
const command: string = copyCommand([
[workbench, workbench_backup],
[product, product_backup]
]);
exec(command, {name: "VSCode Extension Host"}, (err?: Error) => {
if(err)
window.showErrorMessage(
"Failed to backup files",
{
detail: `OS: ${platform()} ${release()}\nUsing command: ${command}\n\n${err.name}\n${err.message}`.trim(),
modal: true
}
);
});
}else
window.showWarningMessage("Background extension is running without backup files");
});
const snap: boolean = platform() === "linux" &&
/* also in */ workbench.toString().replace(/\\/g, '/').includes("/snap/") &&
/* writer.ts */ product.toString().replace(/\\/g, '/').includes("/snap/");

if(snap){
window.showErrorMessage("Background extension does not support snap installations, use deb or rpm");
return;
}else{
window.showWarningMessage("Failed to backup files, run command as administrator?", {detail: `The Background extension does not have permission to backup to the VSCode folder, run command using administrator permissions?\n\n${err.message}`, modal: true}, "Yes").then((value?: string) => {
if(value === "Yes"){
const command: string = copyCommand([
[workbench, workbench_backup],
[product, product_backup]
]);
exec(command, {name: "VSCode Extension Host"}, (err?: Error) => {
if(err)
window.showErrorMessage(
"Failed to backup files",
{
detail: `OS: ${platform()} ${release()}\nUsing command: ${command}\n\n${err.name}\n${err.message}`.trim(),
modal: true
}
);
});
}else
window.showWarningMessage("Background extension is running without backup files");
});
}
}
}
}
Expand Down
82 changes: 45 additions & 37 deletions src/extension/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,66 +21,74 @@ import { window } from "vscode";
import { platform, release } from "os";
import { exec } from "@vscode/sudo-prompt";

import { PathOrFileDescriptor, readFileSync, writeFileSync } from "fs";
import { PathLike, readFileSync, writeFileSync } from "fs";

import { reload } from "../lib/vscode";
import { copyCommand, generateChecksum } from "../lib/file";

import { clean, inject } from "./inject"

export const install: (workbench: PathOrFileDescriptor, product: PathOrFileDescriptor) => void = (workbench: PathOrFileDescriptor, product: PathOrFileDescriptor) => {
export const install: (workbench: PathLike, product: PathLike) => void = (workbench: PathLike, product: PathLike) => {
write(workbench, product, inject(readFileSync(workbench, "utf-8")));
}

export const uninstall: (workbench: PathOrFileDescriptor, product: PathOrFileDescriptor) => void = (workbench: PathOrFileDescriptor, product: PathOrFileDescriptor) => {
export const uninstall: (workbench: PathLike, product: PathLike) => void = (workbench: PathLike, product: PathLike) => {
write(workbench, product, clean(readFileSync(workbench, "utf-8")));
}

// write

const workbenchChecksum: RegExp = /(?<=^\s*"vs\/workbench\/workbench\.desktop\.main\.js\": \").*(?=\",\s*$)/gm;

const write: (workbench: PathOrFileDescriptor, product: PathOrFileDescriptor, content: string) => void = (workbench: PathOrFileDescriptor, product: PathOrFileDescriptor, content: string) => {
const write: (workbench: PathLike, product: PathLike, content: string) => void = (workbench: PathLike, product: PathLike, content: string) => {
const pJson: string = readFileSync(product, "utf-8").replace(workbenchChecksum, generateChecksum(content));

try{ // write changes
writeFileSync(workbench, content, "utf-8");
writeFileSync(product, pJson, "utf-8");
reload();
}catch(error: any){
window.showWarningMessage(
"Failed to write changes, run command as administrator?",
{
detail: `The Background extension does not have permission to write changes, run command using administrator permissions?\n\n${error.message}`,
modal: true
},
"Yes"
).then((value?: string) => {
if(value === "Yes"){
// use temp files
const workbenchTemp = fileSync().name;
writeFileSync(workbenchTemp, content, "utf-8");
const productTemp = fileSync().name;
writeFileSync(productTemp, pJson, "utf-8");
const snap: boolean = platform() === "linux" &&
/* also in */ workbench.toString().replace(/\\/g, '/').includes("/snap/") &&
/* extension.ts */ product.toString().replace(/\\/g, '/').includes("/snap/");

// sudo copy
const command: string = copyCommand([
[workbenchTemp, workbench],
[productTemp, product]
]);
exec(command, { name: "VSCode Extension Host" }, (err: any) => {
if(err)
window.showErrorMessage(
"Failed to write changes",
{
detail: `OS: ${platform()} ${release()}\nUsing command: ${command}\n\n${err.name}\n${err.message}`.trim(),
modal: true
}
);
else
reload();
});
}
})
if(snap){
window.showErrorMessage("Background extension does not support snap installations, use deb or rpm");
}else{
window.showWarningMessage(
"Failed to write changes, run command as administrator?",
{
detail: `The Background extension does not have permission to write changes, run command using administrator permissions?\n\n${error.message}`,
modal: true
},
"Yes"
).then((value?: string) => {
if(value === "Yes"){
// use temp files
const workbenchTemp = fileSync().name;
writeFileSync(workbenchTemp, content, "utf-8");
const productTemp = fileSync().name;
writeFileSync(productTemp, pJson, "utf-8");

// sudo copy
const command: string = copyCommand([
[workbenchTemp, workbench],
[productTemp, product]
]);
exec(command, { name: "VSCode Extension Host" }, (err: any) => {
if(err)
window.showErrorMessage(
"Failed to write changes",
{
detail: `OS: ${platform()} ${release()}\nUsing command: ${command}\n\n${err.name}\n${err.message}`.trim(),
modal: true
}
);
else
reload();
});
}
});
}
}
}