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
3 changes: 2 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"vscode": "^1.75.0"
},
"dependencies": {
"vscode-languageclient": "^8.1.0"
"vscode-languageclient": "^8.1.0",
"which": "^4.0.0"
},
"devDependencies": {
"@types/vscode": "^1.75.1",
Expand Down
110 changes: 54 additions & 56 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ export function activate(context: vscode.ExtensionContext) {
provideTerminalProfile(
token: vscode.CancellationToken
): vscode.ProviderResult<vscode.TerminalProfile> {
const which = require("which");
const path = require("path");
const fs = require("fs");
const glob = require("glob");
const os = require("os");

const PATH_FROM_ENV = process.env["PATH"];
const pathsToCheck = [
PATH_FROM_ENV,
// cargo install location
"~/.cargo/bin/nu",
"~/.cargo/bin/nu.exe",
(process.env["CARGO_HOME"] || "~/.cargo") + "/bin",

// winget on Windows install location
"c:\\program files\\nu\\bin\\nu.exe",
"c:\\program files\\nu\\bin",
// just add a few other drives for fun
"d:\\program files\\nu\\bin\\nu.exe",
"e:\\program files\\nu\\bin\\nu.exe",
"f:\\program files\\nu\\bin\\nu.exe",
"d:\\program files\\nu\\bin",
"e:\\program files\\nu\\bin",
"f:\\program files\\nu\\bin",

// SCOOP:TODO
// all user installed programs and scoop itself install to
// c:\users\<user>\scoop\ unless SCOOP env var is set
// globally installed programs go in
// c:\programdata\scoop unless SCOOP_GLOBAL env var is set
// scoop install location
"~/scoop/apps/nu/*/nu.exe",
"~/scoop/shims/nu.exe",
// SCOOP should already set up the correct `PATH` env var
//"~/scoop/apps/nu/*/nu.exe",
//"~/scoop/shims/nu.exe",

// chocolatey install location - same as winget
// 'c:\\program files\\nu\\bin\\nu.exe',
Expand All @@ -60,59 +60,57 @@ export function activate(context: vscode.ExtensionContext) {

// brew install location mac
// intel
"/usr/local/bin/nu",
"/usr/local/bin",
// arm
"/opt/homebrew/bin/nu",
"/opt/homebrew/bin",

// native package manager install location
"/usr/bin/nu",
// standard location should be in `PATH` env var
//"/usr/bin/nu",
];

let found_nushell_path = "";
const home = os.homedir();

for (const cur_val of pathsToCheck) {
// console.log("Inspecting location: " + cur_val);
let constructed_file = "";
if (cur_val.startsWith("~/scoop")) {
// console.log("Found scoop: " + cur_val);
const p = path.join(home, cur_val.slice(1));
// console.log("Expanded ~: " + p);
const file = glob.sync(p, "debug").toString();
// console.log("Glob for files: " + file);

if (file) {
// console.log("Found some file: " + file);
// if there are slashes, reverse them to back slashes
constructed_file = file.replace(/\//g, "\\");
const found_nushell_path = which.sync("nu", {
nothrow: true,
path: pathsToCheck.join(path.delimiter),
});

if (found_nushell_path == null) {
console.log(
"Nushell not found in env:PATH or any of the heuristic locations."
);
// use an async arrow funciton to use `await` inside
return (async () => {
if (
(await vscode.window.showErrorMessage(
"We cannot find a nushell executable in your path or pre-defined locations",
"install from website"
)) &&
(await vscode.env.openExternal(
vscode.Uri.parse("https://www.nushell.sh/")
)) &&
(await vscode.window.showInformationMessage(
"after you install nushell, you might need to reload vscode",
"reload now"
))
) {
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
} else if (cur_val.startsWith("~")) {
constructed_file = path.join(home, cur_val.slice(1));
// console.log("Found ~, constructing path: " + constructed_file);
} else {
constructed_file = cur_val;
}

if (fs.existsSync(constructed_file)) {
// console.log("File exists, returning: " + constructed_file);
found_nushell_path = constructed_file;
break;
} else {
// console.log("File not found: " + constructed_file);
}
// user has already seen error messages, but they didn't click through
// return a promise that never resolve to supress the confusing error
return await new Promise(() => undefined);
})();
}

if (found_nushell_path.length > 0) {
return {
options: {
name: "Nushell",
shellPath: found_nushell_path,
},
};
} else {
console.log("Nushell not found, returning undefined");
return undefined;
}
return {
options: {
name: "Nushell",
shellPath: found_nushell_path,
iconPath: vscode.Uri.joinPath(
context.extensionUri,
"assets/nu.svg"
),
},
};
},
})
);
Expand Down