@microsoft/inshellisense 0.0.1-rc.2 → 0.0.1-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/commands/root.js +5 -4
- package/build/index.js +2 -1
- package/build/ui/ui-init.js +44 -0
- package/build/utils/bindings.js +25 -5
- package/build/utils/shell.js +10 -0
- package/package.json +2 -1
package/build/commands/root.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
// Copyright (c) Microsoft Corporation.
|
|
2
2
|
// Licensed under the MIT License.
|
|
3
|
+
import { initRender } from "../ui/ui-init.js";
|
|
3
4
|
import { render } from "../ui/ui-root.js";
|
|
4
5
|
import { executeShellCommandTTY } from "../runtime/utils.js";
|
|
5
6
|
import { saveCommand, loadCommand } from "../utils/cache.js";
|
|
6
7
|
import { supportedShells as shells } from "../utils/bindings.js";
|
|
8
|
+
import { inferShell } from "../utils/shell.js";
|
|
7
9
|
export const supportedShells = shells.join(", ");
|
|
8
|
-
export const action = async (options) => {
|
|
10
|
+
export const action = (program) => async (options) => {
|
|
9
11
|
if (options.history) {
|
|
10
12
|
process.stdout.write(await loadCommand());
|
|
11
13
|
process.exit(0);
|
|
12
14
|
}
|
|
13
|
-
const shell = options.shell ?? "";
|
|
15
|
+
const shell = options.shell ?? (await inferShell()) ?? (await initRender()) ?? "";
|
|
14
16
|
if (!shells.map((s) => s.valueOf()).includes(shell)) {
|
|
15
|
-
|
|
16
|
-
process.exit(1);
|
|
17
|
+
program.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`, { exitCode: 1 });
|
|
17
18
|
}
|
|
18
19
|
let executed = false;
|
|
19
20
|
const commands = [];
|
package/build/index.js
CHANGED
|
@@ -16,7 +16,8 @@ program
|
|
|
16
16
|
.option("-c, --command <commmand>", "command to use as initial input")
|
|
17
17
|
.option("--history", "get the last command execute")
|
|
18
18
|
.option("-d, --duration <duration>", "duration of the autocomplete session, supported durations: single, session", "session")
|
|
19
|
-
.action(action)
|
|
19
|
+
.action(action(program))
|
|
20
|
+
.showHelpAfterError("(add --help for additional information)");
|
|
20
21
|
program.addCommand(bind);
|
|
21
22
|
program.addCommand(uninstall);
|
|
22
23
|
program.parse();
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
import React, { useState } from "react";
|
|
4
|
+
import { Box, Text, render, useApp, useInput } from "ink";
|
|
5
|
+
import { supportedShells } from "../utils/bindings.js";
|
|
6
|
+
let uiResult = undefined;
|
|
7
|
+
function UI() {
|
|
8
|
+
const { exit } = useApp();
|
|
9
|
+
const [selectionIdx, setSelectionIdx] = useState(0);
|
|
10
|
+
const [exited, setExited] = useState(false);
|
|
11
|
+
useInput(async (_, key) => {
|
|
12
|
+
if (key.upArrow) {
|
|
13
|
+
setSelectionIdx(Math.max(0, selectionIdx - 1));
|
|
14
|
+
}
|
|
15
|
+
else if (key.downArrow) {
|
|
16
|
+
setSelectionIdx(Math.min(supportedShells.length - 1, selectionIdx + 1));
|
|
17
|
+
}
|
|
18
|
+
else if (key.return) {
|
|
19
|
+
uiResult = supportedShells[selectionIdx];
|
|
20
|
+
setExited(true);
|
|
21
|
+
setTimeout(exit, 0);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return (React.createElement(React.Fragment, null, exited ? null : (React.createElement(Box, { flexDirection: "column" },
|
|
25
|
+
React.createElement(Box, null,
|
|
26
|
+
React.createElement(Text, { bold: true }, "Select your desired shell")),
|
|
27
|
+
React.createElement(Box, { flexDirection: "column" }, supportedShells.map((shell, idx) => {
|
|
28
|
+
if (idx == selectionIdx) {
|
|
29
|
+
return (React.createElement(Text, { color: "cyan", underline: true, key: idx },
|
|
30
|
+
">",
|
|
31
|
+
" ",
|
|
32
|
+
shell));
|
|
33
|
+
}
|
|
34
|
+
return (React.createElement(Text, { key: idx },
|
|
35
|
+
" ",
|
|
36
|
+
shell));
|
|
37
|
+
}))))));
|
|
38
|
+
}
|
|
39
|
+
export const initRender = async () => {
|
|
40
|
+
uiResult = undefined;
|
|
41
|
+
const { waitUntilExit } = render(React.createElement(UI, null));
|
|
42
|
+
await waitUntilExit();
|
|
43
|
+
return uiResult;
|
|
44
|
+
};
|
package/build/utils/bindings.js
CHANGED
|
@@ -39,12 +39,32 @@ const pwshScriptCommand = () => {
|
|
|
39
39
|
return `if(Test-Path '${bindingsPath}' -PathType Leaf){. ${bindingsPath}}`;
|
|
40
40
|
};
|
|
41
41
|
const pwshConfigPath = async () => {
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
try {
|
|
43
|
+
const { stdout } = await execAsync("echo $profile", { shell: "pwsh" });
|
|
44
|
+
return stdout.trim();
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
/* empty */
|
|
48
|
+
}
|
|
49
|
+
switch (process.platform) {
|
|
50
|
+
case "win32":
|
|
51
|
+
return path.join(os.homedir(), "Documents", "Powershell", "Microsoft.PowerShell_profile.ps1");
|
|
52
|
+
case "linux":
|
|
53
|
+
case "darwin":
|
|
54
|
+
return path.join(os.homedir(), ".config", "powershell", "Microsoft.PowerShell_profile.ps1");
|
|
55
|
+
default:
|
|
56
|
+
throw new Error("Unsupported platform");
|
|
57
|
+
}
|
|
44
58
|
};
|
|
45
59
|
const powershellConfigPath = async () => {
|
|
46
|
-
|
|
47
|
-
|
|
60
|
+
try {
|
|
61
|
+
const { stdout } = await execAsync("echo $profile", { shell: "powershell" });
|
|
62
|
+
return stdout.trim();
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
/* empty */
|
|
66
|
+
}
|
|
67
|
+
return path.join(os.homedir(), "Documents", "WindowsPowershell", "Microsoft.PowerShell_profile.ps1");
|
|
48
68
|
};
|
|
49
69
|
export const availableBindings = async () => {
|
|
50
70
|
const cliConfigPath = path.join(os.homedir(), cacheFolder);
|
|
@@ -82,8 +102,8 @@ export const availableBindings = async () => {
|
|
|
82
102
|
bindings.push(Shell.Fish);
|
|
83
103
|
}
|
|
84
104
|
}
|
|
85
|
-
const powershellResolvedConfigPath = await powershellConfigPath();
|
|
86
105
|
if (process.platform == "win32") {
|
|
106
|
+
const powershellResolvedConfigPath = await powershellConfigPath();
|
|
87
107
|
if (!fs.existsSync(powershellResolvedConfigPath)) {
|
|
88
108
|
bindings.push(Shell.Powershell);
|
|
89
109
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import find from "find-process";
|
|
5
|
+
import { supportedShells } from "./bindings.js";
|
|
6
|
+
export const inferShell = async () => {
|
|
7
|
+
const processResult = (await find("pid", process.ppid)).at(0);
|
|
8
|
+
const name = processResult?.name;
|
|
9
|
+
return name != null ? supportedShells.find((shell) => name.includes(shell)) : undefined;
|
|
10
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microsoft/inshellisense",
|
|
3
|
-
"version": "0.0.1-rc.
|
|
3
|
+
"version": "0.0.1-rc.4",
|
|
4
4
|
"description": "IDE style command line auto complete",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"@withfig/autocomplete": "^2.633.0",
|
|
37
37
|
"chalk": "^5.3.0",
|
|
38
38
|
"commander": "^11.0.0",
|
|
39
|
+
"find-process": "^1.4.7",
|
|
39
40
|
"ink": "^4.4.1",
|
|
40
41
|
"react": "^18.2.0",
|
|
41
42
|
"wrap-ansi": "^8.1.0"
|