-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b7dfa5
commit a01330a
Showing
14 changed files
with
259 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
.DS_Store | ||
dist/ | ||
bid_output/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { 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"], | ||
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)); | ||
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, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @see https://uga-box.hatenablog.com/entry/2022/01/07/000000 | ||
*/ | ||
class BaseError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
} | ||
} | ||
|
||
export class FileTypeError extends BaseError { | ||
constructor(filename: string) { | ||
super( | ||
`${filename}は想定したファイル形式ではありません。CSVかJSONを使ってください`, | ||
); | ||
} | ||
} | ||
|
||
export class DataPropertyError extends BaseError { | ||
constructor() { | ||
super(`想定していないプロパティが存在します。データを再確認してください`); | ||
} | ||
} | ||
|
||
export class CliError extends BaseError { | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"tasks": { | ||
"build": "deno run --allow-write=dist/ example/main.ts" | ||
"build": "deno run --allow-read --allow-write cli.ts" | ||
}, | ||
"exclude": ["README.md"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,11 @@ export { parse } from "https://deno.land/[email protected]/csv/parse.ts"; | |
export { z } from "https://deno.land/x/[email protected]/mod.ts"; | ||
export { compress } from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
/* --- cli --- */ | ||
export { ensureDir } from "https://deno.land/[email protected]/fs/mod.ts"; | ||
export { parseArgs } from "https://deno.land/[email protected]/cli/parse_args.ts"; | ||
export { walk } from "https://deno.land/[email protected]/fs/walk.ts"; | ||
export { join } from "https://deno.land/[email protected]/path/mod.ts"; | ||
|
||
/* --- test --- */ | ||
export { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts"; |
Oops, something went wrong.