-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
182 lines (152 loc) · 4.35 KB
/
cli.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env node
import * as assert from "node:assert";
import * as readline from "node:readline";
import { Convention } from "./types";
import { getVersion } from "./version";
import { CONVENTIONS } from "./constants";
import { IS_OPERATIONS } from "./is.operations";
import { FROM_OPERATIONS } from "./from.operations";
function version() {
console.log("version: ", getVersion());
}
function help() {
console.log(
[
"\tconvconv - a tool to work with naming convensions",
"Examples:",
"\tcat file | convconv filter -c kebab",
"\t\tThis command filters lines which have a word which has kebab case convention.",
"\tcat file | convconv convert --from kebab --to camel",
"\t\tThis command converts first word which is kebab case to camel case.",
"",
"Lookup convconv man pages for complete manual",
].join("\n"),
);
}
type Args =
| {
command: "convert";
args: {
from: "auto" | Convention;
to: Convention;
all: boolean;
};
}
| {
command: "filter";
args: {
conventions: Convention[];
};
};
type ConvertArgs = Extract<Args, { command: "convert" }>["args"];
function parseArgs(): Args | void {
const argv = process.argv.slice(2);
if (argv.length === 0) return help();
const showHelp = !!argv.find((p) => ["-h", "--help"].includes(p));
if (showHelp) return help();
const showVersion = !!argv.find((p) => ["-v", "--version"].includes(p));
if (showVersion) return version();
const [command, ...options] = argv[0].startsWith("-")
? ["convert", ...argv]
: argv;
const o = options.map((option) => option.trim());
const obj = {} as Record<string, string | boolean>;
for (let i = 0; i < o.length; i++) {
const key = o[i];
if (i === o.length - 1) {
obj[key] = true;
continue;
}
const value = o[i + 1];
if (value.startsWith("-")) {
obj[key] = true;
continue;
}
obj[key] = value;
i++;
}
try {
switch (command) {
case "convert":
const from = obj["-f"] ?? obj["--from"] ?? "auto";
if (from !== "auto") assert.ok(CONVENTIONS.includes(from as any));
assert.ok(typeof from === "string");
const to = obj["-t"] ?? obj["--to"] ?? "";
assert.ok(CONVENTIONS.includes(to as any));
assert.ok(typeof to === "string");
const all = obj["-a"] ?? obj["--all"] ?? false;
assert.ok(typeof all === "boolean");
return {
command: "convert",
args: {
from: from as Convention,
to: to as Convention,
all,
},
};
case "filter":
const c = obj["-c"] ?? obj["--convention"] ?? "";
assert.ok(typeof c === "string");
const conventions = c.split(",");
conventions.forEach((conv) =>
assert.ok(CONVENTIONS.includes(conv as any)),
);
return {
command: "filter",
args: {
conventions: conventions as Convention[],
},
};
default:
return help();
}
} catch (_) {
return help();
}
}
function findWords(line: string): string[] {
return (line.match(/\b[a-zA-Z0-9\-\_]*\b/g) ?? []).map((w) => w.toString());
}
function filterLine(line: string, conventions: Convention[]) {
let match = false;
findWords(line).forEach((word) =>
conventions.forEach((type) => {
if (IS_OPERATIONS.isConvention(type, word)) match = true;
}),
);
if (match) console.log(line);
}
function filter(types: Convention[]) {
const rl = readline.createInterface(process.stdin);
rl.on("line", (line) => filterLine(line, types));
}
function convertLine(line: string, args: ConvertArgs) {
const words = findWords(line);
for (const word of words) {
try {
line = line.replace(
word,
FROM_OPERATIONS.autoFrom(word).toConvention(args.to),
);
if (!args.all) {
break;
}
} catch (_) {}
}
console.log(line);
}
function convert(args: ConvertArgs) {
const rl = readline.createInterface(process.stdin);
rl.on("line", (line) => convertLine(line, args));
}
function main() {
const parsed = parseArgs();
if (!parsed) return process.exit(1);
switch (parsed.command) {
case "filter":
return filter(parsed.args.conventions);
case "convert":
return convert(parsed.args);
}
}
main();