Skip to content

Commit

Permalink
Uninstall plugin mechanisms
Browse files Browse the repository at this point in the history
Plus now passes list of default plugins through to the
server process as an environment variable.

Refs #72, simonw/datasette-app-support#12
  • Loading branch information
simonw committed Sep 11, 2021
1 parent 0359bbc commit cac7660
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
72 changes: 58 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ class DatasetteServer {
}
return args;
}
serverEnv() {
return {
DATASETTE_API_TOKEN: this.apiToken,
DATASETTE_SECRET: RANDOM_SECRET,
DATASETTE_DEFAULT_PLUGINS: Object.keys(minPackageVersions).join(" "),
};
}
async startOrRestart() {
const venv_dir = await this.ensureVenv();
await this.ensurePackagesInstalled();
Expand All @@ -165,10 +172,7 @@ class DatasetteServer {
let process;
try {
process = cp.spawn(datasette_bin, this.serverArgs(), {
env: {
DATASETTE_API_TOKEN: this.apiToken,
DATASETTE_SECRET: RANDOM_SECRET,
},
env: this.serverEnv(),
});
} catch (e) {
reject(e);
Expand Down Expand Up @@ -291,6 +295,22 @@ class DatasetteServer {
]);
}

async uninstallPlugin(plugin) {
const pip_binary = path.join(
process.env.HOME,
".datasette-app",
"venv",
"bin",
"pip"
);
await this.execCommand(pip_binary, [
"uninstall",
plugin,
"--disable-pip-version-check",
"-y",
]);
}

async packageVersions() {
const venv_dir = await this.ensureVenv();
const pip_path = path.join(venv_dir, "bin", "pip");
Expand Down Expand Up @@ -496,6 +516,25 @@ async function initializeApp() {
}
});

ipcMain.on("uninstall-plugin", async (event, pluginName) => {
try {
await datasette.uninstallPlugin(pluginName);
await datasette.startOrRestart();
dialog.showMessageBoxSync({
type: "info",
message: "Plugin uninstalled",
detail: `${pluginName} has been removed`,
});
event.reply("plugin-uninstalled", pluginName);
} catch (error) {
dialog.showMessageBoxSync({
type: "error",
message: "Error uninstalling plugin",
detail: error.toString(),
});
}
});

ipcMain.on("import-csv", async (event, database) => {
let selectedFiles = dialog.showOpenDialogSync({
properties: ["openFile"],
Expand Down Expand Up @@ -891,23 +930,28 @@ function buildMenu() {
{
label: "Run Server Manually",
click() {
const command = `DATASETTE_API_TOKEN=${
datasette.apiToken
} ${path.join(
process.env.HOME,
".datasette-app",
"venv",
"bin",
"datasette"
)} ${datasette.serverArgs().join(" ")}`;
let command = [];
for (const [key, value] of Object.entries(datasette.serverEnv())) {
command.push(`${key}="${value}"`);
}
command.push(
path.join(
process.env.HOME,
".datasette-app",
"venv",
"bin",
"datasette"
)
);
command.push(datasette.serverArgs().join(" "));
dialog
.showMessageBox({
type: "warning",
message: "Run server manually?",
detail:
"Clicking OK will terminate the Datasette server used by this app\n\n" +
"Copy this command to a terminal to manually run a replacement:\n\n" +
command,
command.join(" "),
buttons: ["OK", "Cancel"],
})
.then(async (click) => {
Expand Down
11 changes: 11 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ contextBridge.exposeInMainWorld("datasetteApp", {
link.innerHTML = `Installing ${plugin}…`;
}
},
uninstallPlugin: (plugin, link) => {
ipcRenderer.send("uninstall-plugin", plugin);
if (link) {
link.style.opacity = 0.5;
link.setAttribute("href", "#");
link.innerHTML = `Uninstalling ${plugin}…`;
}
},
onServerLog: (callback) => {
ipcRenderer.on("serverLog", callback);
},
Expand All @@ -26,3 +34,6 @@ ipcRenderer.on("csv-imported", () => {
ipcRenderer.on("plugin-installed", () => {
location.reload();
});
ipcRenderer.on("plugin-uninstalled", () => {
location.reload();
});

0 comments on commit cac7660

Please sign in to comment.