@kokiito0926/completer 0.0.0 → 0.0.2
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/index.js +22 -13
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
#!/usr/bin/env
|
|
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
|
+
}
|