Skip to content

Commit

Permalink
Update local-install to create $HOME/.foxglove-studio folder if sna…
Browse files Browse the repository at this point in the history
…p is not present (#99)

In #91 we updated the install logic to install correctly for snap users.
In that logic we check for the presence of specific directories before
installing (whereas before we would always make the directory). Since
then we've learned that debian package users don't actually have a
`$HOME/.foxglove-studio/extensions` directory prior to actually
installing an extension. This meant that the `local-install` script
would fail to install a developer's extension locally unless they had
installed one via the marketplace or made the extensions dir manually.

This change updates the `install` command to pro-actively create the
`$HOME/.foxglove-studio/extensions` directory if it does not exist. To
avoid creating this directory for _snap_ users, we first check for the
presence of the _snap/foxglove-studio/current_ directory which tells us
if a snap is installed for foxglove-studio. If the snap is used then we
only install in the snap directory.

We'll see how this holds up to other create ways folks expected the
local install command to work.

Fixes: #98
  • Loading branch information
defunctzombie authored Mar 31, 2023
1 parent 44f4002 commit 97586d5
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import rimraf from "rimraf";
import { promisify } from "util";

import { getPackageDirname, getPackageId, parsePackageName } from "./extensions";
import { fatal, info } from "./log";
import { info } from "./log";

const cpR = promisify(ncp);

Expand Down Expand Up @@ -279,34 +279,35 @@ async function install(
process.chdir(extensionPath);

const dirName = getPackageDirname(pkg);
const defaultStudioDir = join(homedir(), ".foxglove-studio");
const snapStudioDir = join(homedir(), "snap", "foxglove-studio", "current", ".foxglove-studio");

let installed = false;
for (const studioDir of [defaultStudioDir, snapStudioDir]) {
try {
if (!(await isDirectory(studioDir))) {
continue;
}
} catch (_err) {
continue;
}

const destDir = join(studioDir, "extensions", dirName);
await rimraf(destDir);
await mkdir(destDir, { recursive: true });

info(`Copying files to ${destDir}`);
for (const file of files) {
const target = join(destDir, file);
info(` - ${file} -> ${target}`);
await cpR(file, target, { stopOnErr: true });
}
installed = true;
// The snap package does not use the regular _home_ directory but instead uses a separate snap
// application directory to limit filesystem access.
//
// We look for this app directory as a signal that the user installed the snap package rather than
// the deb package. If we detect a snap installation directory, we install to the snap path and
// exit.
const snapAppDir = join(homedir(), "snap", "foxglove-studio", "current");
if (await isDirectory(snapAppDir)) {
info(`Detected snap install at ${snapAppDir}`);
const extensionDir = join(snapAppDir, ".foxglove-studio", "extensions", dirName);
await copyFiles(files, extensionDir);
return;
}

if (!installed) {
fatal(`Failed to install extension: Unable to detect Studio installation.`);
// If there is no snap install present then we install to the home directory
const defaultExtensionDir = join(homedir(), ".foxglove-studio", "extensions", dirName);
await copyFiles(files, defaultExtensionDir);
}

async function copyFiles(files: string[], destDir: string): Promise<void> {
await rimraf(destDir);
await mkdir(destDir, { recursive: true });

info(`Copying files to ${destDir}`);
for (const file of files) {
const target = join(destDir, file);
info(` - ${file} -> ${target}`);
await cpR(file, target, { stopOnErr: true });
}
}

Expand All @@ -328,7 +329,12 @@ async function pathExists(filename: string, fileType: FileType): Promise<boolean
}

async function isDirectory(pathname: string): Promise<boolean> {
return (await stat(pathname)).isDirectory();
try {
return (await stat(pathname)).isDirectory();
} catch (_) {
// ignore any error from stat and assume not a directory
}
return false;
}

async function addDirToZip(zip: JSZip, baseDir: string, dirname: string): Promise<void> {
Expand Down

0 comments on commit 97586d5

Please sign in to comment.