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