cli-macro 1.0.1 → 1.1.0
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 +1 -3
- package/index.js +48 -20
- package/package.json +1 -1
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -1,21 +1,38 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const os = require("os");
|
|
4
6
|
const { spawnSync } = require('child_process');
|
|
5
7
|
const prompt = require('prompt-sync')();
|
|
6
8
|
|
|
7
9
|
const argv = process.argv;
|
|
8
10
|
const command = argv[2];
|
|
9
|
-
|
|
11
|
+
let filename = argv[3];
|
|
12
|
+
|
|
13
|
+
// 홈 디렉토리 경로
|
|
14
|
+
const homeDir = os.homedir();
|
|
15
|
+
|
|
16
|
+
// 파일명에 .clim 확장자 추가/처리
|
|
17
|
+
function getClimFilePath(name) {
|
|
18
|
+
if (!name.endsWith(".clim")) {
|
|
19
|
+
name = name + ".clim";
|
|
20
|
+
}
|
|
21
|
+
return path.join(homeDir, name);
|
|
22
|
+
}
|
|
10
23
|
|
|
11
24
|
// 기본 사용법 안내
|
|
12
25
|
if (!command) {
|
|
13
|
-
console.log("Usage: clim <new|n|run|r|edit|e|delete|d|list|l>
|
|
26
|
+
console.log("Usage: clim <new|n|run|r|edit|e|delete|d|list|l> [filename]");
|
|
14
27
|
process.exit(1);
|
|
15
28
|
}
|
|
16
29
|
|
|
17
30
|
if (command === "new" || command === "n") {
|
|
18
31
|
// 새 매크로 파일 생성
|
|
32
|
+
if (!filename) {
|
|
33
|
+
console.error("Filename required for new command.");
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
19
36
|
const lines = [];
|
|
20
37
|
while (true) {
|
|
21
38
|
const index = lines.length + 1;
|
|
@@ -26,15 +43,21 @@ if (command === "new" || command === "n") {
|
|
|
26
43
|
}
|
|
27
44
|
lines.push(inputLine);
|
|
28
45
|
}
|
|
29
|
-
|
|
30
|
-
|
|
46
|
+
const filepath = getClimFilePath(filename);
|
|
47
|
+
fs.writeFileSync(filepath, lines.join("\n"));
|
|
48
|
+
console.log(`${path.basename(filepath)} was saved`);
|
|
31
49
|
} else if (command === "run" || command === "r") {
|
|
32
50
|
// 매크로 파일 실행
|
|
33
|
-
if (!
|
|
34
|
-
console.error(
|
|
51
|
+
if (!filename) {
|
|
52
|
+
console.error("Filename required for run command.");
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
const filepath = getClimFilePath(filename);
|
|
56
|
+
if (!fs.existsSync(filepath)) {
|
|
57
|
+
console.error(`${path.basename(filepath)} not found`);
|
|
35
58
|
process.exit(1);
|
|
36
59
|
}
|
|
37
|
-
const macro = fs.readFileSync(
|
|
60
|
+
const macro = fs.readFileSync(filepath, "utf-8");
|
|
38
61
|
const commands = macro.split(/\r?\n/).filter(line => line.trim() !== "");
|
|
39
62
|
for (const cmd of commands) {
|
|
40
63
|
console.log(`> ${cmd}`);
|
|
@@ -53,12 +76,13 @@ if (command === "new" || command === "n") {
|
|
|
53
76
|
console.error("Filename required for edit command.");
|
|
54
77
|
process.exit(1);
|
|
55
78
|
}
|
|
56
|
-
const
|
|
57
|
-
|
|
79
|
+
const filepath = getClimFilePath(filename);
|
|
80
|
+
const lines = fs.existsSync(filepath)
|
|
81
|
+
? fs.readFileSync(filepath, "utf-8").split(/\r?\n/)
|
|
58
82
|
: [];
|
|
59
83
|
|
|
60
84
|
while (true) {
|
|
61
|
-
console.log(`\n=== ${
|
|
85
|
+
console.log(`\n=== ${path.basename(filepath)} ===`);
|
|
62
86
|
if (lines.length === 0) {
|
|
63
87
|
console.log("(empty file)");
|
|
64
88
|
} else {
|
|
@@ -89,8 +113,8 @@ if (command === "new" || command === "n") {
|
|
|
89
113
|
console.log("Please enter a valid line number.");
|
|
90
114
|
}
|
|
91
115
|
} else if (action === "s") {
|
|
92
|
-
fs.writeFileSync(
|
|
93
|
-
console.log(`${
|
|
116
|
+
fs.writeFileSync(filepath, lines.join("\n"));
|
|
117
|
+
console.log(`${path.basename(filepath)} was saved`);
|
|
94
118
|
break;
|
|
95
119
|
} else if (action === "q") {
|
|
96
120
|
console.log("Edit cancelled.");
|
|
@@ -99,23 +123,27 @@ if (command === "new" || command === "n") {
|
|
|
99
123
|
console.log("Unknown command. Try again.");
|
|
100
124
|
}
|
|
101
125
|
}
|
|
102
|
-
} else if (command === "delete" || command === "
|
|
126
|
+
} else if (command === "delete" || command === "d") {
|
|
103
127
|
// 파일 삭제 기능
|
|
104
128
|
if (!filename) {
|
|
105
129
|
console.error("Filename required for delete command.");
|
|
106
130
|
process.exit(1);
|
|
107
131
|
}
|
|
108
|
-
|
|
109
|
-
|
|
132
|
+
const filepath = getClimFilePath(filename);
|
|
133
|
+
if (!fs.existsSync(filepath)) {
|
|
134
|
+
console.error(`${path.basename(filepath)} not found`);
|
|
110
135
|
process.exit(1);
|
|
111
136
|
}
|
|
112
|
-
fs.unlinkSync(
|
|
113
|
-
console.log(`${
|
|
137
|
+
fs.unlinkSync(filepath);
|
|
138
|
+
console.log(`${path.basename(filepath)} was deleted`);
|
|
114
139
|
} else if (command === "list" || command === "ls") {
|
|
115
|
-
// 파일 목록 보기 기능
|
|
116
|
-
const files = fs.readdirSync(
|
|
140
|
+
// 파일 목록 보기 기능 (.clim 파일만)
|
|
141
|
+
const files = fs.readdirSync(homeDir).filter(file => {
|
|
142
|
+
const fullPath = path.join(homeDir, file);
|
|
143
|
+
return fs.statSync(fullPath).isFile() && file.endsWith(".clim");
|
|
144
|
+
});
|
|
117
145
|
if (files.length === 0) {
|
|
118
|
-
console.log("No files found.");
|
|
146
|
+
console.log("No .clim files found.");
|
|
119
147
|
} else {
|
|
120
148
|
files.forEach(file => console.log(file));
|
|
121
149
|
}
|