Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use which to search nu location, add icon too. #153

Merged
merged 5 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
91 changes: 34 additions & 57 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",
fdncred marked this conversation as resolved.
Show resolved Hide resolved

// chocolatey install location - same as winget
// 'c:\\program files\\nu\\bin\\nu.exe',
Expand All @@ -60,59 +60,36 @@ 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",
fdncred marked this conversation as resolved.
Show resolved Hide resolved
];

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, "\\");
}
} 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);
}
}
const found_nushell_path = which.sync("nu", {
fdncred marked this conversation as resolved.
Show resolved Hide resolved
nothrow: true,
path: pathsToCheck.join(path.delimiter),
});

if (found_nushell_path.length > 0) {
return {
options: {
name: "Nushell",
shellPath: found_nushell_path,
},
};
} else {
console.log("Nushell not found, returning undefined");
return undefined;
if (found_nushell_path == null) {
console.log(
"Nushell not found in env:PATH or any of the heuristic locations."
);
}

return {
options: {
name: "Nushell",
shellPath: found_nushell_path || "nu",
nerditation marked this conversation as resolved.
Show resolved Hide resolved
iconPath: vscode.Uri.joinPath(
context.extensionUri,
"assets/nu.svg"
),
},
};
},
})
);
Expand Down