@kokiito0926/completer 0.0.1 → 0.0.3
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/README.md +48 -1
- package/index.js +21 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
## completer
|
|
2
2
|
|
|
3
|
-
completer
|
|
3
|
+
completerは、コマンドライン引数の入力補完をサポートするためのツールです。
|
|
4
|
+
設定ファイルで定義されたツリー構造に基づいて、次に続くべき引数の候補を提示します。
|
|
5
|
+
|
|
6
|
+
## インストール
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install --global @kokiito0926/completer
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 使用方法
|
|
13
|
+
|
|
14
|
+
### コマンド
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
completer --config <設定ファイルへのパス> [入力途中のコマンド...]
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 設定ファイル
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
export default {
|
|
24
|
+
apps: {
|
|
25
|
+
list: [],
|
|
26
|
+
info: ["--json", "--short"],
|
|
27
|
+
delete: ["app-1", "app-2", "app-3"],
|
|
28
|
+
},
|
|
29
|
+
config: {
|
|
30
|
+
set: ["port", "host", "timeout"],
|
|
31
|
+
get: ["port", "host", "timeout"],
|
|
32
|
+
},
|
|
33
|
+
deploy: ["production", "staging"],
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 実行例
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
$ completer --config ./example.js apps
|
|
41
|
+
list
|
|
42
|
+
info
|
|
43
|
+
delete
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
$ completer --config ./example.js apps info
|
|
48
|
+
--json
|
|
49
|
+
--short
|
|
50
|
+
```
|
|
4
51
|
|
|
5
52
|
## ライセンス
|
|
6
53
|
|
package/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// >> $ ./index.js --config ./example.js apps
|
|
4
|
+
// >> $ ./index.js --config ./example.js apps info
|
|
5
|
+
// >> $ ./index.js --config ./example.js apps info --json
|
|
6
|
+
// >> $ ./index.js --config ./example.js apps info --short
|
|
4
7
|
|
|
5
8
|
import { fs, path, argv } from "zx";
|
|
6
9
|
import { pathToFileURL } from "node:url";
|
|
@@ -15,23 +18,28 @@ if (!configPath) {
|
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
const config = await import(pathToFileURL(configPath).href);
|
|
18
|
-
|
|
21
|
+
const root = config?.default;
|
|
22
|
+
if (root === undefined) {
|
|
23
|
+
// console.error("Error: Config file must have a default export.");
|
|
19
24
|
process.exit(1);
|
|
20
25
|
}
|
|
21
|
-
// console.log(config);
|
|
22
|
-
// console.log(config?.default);
|
|
23
|
-
// process.exit(1);
|
|
24
26
|
|
|
25
|
-
const
|
|
27
|
+
const rawWords = process.argv.slice(2);
|
|
28
|
+
const words = [];
|
|
29
|
+
for (let i = 0; i < rawWords.length; i++) {
|
|
30
|
+
if (rawWords[i] === "--config") {
|
|
31
|
+
i++; // Skip the flag value
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
words.push(rawWords[i]);
|
|
35
|
+
}
|
|
36
|
+
|
|
26
37
|
const currentWord = words[words.length - 1] || "";
|
|
27
38
|
const previousWords = words.slice(0, -1);
|
|
28
|
-
// console.log(words);
|
|
29
|
-
// console.log(currentWord);
|
|
30
|
-
// console.log(previousWords);
|
|
31
39
|
|
|
32
|
-
let currentNode =
|
|
40
|
+
let currentNode = root;
|
|
33
41
|
for (const word of previousWords) {
|
|
34
|
-
if (currentNode
|
|
42
|
+
if (currentNode && typeof currentNode === "object" && Object.hasOwn(currentNode, word)) {
|
|
35
43
|
currentNode = currentNode[word];
|
|
36
44
|
} else {
|
|
37
45
|
currentNode = null;
|
|
@@ -47,7 +55,8 @@ if (currentNode) {
|
|
|
47
55
|
candidates = Object.keys(currentNode);
|
|
48
56
|
}
|
|
49
57
|
}
|
|
50
|
-
// console.log(candidates)
|
|
51
58
|
|
|
52
59
|
const filtered = candidates.filter((c) => c.startsWith(currentWord));
|
|
53
|
-
|
|
60
|
+
if (filtered.length > 0) {
|
|
61
|
+
console.log(filtered.join("\n"));
|
|
62
|
+
}
|