-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.ts
79 lines (65 loc) · 2.39 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
import { compress, ensureDir, join, parseArgs, walk } from "./deps.ts";
import { isValidFileExtention, isValidJson } from "./core/validation.ts";
import {
convertJsonToTextData,
parsedCsvToJson,
readFile,
} from "./core/convert.ts";
import { generateDictionaryFile } from "./core/build.ts";
import { CliError } from "./core/error.ts";
import type { ImeType } from "./model.ts";
const args = parseArgs(Deno.args, {
boolean: ["all", "google", "macos", "microsoft", "gboard", "compress"],
string: ["dir"],
});
if (!args.dir) throw new CliError("--dirでディレクトリを指定してください");
for await (const dirEntry of walk(join(Deno.cwd(), args.dir))) {
if (dirEntry.isFile) {
const isValidFileExtentionResult = isValidFileExtention(dirEntry.name);
if (!isValidFileExtentionResult.success) {
throw isValidFileExtentionResult.error;
}
let data: Record<string, string | undefined>[] = [];
switch (isValidFileExtentionResult.result) {
case ".csv": {
data = parsedCsvToJson(await readFile(dirEntry.path));
break;
}
default:
data = JSON.parse(await readFile(dirEntry.path)).dictionaries;
break;
}
const isValidJsonResult = isValidJson(data);
if (!isValidJsonResult.success) {
throw isValidJsonResult.error;
}
let imeTypeList: ImeType[] = [];
if (args.all) {
imeTypeList = ["Google IME", "macOS IME", "Microsoft IME", "GBoard"];
} else {
if (args.google) imeTypeList.push("Google IME");
if (args.macos) imeTypeList.push("macOS IME");
if (args.microsoft) imeTypeList.push("Microsoft IME");
if (args.gboard) imeTypeList.push("GBoard");
if (imeTypeList.length === 0) {
throw new CliError("--allや--googleなどでIMEを指定してください");
}
}
const OUTPUT_FILE_PREFIX = dirEntry.name.split(".").at(0)!;
const OUTPUT_BASE_DIR_NAME = "bid_output";
const OUTPUT_DIR_NAME = join(OUTPUT_BASE_DIR_NAME, OUTPUT_FILE_PREFIX);
await ensureDir(OUTPUT_DIR_NAME);
for await (const imeType of imeTypeList) {
await generateDictionaryFile(
convertJsonToTextData(isValidJsonResult.result, imeType),
`${OUTPUT_DIR_NAME}/${OUTPUT_FILE_PREFIX}-${
imeType.toLowerCase().replace(" ", "")
}.txt`,
imeType,
);
}
}
}
if (args.compress) {
await compress("bid_output", "bid_output_archive");
}