diff --git a/.gitignore b/.gitignore index c3c5eb9..fcc326b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .DS_Store -dist/ \ No newline at end of file +bid_output/ \ No newline at end of file diff --git a/README.md b/README.md index 5805d5d..8036380 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,10 @@ Created by Deno. ## Feature - [ ] API -- [ ] CLI +- [x] CLI - [ ] Web App -## Build dictionary workflow sample +## Build dictionary workflow example ``` yml name: Build IME dictionary on: @@ -48,11 +48,12 @@ jobs: deno-version: v1.x - name: Output dictionary data run: | - deno task build + deno install --allow-read --allow-write -n bid https://deno.land/x/bid/cli.ts + bid --dir=test/mock --all - name: Archive production artifacts uses: actions/upload-artifact@v2 with: name: dictionary - path: dist + path: bid_output retention-days: 30 ``` diff --git a/cli.ts b/cli.ts new file mode 100644 index 0000000..9a9d20d --- /dev/null +++ b/cli.ts @@ -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[] = []; + + 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, + ); + } + } +} diff --git a/core/convert.ts b/core/convert.ts index 5e3c2ed..cb1a053 100644 --- a/core/convert.ts +++ b/core/convert.ts @@ -93,3 +93,9 @@ export const convertJsonToTextData = ( return insertDelimiter(orderd, imeType).join(""); }).join(NEW_LINE); }; + +export const readFile = async (path: string): Promise => { + const decoder = new TextDecoder(); + const data = await Deno.readFile(path); + return decoder.decode(data); +}; diff --git a/core/error.ts b/core/error.ts new file mode 100644 index 0000000..53e55e6 --- /dev/null +++ b/core/error.ts @@ -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); + } +} diff --git a/core/validation.ts b/core/validation.ts index fa996d7..477d269 100644 --- a/core/validation.ts +++ b/core/validation.ts @@ -7,6 +7,7 @@ import type { YesOrNo, } from "../model.ts"; import { COMMA, TAB } from "./config.ts"; +import { DataPropertyError, FileTypeError } from "./error.ts"; /** * ファイル名がCSVかJSONか確認する @@ -24,7 +25,7 @@ export const isValidFileExtention = (filename: string): Result => { } else { return { success: false, - error: new Error("File Type Error"), + error: new FileTypeError(filename), }; } }; @@ -49,7 +50,7 @@ export const isValidJson = ( if (!InputuserDictionarySchema.safeParse(jsonData).success) { return { success: false, - error: new Error("Data Proptry Error"), + error: new DataPropertyError(), }; } diff --git a/deno.jsonc b/deno.jsonc index c95dcb8..36e7776 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -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"] } diff --git a/deno.lock b/deno.lock index fe82da4..6cb824c 100644 --- a/deno.lock +++ b/deno.lock @@ -72,9 +72,107 @@ "https://deno.land/std@0.210.0/assert/assert_equals.ts": "991b0c2b437a015d623654f758e48bfd931068211a52e8131b397cdf005c595f", "https://deno.land/std@0.210.0/assert/assertion_error.ts": "26ed1863d905005f00785c89750c001c3522c5417e4f58f95044b8143cfc1593", "https://deno.land/std@0.210.0/assert/equal.ts": "6f81c8a3b12c08bdc3510c8a1293b4db1c083692219d7e3828d2234b448d3d3d", + "https://deno.land/std@0.210.0/cli/parse_args.ts": "9bea02050b3f302e706871ff87ecfa3ad82cc34249adbe0dcddfaac75bdb48ff", "https://deno.land/std@0.210.0/csv/_io.ts": "5bfe70bfaae7bf642f0f178cbcd6fbd8105bb63ee3a487ee866009c2b13befc5", "https://deno.land/std@0.210.0/csv/parse.ts": "6902634f66b3ecfcb953d26e59f465ef94f076ac2906cb407f72719673030613", + "https://deno.land/std@0.210.0/datetime/_common.ts": "32ab92a6a9353e2f7ee23a30f0dfbd458b750e1c6fd71af1ec83f93a8f7c268a", + "https://deno.land/std@0.210.0/datetime/format.ts": "2d7a430ca9571e054ac181dcb950faf9ac23445e081dcb230ca37134e6eaad0c", "https://deno.land/std@0.210.0/fmt/colors.ts": "2685c524bef9b16b3059a417daf6860c754eb755e19e812762ef5dff62f24481", + "https://deno.land/std@0.210.0/fs/_create_walk_entry.ts": "7462542d30c53e925b07c4243929434e78a190d31987a3388c8e0bd52ef8e440", + "https://deno.land/std@0.210.0/fs/_get_file_info_type.ts": "9c92ff74f39bfba66b09faf9901af6a944cc71f014d9649e7d3e32eac4aeddfc", + "https://deno.land/std@0.210.0/fs/_is_same_path.ts": "9ba34405c53da1e1bd9635785da85b21e9593698369dbb675dfac33c30f183f4", + "https://deno.land/std@0.210.0/fs/_is_subdir.ts": "65525686538138cdfc7e9bc24d9c8397cd28342f8395f0c472989c6787ff6749", + "https://deno.land/std@0.210.0/fs/_to_path_string.ts": "d963898a0d277b155eefb5c64202de71ca2f5e1348897a2f56d3bce6347fc3cc", + "https://deno.land/std@0.210.0/fs/copy.ts": "2a7ccfd78976fb624699bd8dec214bc492804b29414c80e7c29955526598b57b", + "https://deno.land/std@0.210.0/fs/empty_dir.ts": "a0d35035b604fbbdedee9a8fe28b7436ae0f32a111e9e88fe0469b93b8548940", + "https://deno.land/std@0.210.0/fs/ensure_dir.ts": "f797d9bf39aa2a796923a62b0e10d864998cec396f4712f5c869f3d0d8a6bfd3", + "https://deno.land/std@0.210.0/fs/ensure_file.ts": "2498a930983c33a51152d4a6a47b161711ba3f2290879cd1f89a9a8e23f2fbc1", + "https://deno.land/std@0.210.0/fs/ensure_link.ts": "1121ac4cfd24e6d20206c7d4dbdc6b02ed9b9f6550447b590a077c941e3c456d", + "https://deno.land/std@0.210.0/fs/ensure_symlink.ts": "d01debcdf62a5fdb0b5d70a516713cec3628a8ae5eddd99cb278305a4fd98091", + "https://deno.land/std@0.210.0/fs/eol.ts": "27516caa5b0f703bc67817493d07b9eb861b1a8bb8dfe52f73a011ae6e58e7d5", + "https://deno.land/std@0.210.0/fs/exists.ts": "da38bab3cd416131a2075c1a532c69b0b634c2ce8d75222355aa6d3d15995199", + "https://deno.land/std@0.210.0/fs/expand_glob.ts": "9b68f6146a106c8c2ce8f8794ce07678fcb232536222929dded8acecab6f8782", + "https://deno.land/std@0.210.0/fs/mod.ts": "bc3d0acd488cc7b42627044caf47d72019846d459279544e1934418955ba4898", + "https://deno.land/std@0.210.0/fs/move.ts": "ec2c5ae4a5cbef32c95af7171b4296d4c1ef414fbcebf473f00d1682c6f6c708", + "https://deno.land/std@0.210.0/fs/walk.ts": "78131d81952231f173e01aa1c1d69485e627bf4bb3af020a1c1b2ad490892c12", + "https://deno.land/std@0.210.0/path/_common/assert_path.ts": "061e4d093d4ba5aebceb2c4da3318bfe3289e868570e9d3a8e327d91c2958946", + "https://deno.land/std@0.210.0/path/_common/basename.ts": "0d978ff818f339cd3b1d09dc914881f4d15617432ae519c1b8fdc09ff8d3789a", + "https://deno.land/std@0.210.0/path/_common/common.ts": "9e4233b2eeb50f8b2ae10ecc2108f58583aea6fd3e8907827020282dc2b76143", + "https://deno.land/std@0.210.0/path/_common/constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0", + "https://deno.land/std@0.210.0/path/_common/dirname.ts": "2ba7fb4cc9fafb0f38028f434179579ce61d4d9e51296fad22b701c3d3cd7397", + "https://deno.land/std@0.210.0/path/_common/format.ts": "11aa62e316dfbf22c126917f5e03ea5fe2ee707386555a8f513d27ad5756cf96", + "https://deno.land/std@0.210.0/path/_common/from_file_url.ts": "ef1bf3197d2efbf0297a2bdbf3a61d804b18f2bcce45548ae112313ec5be3c22", + "https://deno.land/std@0.210.0/path/_common/glob_to_reg_exp.ts": "50386887d6041f15741d0013a703ee63ef673983d465d3a0c9c190e95f8da4fe", + "https://deno.land/std@0.210.0/path/_common/normalize.ts": "2ba7fb4cc9fafb0f38028f434179579ce61d4d9e51296fad22b701c3d3cd7397", + "https://deno.land/std@0.210.0/path/_common/normalize_string.ts": "88c472f28ae49525f9fe82de8c8816d93442d46a30d6bb5063b07ff8a89ff589", + "https://deno.land/std@0.210.0/path/_common/relative.ts": "1af19d787a2a84b8c534cc487424fe101f614982ae4851382c978ab2216186b4", + "https://deno.land/std@0.210.0/path/_common/strip_trailing_separators.ts": "7ffc7c287e97bdeeee31b155828686967f222cd73f9e5780bfe7dfb1b58c6c65", + "https://deno.land/std@0.210.0/path/_common/to_file_url.ts": "a8cdd1633bc9175b7eebd3613266d7c0b6ae0fb0cff24120b6092ac31662f9ae", + "https://deno.land/std@0.210.0/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b", + "https://deno.land/std@0.210.0/path/_os.ts": "30b0c2875f360c9296dbe6b7f2d528f0f9c741cecad2e97f803f5219e91b40a2", + "https://deno.land/std@0.210.0/path/basename.ts": "04bb5ef3e86bba8a35603b8f3b69537112cdd19ce64b77f2522006da2977a5f3", + "https://deno.land/std@0.210.0/path/common.ts": "f4d061c7d0b95a65c2a1a52439edec393e906b40f1caf4604c389fae7caa80f5", + "https://deno.land/std@0.210.0/path/dirname.ts": "88a0a71c21debafc4da7a4cd44fd32e899462df458fbca152390887d41c40361", + "https://deno.land/std@0.210.0/path/extname.ts": "8c6d6112bce335b4d3d5a07cb0451816d0c2094c147049874fca2db5f707044b", + "https://deno.land/std@0.210.0/path/format.ts": "3457530cc85d1b4bab175f9ae73998b34fd456c830d01883169af0681b8894fb", + "https://deno.land/std@0.210.0/path/from_file_url.ts": "e7fa233ea1dff9641e8d566153a24d95010110185a6f418dd2e32320926043f8", + "https://deno.land/std@0.210.0/path/glob.ts": "a00a81a55c02bbe074ab21a50b6495c6f7795f54cd718c824adaa92c6c9b7419", + "https://deno.land/std@0.210.0/path/glob_to_regexp.ts": "74d7448c471e293d03f05ccb968df4365fed6aaa508506b6325a8efdc01d8271", + "https://deno.land/std@0.210.0/path/is_absolute.ts": "67232b41b860571c5b7537f4954c88d86ae2ba45e883ee37d3dec27b74909d13", + "https://deno.land/std@0.210.0/path/is_glob.ts": "567dce5c6656bdedfc6b3ee6c0833e1e4db2b8dff6e62148e94a917f289c06ad", + "https://deno.land/std@0.210.0/path/join.ts": "3ee91038e3eaa966897eddda43d5207d7cae5c2de8a658bdbd722e8f8f29206a", + "https://deno.land/std@0.210.0/path/join_globs.ts": "9b84d5103b63d3dbed4b2cf8b12477b2ad415c7d343f1488505162dc0e5f4db8", + "https://deno.land/std@0.210.0/path/mod.ts": "eff1d7b0617293bd90254d379a7266887dc6fbf5a00e0f450eeb854959379294", + "https://deno.land/std@0.210.0/path/normalize.ts": "aa95be9a92c7bd4f9dc0ba51e942a1973e2b93d266cd74f5ca751c136d520b66", + "https://deno.land/std@0.210.0/path/normalize_glob.ts": "674baa82e1c00b6cb153bbca36e06f8e0337cb8062db6d905ab5de16076ca46b", + "https://deno.land/std@0.210.0/path/parse.ts": "d87ff0deef3fb495bc0d862278ff96da5a06acf0625ca27769fc52ac0d3d6ece", + "https://deno.land/std@0.210.0/path/posix/_util.ts": "ecf49560fedd7dd376c6156cc5565cad97c1abe9824f4417adebc7acc36c93e5", + "https://deno.land/std@0.210.0/path/posix/basename.ts": "a630aeb8fd8e27356b1823b9dedd505e30085015407caa3396332752f6b8406a", + "https://deno.land/std@0.210.0/path/posix/common.ts": "e781d395dc76f6282e3f7dd8de13194abb8b04a82d109593141abc6e95755c8b", + "https://deno.land/std@0.210.0/path/posix/dirname.ts": "f48c9c42cc670803b505478b7ef162c7cfa9d8e751b59d278b2ec59470531472", + "https://deno.land/std@0.210.0/path/posix/extname.ts": "ee7f6571a9c0a37f9218fbf510c440d1685a7c13082c348d701396cc795e0be0", + "https://deno.land/std@0.210.0/path/posix/format.ts": "b94876f77e61bfe1f147d5ccb46a920636cd3cef8be43df330f0052b03875968", + "https://deno.land/std@0.210.0/path/posix/from_file_url.ts": "b97287a83e6407ac27bdf3ab621db3fccbf1c27df0a1b1f20e1e1b5acf38a379", + "https://deno.land/std@0.210.0/path/posix/glob_to_regexp.ts": "6ed00c71fbfe0ccc35977c35444f94e82200b721905a60bd1278b1b768d68b1a", + "https://deno.land/std@0.210.0/path/posix/is_absolute.ts": "159900a3422d11069d48395568217eb7fc105ceda2683d03d9b7c0f0769e01b8", + "https://deno.land/std@0.210.0/path/posix/is_glob.ts": "ec4fbc604b9db8487f7b56ab0e759b24a971ab6a45f7b0b698bc39b8b9f9680f", + "https://deno.land/std@0.210.0/path/posix/join.ts": "0c0d84bdc344876930126640011ec1b888e6facf74153ffad9ef26813aa2a076", + "https://deno.land/std@0.210.0/path/posix/join_globs.ts": "f4838d54b1f60a34a40625a3293f6e583135348be1b2974341ac04743cb26121", + "https://deno.land/std@0.210.0/path/posix/mod.ts": "f1b08a7f64294b7de87fc37190d63b6ce5b02889af9290c9703afe01951360ae", + "https://deno.land/std@0.210.0/path/posix/normalize.ts": "11de90a94ab7148cc46e5a288f7d732aade1d616bc8c862f5560fa18ff987b4b", + "https://deno.land/std@0.210.0/path/posix/normalize_glob.ts": "10a1840c628ebbab679254d5fa1c20e59106102354fb648a1765aed72eb9f3f9", + "https://deno.land/std@0.210.0/path/posix/parse.ts": "199208f373dd93a792e9c585352bfc73a6293411bed6da6d3bc4f4ef90b04c8e", + "https://deno.land/std@0.210.0/path/posix/relative.ts": "e2f230608b0f083e6deaa06e063943e5accb3320c28aef8d87528fbb7fe6504c", + "https://deno.land/std@0.210.0/path/posix/resolve.ts": "51579d83159d5c719518c9ae50812a63959bbcb7561d79acbdb2c3682236e285", + "https://deno.land/std@0.210.0/path/posix/separator.ts": "0b6573b5f3269a3164d8edc9cefc33a02dd51003731c561008c8bb60220ebac1", + "https://deno.land/std@0.210.0/path/posix/to_file_url.ts": "ac5499aa0c6e2c266019cba7d1f7e5a92b8e04983cd72be97f81adad185619a6", + "https://deno.land/std@0.210.0/path/posix/to_namespaced_path.ts": "c9228a0e74fd37e76622cd7b142b8416663a9b87db643302fa0926b5a5c83bdc", + "https://deno.land/std@0.210.0/path/relative.ts": "23d45ede8b7ac464a8299663a43488aad6b561414e7cbbe4790775590db6349c", + "https://deno.land/std@0.210.0/path/resolve.ts": "5b184efc87155a0af9fa305ff68a109e28de9aee81fc3e77cd01380f19daf867", + "https://deno.land/std@0.210.0/path/separator.ts": "1a21ffd408bfaa317bffff604e5a799f78a7a5571590bde6b9cdce7685953d74", + "https://deno.land/std@0.210.0/path/to_file_url.ts": "edaafa089e0bce386e1b2d47afe7c72e379ff93b28a5829a5885e4b6c626d864", + "https://deno.land/std@0.210.0/path/to_namespaced_path.ts": "cf8734848aac3c7527d1689d2adf82132b1618eff3cc523a775068847416b22a", + "https://deno.land/std@0.210.0/path/windows/_util.ts": "f32b9444554c8863b9b4814025c700492a2b57ff2369d015360970a1b1099d54", + "https://deno.land/std@0.210.0/path/windows/basename.ts": "8a9dbf7353d50afbc5b221af36c02a72c2d1b2b5b9f7c65bf6a5a2a0baf88ad3", + "https://deno.land/std@0.210.0/path/windows/common.ts": "e781d395dc76f6282e3f7dd8de13194abb8b04a82d109593141abc6e95755c8b", + "https://deno.land/std@0.210.0/path/windows/dirname.ts": "5c2aa541384bf0bd9aca821275d2a8690e8238fa846198ef5c7515ce31a01a94", + "https://deno.land/std@0.210.0/path/windows/extname.ts": "07f4fa1b40d06a827446b3e3bcc8d619c5546b079b8ed0c77040bbef716c7614", + "https://deno.land/std@0.210.0/path/windows/format.ts": "343019130d78f172a5c49fdc7e64686a7faf41553268961e7b6c92a6d6548edf", + "https://deno.land/std@0.210.0/path/windows/from_file_url.ts": "d53335c12b0725893d768be3ac6bf0112cc5b639d2deb0171b35988493b46199", + "https://deno.land/std@0.210.0/path/windows/glob_to_regexp.ts": "290755e18ec6c1a4f4d711c3390537358e8e3179581e66261a0cf348b1a13395", + "https://deno.land/std@0.210.0/path/windows/is_absolute.ts": "245b56b5f355ede8664bd7f080c910a97e2169972d23075554ae14d73722c53c", + "https://deno.land/std@0.210.0/path/windows/is_glob.ts": "ec4fbc604b9db8487f7b56ab0e759b24a971ab6a45f7b0b698bc39b8b9f9680f", + "https://deno.land/std@0.210.0/path/windows/join.ts": "e6600bf88edeeef4e2276e155b8de1d5dec0435fd526ba2dc4d37986b2882f16", + "https://deno.land/std@0.210.0/path/windows/join_globs.ts": "f4838d54b1f60a34a40625a3293f6e583135348be1b2974341ac04743cb26121", + "https://deno.land/std@0.210.0/path/windows/mod.ts": "d7040f461465c2c21c1c68fc988ef0bdddd499912138cde3abf6ad60c7fb3814", + "https://deno.land/std@0.210.0/path/windows/normalize.ts": "9deebbf40c81ef540b7b945d4ccd7a6a2c5a5992f791e6d3377043031e164e69", + "https://deno.land/std@0.210.0/path/windows/normalize_glob.ts": "344ff5ed45430495b9a3d695567291e50e00b1b3b04ea56712a2acf07ab5c128", + "https://deno.land/std@0.210.0/path/windows/parse.ts": "120faf778fe1f22056f33ded069b68e12447668fcfa19540c0129561428d3ae5", + "https://deno.land/std@0.210.0/path/windows/relative.ts": "026855cd2c36c8f28f1df3c6fbd8f2449a2aa21f48797a74700c5d872b86d649", + "https://deno.land/std@0.210.0/path/windows/resolve.ts": "5ff441ab18a2346abadf778121128ee71bda4d0898513d4639a6ca04edca366b", + "https://deno.land/std@0.210.0/path/windows/separator.ts": "ae21f27015f10510ed1ac4a0ba9c4c9c967cbdd9d9e776a3e4967553c397bd5d", + "https://deno.land/std@0.210.0/path/windows/to_file_url.ts": "8e9ea9e1ff364aa06fa72999204229952d0a279dbb876b7b838b2b2fea55cce3", + "https://deno.land/std@0.210.0/path/windows/to_namespaced_path.ts": "e0f4d4a5e77f28a5708c1a33ff24360f35637ba6d8f103d19661255ef7bfd50d", "https://deno.land/x/zip@v1.2.5/compress.ts": "43d9f4440960d15a85aec58f5d365acc25530d3d4186b2f5f896c090ecac20e8", "https://deno.land/x/zip@v1.2.5/decompress.ts": "0bce3d453726f686274fab3f6c19b72b5e74223a00d89c176b1de49a5dd5528d", "https://deno.land/x/zip@v1.2.5/deps.ts": "79548387594b3ae1efaaa870b5a507c4d6bedede13dbd5d4ad42f6cda0aeef86", diff --git a/deps.ts b/deps.ts index 417c357..99b86af 100644 --- a/deps.ts +++ b/deps.ts @@ -4,5 +4,11 @@ export { parse } from "https://deno.land/std@0.210.0/csv/parse.ts"; export { z } from "https://deno.land/x/zod@v3.22.4/mod.ts"; export { compress } from "https://deno.land/x/zip@v1.2.5/mod.ts"; +/* --- cli --- */ +export { ensureDir } from "https://deno.land/std@0.210.0/fs/mod.ts"; +export { parseArgs } from "https://deno.land/std@0.210.0/cli/parse_args.ts"; +export { walk } from "https://deno.land/std@0.210.0/fs/walk.ts"; +export { join } from "https://deno.land/std@0.210.0/path/mod.ts"; + /* --- test --- */ export { assertEquals } from "https://deno.land/std@0.210.0/assert/assert_equals.ts"; diff --git a/example/main.ts b/example/main.ts deleted file mode 100644 index aa16461..0000000 --- a/example/main.ts +++ /dev/null @@ -1,267 +0,0 @@ -import { buildDictionaryFile, combineDictionary } from "../mod.ts"; -import type { - BuildDictionaryFileOptions, - CombineDictionaries, - Dictionaries, -} from "../model.ts"; - -const DEVELOPMENT: Dictionaries = [ - { - type: "固有名詞", - word: "Mastodon", - reading: "ますとどん", - }, - { - type: "固有名詞", - word: "nginx", - reading: "えんじんえっくす", - }, - { - type: "固有名詞", - word: "Visual Studio Code", - reading: "ぶいえすこーど", - }, - { - type: "固有名詞", - word: "VS Code", - reading: "ぶいえすこーど", - }, - { - type: "固有名詞", - word: "ECAMScript", - reading: "えくますくりぷと", - }, -]; - -const KAOMOJI: Dictionaries = [ - { - type: "顔文字", - word: "✌(’ω’✌ )三✌(’ω’)✌三( ✌’ω’)✌", - reading: "いえーい", - }, - { - type: "顔文字", - word: "(°∀。)y─┛~~", - reading: "ういうい", - }, - { - type: "顔文字", - word: "(๑´╹‸╹`๑)", - reading: "うーん", - }, - { - type: "顔文字", - word: "(`0言0´*)ヴェアアア", - reading: "ゔぇあ", - }, - { - type: "顔文字", - word: "(`0言0´*)ヴェアアア", - reading: "ゔえあ", - }, - { - type: "顔文字", - word: "三 ヾ(⌒(厂 ˙ω˙ )厂 ウェーイ", - reading: "うぇーい", - }, - { - type: "顔文字", - word: "\( 'ω')/ウオオアアアアアアアアアアアアアアアアーッッッッッッッッ!!", - reading: "うおあー", - }, - { - type: "顔文字", - word: "_(:3」∠)_", - reading: "うだぁ", - }, - { - type: "顔文字", - word: "(‘、3_ヽ)_", - reading: "うだぁ", - }, - { - type: "顔文字", - word: "_('ω' _)⌒)_", - reading: "うだぁ", - }, - { - type: "顔文字", - word: "(´Д`;)", - reading: "うへぇ", - }, - { - type: "顔文字", - word: "_(´ཀ`」 ∠)_", - reading: "うぼあ", - }, - { - type: "顔文字", - word: "▂▅▇█▓▒░('ω')░▒▓█▇▅▂うわあああああああ", - reading: "うわああ", - }, - { - type: "顔文字", - word: "٩( 'ω' )و", - reading: "えいおー", - }, - { - type: "顔文字", - word: "\\\\\\٩( 'ω' )و ////", - reading: "えいおー", - }, - { - type: "顔文字", - word: "(^ω^ 三 ^ω^)", - reading: "おっおっ", - }, - { - type: "顔文字", - word: "(๑•̀ㅂ•́)و✧", - reading: "きらっ", - }, - { - type: "顔文字", - word: "Σ┌( ┐*_ロ_)┐", - reading: "ぐきっ", - }, - { - type: "顔文字", - word: "( ‘ᾥ’ )", - reading: "くだち", - }, - { - type: "顔文字", - word: "(゜ロ゜)", - reading: "くわっ", - }, - { - type: "顔文字", - word: "('ω')三( ε: )三(.ω.)三( :3 )三('ω')ゴロゴロ", - reading: "ごろごろ", - }, - { - type: "顔文字", - word: "_(:зゝ∠)_", - reading: "しゃかにゅうめつじのぽーず", - }, - { - type: "顔文字", - word: "(^ω^ 三 ^ω^)", - reading: "しゅしゅしゅ", - }, - { - type: "顔文字", - word: "=͟͟͞͞( 'ω' =͟͟͞͞( 'ω' =͟͟͞͞)=͟͟͞=͟͟͞͞( 'ω' =͟͟͞͞( 'ω' =͟͟͞͞)", - reading: "しゅしゅしゅ", - }, - { - type: "顔文字", - word: "(^q^)", - reading: "じゅるり", - }, - { - type: "顔文字", - word: "( ´・ω・)(´・ω・)(・ω・`)(・ω・` )【審議中】", - reading: "しんぎちゅう", - }, - { - type: "顔文字", - word: "( ˘ω˘)", - reading: "すやぁ", - }, - { - type: "顔文字", - word: "0(:3 )〜 _(:3」 ∠ )_", - reading: "ちーん", - }, - { - type: "顔文字", - word: "ヾ(⌒(ノ'ω')ノ", - reading: "とうっ", - }, - { - type: "顔文字", - word: "(╹◡╹)", - reading: "にこー", - }, - { - type: "顔文字", - word: "( ・`ω・´)", - reading: "にゅいっ", - }, - { - type: "顔文字", - word: "٩( 'ω' )۶", - reading: "ぬんっ", - }, - { - type: "顔文字", - word: "(´・ωゞ)", - reading: "ねむねむ", - }, - { - type: "顔文字", - word: "('ω' )", - reading: "のーん", - }, - { - type: "顔文字", - word: "(_・ω・)_ バァン", - reading: "ばぁん", - }, - { - type: "顔文字", - word: "ヾ(:3ノシヾ)ノシ", - reading: "ばたばた", - }, - { - type: "顔文字", - word: "ヾ(⌒(ノシ'ω')ノシ", - reading: "ばたばた", - }, - { - type: "顔文字", - word: "((ง'ω')و三 ง'ω')ڡ≡)`Д゚);、;'.・”", - reading: "びしゅっ", - }, - { - type: "顔文字", - word: "・:*三(    ε:)", - reading: "びゅーん", - }, - { - type: "顔文字", - word: "‹‹\\(´ω` )/››‹‹\\( ´)/››‹‹\\( ´ω`)/››", - reading: "くるくる", - }, - { - type: "顔文字", - word: "\\\\\\ꐕ ꐕ ꐕ////", - reading: "わー", - }, -]; - -const dictionaryByType: CombineDictionaries = { - KAOMOJI, - DEVELOPMENT, -}; - -const dictionaryAll = combineDictionary(dictionaryByType); - -const BASE_PATH = "./dist"; -const options: BuildDictionaryFileOptions = { - basePath: BASE_PATH, - imeTxtPathList: { - googleime: `${BASE_PATH}/googleime.txt`, - kotoeri: `${BASE_PATH}/kotoeri.txt`, - msime: `${BASE_PATH}/msime.txt`, - }, - dictionaries: dictionaryAll, - combineDictionaries: dictionaryByType, -}; - -try { - buildDictionaryFile(options); -} catch (error: unknown) { - console.error(error); -} diff --git a/mod.ts b/mod.ts deleted file mode 100644 index 8c4edf0..0000000 --- a/mod.ts +++ /dev/null @@ -1,84 +0,0 @@ -import { ensureDir } from "./deps.ts"; -import { convertJsonToCsv, convertJsonToTsv } from "./core/convert.ts"; -import { - detectEncoding, - generateDictionaryFile, - generateDictionaryFileByType, -} from "./core/build.ts"; -import { - BuildDictionaryFileOptions, - CombineDictionaries, - Dictionaries, - IME_TYPE, -} from "./model.ts"; - -/** 単語のまとまりを分解して一つの配列を形成 */ -export const combineDictionary = ( - combineDictionaries: CombineDictionaries, -): Dictionaries => { - const dictionaries: Dictionaries = []; - for (const dictionary in combineDictionaries) { - dictionaries.push(...combineDictionaries[dictionary]); - } - return dictionaries; -}; - -/** 各IMEに合わせたユーザー辞書ファイルの作成 */ -export const buildDictionaryFile = async ( - options: BuildDictionaryFileOptions, -): Promise => { - const { basePath, imeTxtPathList, dictionaries, combineDictionaries } = - options; - - if (!dictionaries) return; - - await ensureDir(basePath); - if (imeTxtPathList.kotoeri) { - const { encode, bom } = detectEncoding(IME_TYPE.Apple); - await generateDictionaryFile( - convertJsonToCsv(dictionaries, IME_TYPE.Apple), - imeTxtPathList.kotoeri, - encode, - bom, - ); - } - if (imeTxtPathList.googleime) { - const { encode, bom } = detectEncoding(IME_TYPE.Google); - await generateDictionaryFile( - convertJsonToTsv(dictionaries, IME_TYPE.Google, { after: "\n" }), - imeTxtPathList.googleime, - encode, - bom, - ); - } - if (imeTxtPathList.msime) { - const { encode, bom } = detectEncoding(IME_TYPE.Microsoft); - await generateDictionaryFile( - convertJsonToTsv(dictionaries, IME_TYPE.Microsoft, { after: "\n" }), - imeTxtPathList.msime, - encode, - bom, - ); - } - if (combineDictionaries) { - await generateDictionaryFileByType( - basePath, - convertJsonToTsv, - combineDictionaries, - IME_TYPE.Google, - { after: "\n" }, - ); - await generateDictionaryFileByType( - basePath, - convertJsonToCsv, - combineDictionaries, - IME_TYPE.Apple, - ); - await generateDictionaryFileByType( - basePath, - convertJsonToTsv, - combineDictionaries, - IME_TYPE.Microsoft, - ); - } -}; diff --git a/test/core.test.ts b/test/core.test.ts index e5c2c85..c294070 100644 --- a/test/core.test.ts +++ b/test/core.test.ts @@ -1,6 +1,7 @@ +import { assertEquals } from "../deps.ts"; import { convertJsonToTextData, parsedCsvToJson } from "../core/convert.ts"; import { isValidFileExtention, isValidJson } from "../core/validation.ts"; -import { assertEquals } from "../deps.ts"; +import { DataPropertyError, FileTypeError } from "../core/error.ts"; Deno.test("ファイル形式がCSVかJSONかを確認する", async (t) => { await t.step("CSV", () => { @@ -18,7 +19,7 @@ Deno.test("ファイル形式がCSVかJSONかを確認する", async (t) => { await t.step("それ以外", () => { assertEquals(isValidFileExtention("test/mock.test.yml"), { success: false, - error: new Error("File Type Error"), + error: new FileTypeError("test/mock.test.yml"), }); }); }); @@ -115,7 +116,7 @@ Deno.test("JSONプロパティが想定されたものか検査する", async (t }, ); }); - await t.step("想定内", () => { + await t.step("想定外", () => { assertEquals( isValidJson([ { @@ -140,7 +141,7 @@ Deno.test("JSONプロパティが想定されたものか検査する", async (t ]), { success: false, - error: new Error(), + error: new DataPropertyError(), }, ); }); diff --git a/test/mock/private.csv b/test/mock/private.csv new file mode 100644 index 0000000..d4879dd --- /dev/null +++ b/test/mock/private.csv @@ -0,0 +1,4 @@ +type,word,reading,isSuppress,isSuggest,description +人名,遊馬賀樋香,あそまかといか,NO,NO,なんとなく思いついた名前 +姓,遊馬賀,あそまか,NO,NO,なんとなく思いついた名前 +名,樋香,といか,NO,NO,なんとなく思いついた名前 \ No newline at end of file diff --git a/test/mock/public.json b/test/mock/public.json new file mode 100644 index 0000000..d173f94 --- /dev/null +++ b/test/mock/public.json @@ -0,0 +1,26 @@ +[ + { + "type": "人名", + "word": "遊馬賀樋香", + "reading": "あそまかといか", + "isSuppress": "NO", + "isSuggest": "NO", + "description": "なんとなく思いついた名前" + }, + { + "type": "姓", + "word": "遊馬賀", + "reading": "あそまか", + "isSuppress": "NO", + "isSuggest": "NO", + "description": "なんとなく思いついた名前" + }, + { + "type": "名", + "word": "樋香", + "reading": "といか", + "isSuppress": "NO", + "isSuggest": "NO", + "description": "なんとなく思いついた名前" + } +]