-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (117 loc) · 4.06 KB
/
index.js
File metadata and controls
141 lines (117 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env node
// いちおう、Bashで入力補完が効くようになった。
// タブを1回だけ押したときに入力補完が決まるようになった。
// それから、ファイルのパスの入力補完も効くようになった。
// >> $ complete -C "pseudoalias --config ./example.js --completion" -o default pseudoalias
// >> 2026/01/27 21:36.
// いちおう、入力補完のコマンドを書いておいたほうがいい。
// -oのオプションにdefaultを指定していても、ファイルのパスの入力補完が効かない。
// >> $ complete -p
// >> $ complete -r pseudoalias
// >> $ complete -C "./index.js --config ./example.js --completion" -o default pseudoalias
// >> 2026/01/27 20:53.
// 入力補完の単語を表示する機能を実装した。
// >> $ ./index.js --config ./example.js --completion
// >> 2026/01/27 20:18.
// ホームディレクトリに設定のファイルを保存できるようにする。
// そのようにすれば、コマンドをより短くすることができると思う。
// そのような処理を実装するのであれば、os.homedir関数を用いればいいような気がする。
// >> 2026/01/27 18:33.
// >> $ ./index.js --config example.js
// >> $ ./index.js --config example.js greetings
// >> $ ./index.js --config example.js addition 1 2
// >> $ ./index.js --config ./example.js --register
// >> $ ./index.js greetings
// >> $ ./index.js addition 1 2
// >> $ ./index.js --unregister
import { os, fs, path, argv } from "zx";
import { pathToFileURL } from "node:url";
const CONFIG_DIR = path.join(os.homedir(), ".pseudoalias");
const CONFIG_FILE = path.join(CONFIG_DIR, "config.js");
const commands = argv._;
// console.log(commands);
const config = argv.config;
// console.log(argv);
// process.exit(0);
/*
const completion = argv.completion;
*/
if (argv.register) {
if (argv.config) {
const absConfigPath = path.resolve(argv.config);
const configData = fs.readFileSync(absConfigPath, "utf-8");
await fs.ensureDir(CONFIG_DIR);
await fs.writeFile(CONFIG_FILE, configData, "utf-8");
console.log(`Success: Registered config path to ${absConfigPath}`);
console.log(`Stored in: ${CONFIG_FILE}`);
} else {
console.error("Error: --config is required with --register");
}
process.exit(0);
}
if (argv.unregister) {
if (await fs.exists(CONFIG_DIR)) {
await fs.remove(CONFIG_DIR);
console.log("Success: Unregistered and removed configuration.");
} else {
console.log("Notice: No configuration found to unregister.");
}
process.exit(0);
}
let targetFile = config;
if (!targetFile) {
if (fs.existsSync(CONFIG_FILE)) {
targetFile = CONFIG_FILE;
} else {
process.exit(1);
}
}
// console.log(targetFile);
// process.exit(0);
const targetPath = path.resolve(targetFile);
if (!targetPath) {
process.exit(0);
}
// console.log(targetPath);
// process.exit(0);
try {
const aliasModule = await import(pathToFileURL(targetPath).href);
const [subCommand, ...restArgs] = commands;
// console.log(subCommand);
// console.log(restArgs);
// process.exit(0);
/*
if (completion) {
const args2 = process.argv;
const prevWord = args2[args2.length - 1];
const currentWord = args2[args2.length - 2];
if (prevWord === "pseudoalias") {
const candidates = Object.keys(aliasModule).filter((k) => k !== "default");
const filtered = candidates.filter((c) => c.startsWith(currentWord));
if (filtered.length > 0) {
console.log(filtered.join("\n"));
}
process.exit(0);
}
process.exit(0);
}
*/
if (!subCommand) {
const availableFunctions = Object.keys(aliasModule).filter((k) => k !== "default");
console.log("Available aliases:");
if (availableFunctions.length === 0) {
console.log("Error: Not found aliases.");
} else {
availableFunctions.forEach((fn) => console.log(` - ${fn}`));
}
process.exit(0);
}
if (typeof aliasModule[subCommand] === "function") {
await aliasModule[subCommand](...restArgs);
} else {
console.error(`Error: Command "${subCommand}" is not registered.`);
}
} catch (err) {
console.error("Error: Execution error");
console.error(err);
}