@microsoft/inshellisense 0.0.1-rc.1
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/CODE_OF_CONDUCT.md +9 -0
- package/LICENSE +21 -0
- package/README.md +55 -0
- package/SECURITY.md +41 -0
- package/SUPPORT.md +13 -0
- package/build/commands/bind.js +12 -0
- package/build/commands/root.js +40 -0
- package/build/index.js +20 -0
- package/build/runtime/generator.js +42 -0
- package/build/runtime/model.js +3 -0
- package/build/runtime/parser.js +63 -0
- package/build/runtime/runtime.js +224 -0
- package/build/runtime/suggestion.js +184 -0
- package/build/runtime/template.js +35 -0
- package/build/runtime/utils.js +22 -0
- package/build/ui/input.js +55 -0
- package/build/ui/suggestions.js +84 -0
- package/build/ui/ui-bind.js +64 -0
- package/build/ui/ui-root.js +71 -0
- package/build/utils/bindings.js +144 -0
- package/build/utils/cache.js +21 -0
- package/build/utils/version.js +13 -0
- package/package.json +59 -0
- package/shell/key-bindings-powershell.ps1 +27 -0
- package/shell/key-bindings-pwsh.ps1 +27 -0
- package/shell/key-bindings.bash +7 -0
- package/shell/key-bindings.fish +8 -0
- package/shell/key-bindings.zsh +10 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Microsoft Open Source Code of Conduct
|
|
2
|
+
|
|
3
|
+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
|
|
4
|
+
|
|
5
|
+
Resources:
|
|
6
|
+
|
|
7
|
+
- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/)
|
|
8
|
+
- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
|
|
9
|
+
- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# inshellisense
|
|
2
|
+
|
|
3
|
+
`inshellisense` provides IDE style autocomplete for shells. It's a terminal native runtime for [autocomplete](https://github.com/withfig/autocomplete) which has support for 600+ command line tools. `inshellisense` supports Windows, Linux, & MacOS.
|
|
4
|
+
|
|
5
|
+
<p align="center"><img alt="demo of inshellisense working" src="/docs/demo.gif" height="450px"/></p>
|
|
6
|
+
|
|
7
|
+
## Getting Started
|
|
8
|
+
|
|
9
|
+
### Installation
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
npm install -g @microsoft/inshellisense
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Quickstart
|
|
16
|
+
|
|
17
|
+
After completing the installation, you can already run `inshellisense --shell <shell>` to start the autocomplete session for your desired shell. Additionally, you can bind `inshellisense` to a keybinding of `CTRL+a` by running the below command. This brings the added advantages of automatically starting the autocomplete session with your current shell and injecting any accepted command into your shell's history.
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
inshellisense bind
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Additionally, inshellisense is also aliased under `is` after install for convenience.
|
|
24
|
+
|
|
25
|
+
## Integrations
|
|
26
|
+
|
|
27
|
+
inshellisense supports the following shells:
|
|
28
|
+
|
|
29
|
+
- [bash](https://www.gnu.org/software/bash/)
|
|
30
|
+
- [zsh](https://www.zsh.org/)
|
|
31
|
+
- [fish](https://github.com/fish-shell/fish-shell)
|
|
32
|
+
- [pwsh](https://github.com/PowerShell/PowerShell)
|
|
33
|
+
- [powershell](https://learn.microsoft.com/en-us/powershell/scripting/windows-powershell/starting-windows-powershell) (Windows Powershell)
|
|
34
|
+
|
|
35
|
+
## Contributing
|
|
36
|
+
|
|
37
|
+
This project welcomes contributions and suggestions. Most contributions require you to agree to a
|
|
38
|
+
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
|
|
39
|
+
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
|
|
40
|
+
|
|
41
|
+
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
|
|
42
|
+
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
|
|
43
|
+
provided by the bot. You will only need to do this once across all repos using our CLA.
|
|
44
|
+
|
|
45
|
+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
|
|
46
|
+
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
|
|
47
|
+
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
|
|
48
|
+
|
|
49
|
+
## Trademarks
|
|
50
|
+
|
|
51
|
+
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
|
|
52
|
+
trademarks or logos is subject to and must follow
|
|
53
|
+
[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
|
|
54
|
+
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
|
|
55
|
+
Any use of third-party trademarks or logos are subject to those third-party's policies.
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<!-- BEGIN MICROSOFT SECURITY.MD V0.0.9 BLOCK -->
|
|
2
|
+
|
|
3
|
+
## Security
|
|
4
|
+
|
|
5
|
+
Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet) and [Xamarin](https://github.com/xamarin).
|
|
6
|
+
|
|
7
|
+
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/security.md/definition), please report it to us as described below.
|
|
8
|
+
|
|
9
|
+
## Reporting Security Issues
|
|
10
|
+
|
|
11
|
+
**Please do not report security vulnerabilities through public GitHub issues.**
|
|
12
|
+
|
|
13
|
+
Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/security.md/msrc/create-report).
|
|
14
|
+
|
|
15
|
+
If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/security.md/msrc/pgp).
|
|
16
|
+
|
|
17
|
+
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
|
|
18
|
+
|
|
19
|
+
Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
|
|
20
|
+
|
|
21
|
+
* Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
|
|
22
|
+
* Full paths of source file(s) related to the manifestation of the issue
|
|
23
|
+
* The location of the affected source code (tag/branch/commit or direct URL)
|
|
24
|
+
* Any special configuration required to reproduce the issue
|
|
25
|
+
* Step-by-step instructions to reproduce the issue
|
|
26
|
+
* Proof-of-concept or exploit code (if possible)
|
|
27
|
+
* Impact of the issue, including how an attacker might exploit the issue
|
|
28
|
+
|
|
29
|
+
This information will help us triage your report more quickly.
|
|
30
|
+
|
|
31
|
+
If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/security.md/msrc/bounty) page for more details about our active programs.
|
|
32
|
+
|
|
33
|
+
## Preferred Languages
|
|
34
|
+
|
|
35
|
+
We prefer all communications to be in English.
|
|
36
|
+
|
|
37
|
+
## Policy
|
|
38
|
+
|
|
39
|
+
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/security.md/cvd).
|
|
40
|
+
|
|
41
|
+
<!-- END MICROSOFT SECURITY.MD BLOCK -->
|
package/SUPPORT.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Support
|
|
2
|
+
|
|
3
|
+
## How to file issues and get help
|
|
4
|
+
|
|
5
|
+
This project uses GitHub Issues to track bugs and feature requests. Please search the existing
|
|
6
|
+
issues before filing new issues to avoid duplicates. For new issues, file your bug or
|
|
7
|
+
feature request as a new Issue.
|
|
8
|
+
|
|
9
|
+
For help and questions about using this project, create a new Github Discussion.
|
|
10
|
+
|
|
11
|
+
## Microsoft Support Policy
|
|
12
|
+
|
|
13
|
+
Support for this project is limited to the resources listed above.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import { supportedShells } from "../utils/bindings.js";
|
|
5
|
+
import { render } from "../ui/ui-bind.js";
|
|
6
|
+
const action = async () => {
|
|
7
|
+
await render();
|
|
8
|
+
};
|
|
9
|
+
const cmd = new Command("bind");
|
|
10
|
+
cmd.description(`adds keybindings to the selected shell: ${supportedShells.join(", ")}`);
|
|
11
|
+
cmd.action(action);
|
|
12
|
+
export default cmd;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
import { render } from "../ui/ui-root.js";
|
|
4
|
+
import { executeShellCommandTTY } from "../runtime/utils.js";
|
|
5
|
+
import { saveCommand, loadCommand } from "../utils/cache.js";
|
|
6
|
+
import { supportedShells as shells } from "../utils/bindings.js";
|
|
7
|
+
export const supportedShells = shells.join(", ");
|
|
8
|
+
export const action = async (options) => {
|
|
9
|
+
if (options.history) {
|
|
10
|
+
process.stdout.write(await loadCommand());
|
|
11
|
+
process.exit(0);
|
|
12
|
+
}
|
|
13
|
+
const shell = options.shell ?? "";
|
|
14
|
+
if (!shells.map((s) => s.valueOf()).includes(shell)) {
|
|
15
|
+
console.error(`Unsupported shell: '${shell}', supported shells: ${supportedShells}`);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
let executed = false;
|
|
19
|
+
const commands = [];
|
|
20
|
+
let result = { code: 0 };
|
|
21
|
+
let startingCommand = options.command;
|
|
22
|
+
while (options.duration === "session" || !executed) {
|
|
23
|
+
const commandToExecute = await render(startingCommand);
|
|
24
|
+
if (commandToExecute == null || commandToExecute.trim().toLowerCase() == "exit" || commandToExecute.trim().toLowerCase() == "logout") {
|
|
25
|
+
result = { code: 0 };
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
commands.push(commandToExecute);
|
|
29
|
+
result = await executeShellCommandTTY(shell, commandToExecute);
|
|
30
|
+
executed = true;
|
|
31
|
+
startingCommand = undefined;
|
|
32
|
+
}
|
|
33
|
+
await saveCommand(commands);
|
|
34
|
+
if (result.code) {
|
|
35
|
+
process.exit(result.code);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
process.exit(0);
|
|
39
|
+
}
|
|
40
|
+
};
|
package/build/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Copyright (c) Microsoft Corporation.
|
|
3
|
+
// Licensed under the MIT License.
|
|
4
|
+
/* eslint-disable header/header */
|
|
5
|
+
import { Command } from "commander";
|
|
6
|
+
import bind from "./commands/bind.js";
|
|
7
|
+
import { action, supportedShells } from "./commands/root.js";
|
|
8
|
+
import { getVersion } from "./utils/version.js";
|
|
9
|
+
const program = new Command();
|
|
10
|
+
program
|
|
11
|
+
.name("inshellisense")
|
|
12
|
+
.description("IDE style command line auto complete")
|
|
13
|
+
.version(await getVersion(), "-v, --version", "output the current version")
|
|
14
|
+
.option("-s, --shell <shell>", `shell to use for command execution, supported shells: ${supportedShells}`)
|
|
15
|
+
.option("-c, --command <commmand>", "command to use as initial input")
|
|
16
|
+
.option("--history", "get the last command execute")
|
|
17
|
+
.option("-d, --duration <duration>", "duration of the autocomplete session, supported durations: single, session", "session")
|
|
18
|
+
.action(action);
|
|
19
|
+
program.addCommand(bind);
|
|
20
|
+
program.parse();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
import { runTemplates } from "./template.js";
|
|
4
|
+
import { buildExecuteShellCommand } from "./utils.js";
|
|
5
|
+
const getGeneratorContext = () => {
|
|
6
|
+
return {
|
|
7
|
+
environmentVariables: Object.fromEntries(Object.entries(process.env).filter((entry) => entry[1] != null)),
|
|
8
|
+
currentWorkingDirectory: process.cwd(),
|
|
9
|
+
currentProcess: "",
|
|
10
|
+
sshPrefix: "",
|
|
11
|
+
isDangerous: false,
|
|
12
|
+
searchTerm: "", // TODO: define search term
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
// TODO: add support for caching, trigger, & getQueryTerm
|
|
16
|
+
export const runGenerator = async (generator, tokens) => {
|
|
17
|
+
const { script, postProcess, scriptTimeout, splitOn, custom, template } = generator;
|
|
18
|
+
const executeShellCommand = buildExecuteShellCommand(scriptTimeout ?? 5000);
|
|
19
|
+
const suggestions = [];
|
|
20
|
+
try {
|
|
21
|
+
if (script) {
|
|
22
|
+
const scriptOutput = typeof script === "function" ? script(tokens) : script != null ? await executeShellCommand(script) : "";
|
|
23
|
+
if (postProcess) {
|
|
24
|
+
suggestions.push(...postProcess(scriptOutput, tokens));
|
|
25
|
+
}
|
|
26
|
+
else if (splitOn) {
|
|
27
|
+
suggestions.push(...scriptOutput.split(splitOn).map((s) => ({ name: s })));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (custom) {
|
|
31
|
+
suggestions.push(...(await custom(tokens, executeShellCommand, getGeneratorContext())));
|
|
32
|
+
}
|
|
33
|
+
if (template != null) {
|
|
34
|
+
suggestions.push(...(await runTemplates(template)));
|
|
35
|
+
}
|
|
36
|
+
return suggestions;
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
/* empty */
|
|
40
|
+
}
|
|
41
|
+
return suggestions;
|
|
42
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
const cmdDelim = /(\|\|)|(&&)|(;)/;
|
|
4
|
+
const spaceRegex = /\s/;
|
|
5
|
+
export const parseCommand = (command) => {
|
|
6
|
+
const lastCommand = command.split(cmdDelim).at(-1)?.trimStart();
|
|
7
|
+
return lastCommand ? lex(lastCommand) : [];
|
|
8
|
+
};
|
|
9
|
+
const lex = (command) => {
|
|
10
|
+
const tokens = [];
|
|
11
|
+
let [readingQuotedString, readingFlag, readingCmd] = [false, false, false];
|
|
12
|
+
let readingIdx = 0;
|
|
13
|
+
let readingQuoteChar = "";
|
|
14
|
+
[...command].forEach((char, idx) => {
|
|
15
|
+
const reading = readingQuotedString || readingFlag || readingCmd;
|
|
16
|
+
if (!reading && (char === `'` || char === `"`)) {
|
|
17
|
+
[readingQuotedString, readingIdx, readingQuoteChar] = [true, idx, char];
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
else if (!reading && char === `-`) {
|
|
21
|
+
[readingFlag, readingIdx] = [true, idx];
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
else if (!reading && !spaceRegex.test(char)) {
|
|
25
|
+
[readingCmd, readingIdx] = [true, idx];
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (readingQuotedString && char === readingQuoteChar && command.at(idx - 1) !== "\\") {
|
|
29
|
+
readingQuotedString = false;
|
|
30
|
+
const complete = idx + 1 < command.length && spaceRegex.test(command[idx + 1]);
|
|
31
|
+
tokens.push({
|
|
32
|
+
token: command.slice(readingIdx, idx + 1),
|
|
33
|
+
complete,
|
|
34
|
+
isOption: false,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else if ((readingFlag && spaceRegex.test(char)) || char === "=") {
|
|
38
|
+
readingFlag = false;
|
|
39
|
+
tokens.push({
|
|
40
|
+
token: command.slice(readingIdx, idx),
|
|
41
|
+
complete: true,
|
|
42
|
+
isOption: true,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else if (readingCmd && spaceRegex.test(char)) {
|
|
46
|
+
readingCmd = false;
|
|
47
|
+
tokens.push({
|
|
48
|
+
token: command.slice(readingIdx, idx),
|
|
49
|
+
complete: true,
|
|
50
|
+
isOption: false,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
const reading = readingQuotedString || readingFlag || readingCmd;
|
|
55
|
+
if (reading) {
|
|
56
|
+
tokens.push({
|
|
57
|
+
token: command.slice(readingIdx),
|
|
58
|
+
complete: false,
|
|
59
|
+
isOption: false,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return tokens;
|
|
63
|
+
};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT License.
|
|
3
|
+
import speclist, { diffVersionedCompletions as versionedSpeclist,
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
} from "@withfig/autocomplete/build/index.js";
|
|
7
|
+
import { parseCommand } from "./parser.js";
|
|
8
|
+
import { getArgDrivenRecommendation, getSubcommandDrivenRecommendation } from "./suggestion.js";
|
|
9
|
+
import { buildExecuteShellCommand } from "./utils.js";
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- recursive type, setting as any
|
|
11
|
+
const specSet = {};
|
|
12
|
+
speclist.forEach((s) => {
|
|
13
|
+
let activeSet = specSet;
|
|
14
|
+
const specRoutes = s.split("/");
|
|
15
|
+
specRoutes.forEach((route, idx) => {
|
|
16
|
+
if (idx === specRoutes.length - 1) {
|
|
17
|
+
const prefix = versionedSpeclist.includes(s) ? "/index.js" : `.js`;
|
|
18
|
+
activeSet[route] = `@withfig/autocomplete/build/${s}${prefix}`;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
activeSet[route] = activeSet[route] || {};
|
|
22
|
+
activeSet = activeSet[route];
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
const loadedSpecs = {};
|
|
27
|
+
const loadSpec = async (cmd) => {
|
|
28
|
+
const rootToken = cmd.at(0);
|
|
29
|
+
if (!rootToken?.complete) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (loadedSpecs[rootToken.token]) {
|
|
33
|
+
return loadedSpecs[rootToken.token];
|
|
34
|
+
}
|
|
35
|
+
if (specSet[rootToken.token]) {
|
|
36
|
+
const spec = (await import(specSet[rootToken.token])).default;
|
|
37
|
+
loadedSpecs[rootToken.token] = spec;
|
|
38
|
+
return spec;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
// this load spec function should only be used for `loadSpec` on the fly as it is cacheless
|
|
42
|
+
const lazyLoadSpec = async (key) => {
|
|
43
|
+
return (await import(`@withfig/autocomplete/build/${key}.js`)).default;
|
|
44
|
+
};
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- will be implemented in below TODO
|
|
46
|
+
const lazyLoadSpecLocation = async (location) => {
|
|
47
|
+
return; //TODO: implement spec location loading
|
|
48
|
+
};
|
|
49
|
+
export const getSuggestions = async (cmd) => {
|
|
50
|
+
const activeCmd = parseCommand(cmd);
|
|
51
|
+
const rootToken = activeCmd.at(0);
|
|
52
|
+
if (activeCmd.length === 0 || !rootToken?.complete) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const spec = await loadSpec(activeCmd);
|
|
56
|
+
if (spec == null)
|
|
57
|
+
return;
|
|
58
|
+
const subcommand = getSubcommand(spec);
|
|
59
|
+
if (subcommand == null)
|
|
60
|
+
return;
|
|
61
|
+
const result = await runSubcommand(activeCmd.slice(1), subcommand);
|
|
62
|
+
if (result == null)
|
|
63
|
+
return;
|
|
64
|
+
const lastCommand = activeCmd.at(-1);
|
|
65
|
+
const charactersToDrop = lastCommand?.complete ? 0 : lastCommand?.token.length ?? 0;
|
|
66
|
+
return { ...result, charactersToDrop };
|
|
67
|
+
};
|
|
68
|
+
const getPersistentOptions = (persistentOptions, options) => {
|
|
69
|
+
const persistentOptionNames = new Set(persistentOptions.map((o) => (typeof o.name === "string" ? [o.name] : o.name)).flat());
|
|
70
|
+
return persistentOptions.concat((options ?? []).filter((o) => (typeof o.name == "string" ? !persistentOptionNames.has(o.name) : o.name.some((n) => !persistentOptionNames.has(n))) && o.isPersistent === true));
|
|
71
|
+
};
|
|
72
|
+
// TODO: handle subcommands that are versioned
|
|
73
|
+
const getSubcommand = (spec) => {
|
|
74
|
+
if (spec == null)
|
|
75
|
+
return;
|
|
76
|
+
if (typeof spec === "function") {
|
|
77
|
+
const potentialSubcommand = spec();
|
|
78
|
+
if (Object.prototype.hasOwnProperty.call(potentialSubcommand, "name")) {
|
|
79
|
+
return potentialSubcommand;
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
return spec;
|
|
84
|
+
};
|
|
85
|
+
const executeShellCommand = buildExecuteShellCommand(5000);
|
|
86
|
+
const genSubcommand = async (command, parentCommand) => {
|
|
87
|
+
const subcommandIdx = parentCommand.subcommands?.findIndex((s) => s.name === command);
|
|
88
|
+
if (subcommandIdx == null)
|
|
89
|
+
return;
|
|
90
|
+
const subcommand = parentCommand.subcommands?.at(subcommandIdx);
|
|
91
|
+
if (subcommand == null)
|
|
92
|
+
return;
|
|
93
|
+
// this pulls in the spec from the load spec and overwrites the subcommand in the parent with the loaded spec.
|
|
94
|
+
// then it returns the subcommand and clears the loadSpec field so that it doesn't get called again
|
|
95
|
+
switch (typeof subcommand.loadSpec) {
|
|
96
|
+
case "function": {
|
|
97
|
+
const partSpec = await subcommand.loadSpec(command, executeShellCommand);
|
|
98
|
+
if (partSpec instanceof Array) {
|
|
99
|
+
const locationSpecs = (await Promise.all(partSpec.map((s) => lazyLoadSpecLocation(s)))).filter((s) => s != null);
|
|
100
|
+
const subcommands = locationSpecs.map((s) => getSubcommand(s)).filter((s) => s != null);
|
|
101
|
+
parentCommand.subcommands[subcommandIdx] = {
|
|
102
|
+
...subcommand,
|
|
103
|
+
...(subcommands.find((s) => s?.name == command) ?? []),
|
|
104
|
+
loadSpec: undefined,
|
|
105
|
+
};
|
|
106
|
+
return parentCommand.subcommands[subcommandIdx];
|
|
107
|
+
}
|
|
108
|
+
else if (Object.prototype.hasOwnProperty.call(partSpec, "type")) {
|
|
109
|
+
const locationSingleSpec = await lazyLoadSpecLocation(partSpec);
|
|
110
|
+
parentCommand.subcommands[subcommandIdx] = {
|
|
111
|
+
...subcommand,
|
|
112
|
+
...(getSubcommand(locationSingleSpec) ?? []),
|
|
113
|
+
loadSpec: undefined,
|
|
114
|
+
};
|
|
115
|
+
return parentCommand.subcommands[subcommandIdx];
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
parentCommand.subcommands[subcommandIdx] = { ...subcommand, ...partSpec, loadSpec: undefined };
|
|
119
|
+
return parentCommand.subcommands[subcommandIdx];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
case "string": {
|
|
123
|
+
const spec = await lazyLoadSpec(subcommand.loadSpec);
|
|
124
|
+
parentCommand.subcommands[subcommandIdx] = { ...subcommand, ...(getSubcommand(spec) ?? []), loadSpec: undefined };
|
|
125
|
+
return parentCommand.subcommands[subcommandIdx];
|
|
126
|
+
}
|
|
127
|
+
case "object": {
|
|
128
|
+
parentCommand.subcommands[subcommandIdx] = { ...subcommand, ...(subcommand.loadSpec ?? {}), loadSpec: undefined };
|
|
129
|
+
return parentCommand.subcommands[subcommandIdx];
|
|
130
|
+
}
|
|
131
|
+
case "undefined": {
|
|
132
|
+
return subcommand;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const getOption = (activeToken, options) => {
|
|
137
|
+
return options.find((o) => (typeof o.name === "string" ? o.name === activeToken.token : o.name.includes(activeToken.token)));
|
|
138
|
+
};
|
|
139
|
+
const getPersistentTokens = (tokens) => {
|
|
140
|
+
return tokens.filter((t) => t.isPersistent === true);
|
|
141
|
+
};
|
|
142
|
+
const getArgs = (args) => {
|
|
143
|
+
return args instanceof Array ? args : args != null ? [args] : [];
|
|
144
|
+
};
|
|
145
|
+
const runOption = async (tokens, option, subcommand, persistentOptions, acceptedTokens) => {
|
|
146
|
+
if (tokens.length === 0) {
|
|
147
|
+
throw new Error("invalid state reached, option expected but no tokens found");
|
|
148
|
+
}
|
|
149
|
+
const activeToken = tokens[0];
|
|
150
|
+
const isPersistent = persistentOptions.some((o) => (typeof o.name === "string" ? o.name === activeToken.token : o.name.includes(activeToken.token)));
|
|
151
|
+
if ((option.args instanceof Array && option.args.length > 0) || option.args != null) {
|
|
152
|
+
const args = option.args instanceof Array ? option.args : [option.args];
|
|
153
|
+
return runArg(tokens.slice(1), args, subcommand, persistentOptions, acceptedTokens.concat(activeToken), true, false);
|
|
154
|
+
}
|
|
155
|
+
return runSubcommand(tokens.slice(1), subcommand, persistentOptions, acceptedTokens.concat({ ...activeToken, isPersistent }));
|
|
156
|
+
};
|
|
157
|
+
const runArg = async (tokens, args, subcommand, persistentOptions, acceptedTokens, fromOption, fromVariadic) => {
|
|
158
|
+
if (args.length === 0) {
|
|
159
|
+
return runSubcommand(tokens, subcommand, persistentOptions, acceptedTokens, true, !fromOption);
|
|
160
|
+
}
|
|
161
|
+
else if (tokens.length === 0) {
|
|
162
|
+
return await getArgDrivenRecommendation(args, subcommand, persistentOptions, undefined, acceptedTokens, fromVariadic);
|
|
163
|
+
}
|
|
164
|
+
else if (!tokens.at(0)?.complete) {
|
|
165
|
+
return await getArgDrivenRecommendation(args, subcommand, persistentOptions, tokens[0].token, acceptedTokens, fromVariadic);
|
|
166
|
+
}
|
|
167
|
+
const activeToken = tokens[0];
|
|
168
|
+
if (args.every((a) => a.isOptional)) {
|
|
169
|
+
if (activeToken.isOption) {
|
|
170
|
+
const option = getOption(activeToken, persistentOptions.concat(subcommand.options ?? []));
|
|
171
|
+
if (option != null) {
|
|
172
|
+
return runOption(tokens, option, subcommand, persistentOptions, acceptedTokens);
|
|
173
|
+
}
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const nextSubcommand = await genSubcommand(activeToken.token, subcommand);
|
|
177
|
+
if (nextSubcommand != null) {
|
|
178
|
+
return runSubcommand(tokens.slice(1), nextSubcommand, persistentOptions, getPersistentTokens(acceptedTokens.concat(activeToken)));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const activeArg = args[0];
|
|
182
|
+
if (activeArg.isVariadic) {
|
|
183
|
+
return runArg(tokens.slice(1), args, subcommand, persistentOptions, acceptedTokens.concat(activeToken), fromOption, true);
|
|
184
|
+
}
|
|
185
|
+
else if (activeArg.isCommand) {
|
|
186
|
+
if (tokens.length <= 0) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const spec = await loadSpec(tokens);
|
|
190
|
+
if (spec == null)
|
|
191
|
+
return;
|
|
192
|
+
const subcommand = getSubcommand(spec);
|
|
193
|
+
if (subcommand == null)
|
|
194
|
+
return;
|
|
195
|
+
return runSubcommand(tokens.slice(1), subcommand);
|
|
196
|
+
}
|
|
197
|
+
return runArg(tokens.slice(1), args.slice(1), subcommand, persistentOptions, acceptedTokens.concat(activeToken), fromOption, false);
|
|
198
|
+
};
|
|
199
|
+
const runSubcommand = async (tokens, subcommand, persistentOptions = [], acceptedTokens = [], argsDepleted = false, argsUsed = false) => {
|
|
200
|
+
if (tokens.length === 0) {
|
|
201
|
+
return getSubcommandDrivenRecommendation(subcommand, persistentOptions, undefined, argsDepleted, argsUsed, acceptedTokens);
|
|
202
|
+
}
|
|
203
|
+
else if (!tokens.at(0)?.complete) {
|
|
204
|
+
return getSubcommandDrivenRecommendation(subcommand, persistentOptions, tokens[0].token, argsDepleted, argsUsed, acceptedTokens);
|
|
205
|
+
}
|
|
206
|
+
const activeToken = tokens[0];
|
|
207
|
+
const activeArgsLength = subcommand.args instanceof Array ? subcommand.args.length : 1;
|
|
208
|
+
const allOptions = [...persistentOptions, ...(subcommand.options ?? [])];
|
|
209
|
+
if (activeToken.isOption) {
|
|
210
|
+
const option = getOption(activeToken, allOptions);
|
|
211
|
+
if (option != null) {
|
|
212
|
+
return runOption(tokens, option, subcommand, persistentOptions, acceptedTokens);
|
|
213
|
+
}
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const nextSubcommand = await genSubcommand(activeToken.token, subcommand);
|
|
217
|
+
if (nextSubcommand != null) {
|
|
218
|
+
return runSubcommand(tokens.slice(1), nextSubcommand, getPersistentOptions(persistentOptions, subcommand.options), getPersistentTokens(acceptedTokens.concat(activeToken)));
|
|
219
|
+
}
|
|
220
|
+
if (activeArgsLength <= 0) {
|
|
221
|
+
return; // not subcommand or option & no args exist
|
|
222
|
+
}
|
|
223
|
+
return runArg(tokens, getArgs(subcommand.args), subcommand, allOptions, acceptedTokens, false, false);
|
|
224
|
+
};
|