Skip to content

Commit

Permalink
add some more logging to the extension startup (#8047)
Browse files Browse the repository at this point in the history
### Description

Adds a logging channel for the turbo vsc extension and rework some logging to prevent noisiness in the notification bar. This also adds some extra handling for homebrew based npm installs, and some additional handling to ensure paths are trimmed properly.

### Testing Instructions

Build and boot up the extension and observe the new output tab for Turbo.

Closes TURBO-2894
  • Loading branch information
arlyon committed Apr 30, 2024
1 parent f1bf03f commit 903ce78
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions packages/turbo-vsc/src/extension.ts
Expand Up @@ -42,6 +42,8 @@ const decoration = window.createTextEditorDecorationType({
color: "#04f1f9", // something like cyan
});

const logs = window.createOutputChannel("Turborepo Extension");

function rainbowRgb(i: number) {
const f = 0.5;
const r = Math.sin(f * i + (4.0 * Math.PI) / 3.0) * 127.0 + 128.0;
Expand Down Expand Up @@ -71,21 +73,27 @@ export function activate(context: ExtensionContext) {
let turboPath: string | undefined = turboSettings.get("path");
const useLocalTurbo: boolean = turboSettings.get("useLocalTurbo") ?? false;

logs.appendLine("starting the turbo extension");

if (turboPath && !fs.existsSync(turboPath)) {
window.showErrorMessage(
`turbo does not exist at path \`${turboPath}\`, attempting to locate it`
logs.appendLine(
`manually specified turbo does not exist at path \`${turboPath}\`, attempting to locate it`
);
turboPath = undefined;
}

try {
if (turboPath == null) {
turboPath = cp.execSync(
// attempt to source two well known version managers
// as well as adding the bun global bin to the path
'sh -c \'source "$HOME/.nvm/nvm.sh" > /dev/null 2>&1; source "$HOME/.asdf/asdf.sh" > /dev/null 2>&1; export PATH="$HOME/.bun/bin:$PATH"; which turbo\'',
options
);
logs.appendLine("attempting to find global turbo");
turboPath = cp
.execSync(
// attempt to source two well known version managers
// as well as adding the bun global bin to the path
'bash -c \'source "$HOME/.nvm/nvm.sh" > /dev/null 2>&1; source "$HOME/.asdf/asdf.sh" > /dev/null 2>&1; eval "$(brew shellenv)" > /dev/null 2>&1; export PATH="$HOME/.bun/bin:$PATH"; which turbo\'',
options
)
.trim();
logs.appendLine(`set turbo path to ${turboPath}`);
}
} catch (e: any) {
if (
Expand All @@ -94,21 +102,26 @@ export function activate(context: ExtensionContext) {
e.message.includes("which: no turbo in")
) {
// attempt to find local turbo instead
logs.appendLine("prompting global turbo");
promptGlobalTurbo(useLocalTurbo);
turboPath = findLocalTurbo();
} else {
window.showErrorMessage(e.message);
logs.appendLine(`unable to find turbo: ${e.message}`);
}
}

if (turboPath) {
logs.appendLine(`using turbo at path ${turboPath}`);
}

context.subscriptions.push(
commands.registerCommand("turbo.daemon.start", () => {
cp.exec(`${turboPath} daemon start`, options, (err) => {
if (err) {
if (err.message.includes("command not found")) {
promptGlobalTurbo(useLocalTurbo);
} else {
window.showErrorMessage(JSON.stringify(err));
logs.appendLine(`unable to start turbo: ${err.message}`);
}
} else {
updateStatusBarItem(true);
Expand All @@ -125,7 +138,7 @@ export function activate(context: ExtensionContext) {
if (err.message.includes("command not found")) {
promptGlobalTurbo(useLocalTurbo);
} else {
window.showErrorMessage(err.message);
logs.appendLine(`unable to stop turbo: ${err.message}`);
}
} else {
updateStatusBarItem(false);
Expand All @@ -142,7 +155,7 @@ export function activate(context: ExtensionContext) {
if (err.message.includes("command not found")) {
promptGlobalTurbo(useLocalTurbo);
} else {
window.showErrorMessage(err.message);
logs.appendLine(`unable to get turbo status: ${err.message}`);
updateStatusBarItem(false);
}
} else {
Expand Down Expand Up @@ -384,6 +397,7 @@ function findLocalTurbo(): string | undefined {

const checks = [
() => {
logs.appendLine("attempting to find local turbo using npm");
const npmList = cp.execSync("npm ls turbo --json", options);
const npmData = JSON.parse(npmList);

Expand All @@ -402,23 +416,27 @@ function findLocalTurbo(): string | undefined {
}
},
() => {
logs.appendLine("attempting to find local turbo using yarn");
const turboBin = cp.execSync("yarn bin turbo", options);
return turboBin.trim();
},
() => {
logs.appendLine("attempting to find local turbo using pnpm");
const binFolder = cp.execSync("pnpm bin", options).trim();
return path.join(binFolder, "turbo");
},
() => {
logs.appendLine("attempting to find local turbo using bun");
const binFolder = cp.execSync("bun pm bin", options).trim();
return path.join(binFolder, "turbo");
},
];

for (const potentialPath of checks) {
try {
const potential = potentialPath();
const potential = potentialPath()?.trim();
if (potential && fs.existsSync(potential)) {
logs.appendLine(`found local turbo at ${potential}`);
return potential;
}
} catch (e) {
Expand Down

0 comments on commit 903ce78

Please sign in to comment.