From 919a97ca63bf8314ebbb79538cac4a24d2d7387b Mon Sep 17 00:00:00 2001 From: MarvNC Date: Thu, 11 Jul 2024 16:41:06 -0700 Subject: [PATCH] Use bun args --- readme.md | 2 +- src/index.ts | 41 +++++++++++++++++++++++++++++++---------- src/util/getVersion.ts | 18 +++++++++++++----- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/readme.md b/readme.md index 65e32f0..5c8c440 100644 --- a/readme.md +++ b/readme.md @@ -69,7 +69,7 @@ To download the abstracts for a language, run: To build a dictionary, run: ```sh -bun run start ja 2022-12-01 +bun run start -l ja -d 2022-12-01 ``` where `ja` is the language code and `2022.12.01` is the date of the dump (there diff --git a/src/index.ts b/src/index.ts index ac71f01..58358bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { file } from 'bun'; import { Dictionary, TermEntry } from 'yomichan-dict-builder'; +import { parseArgs } from 'util'; import { parseLine } from './parse/parseLine'; import { languages } from './constants'; @@ -17,7 +18,7 @@ const shortAbstractFile = (lang: string) => `short-abstracts_lang=${lang.toLowerCase()}.ttl`; (async () => { - const version = getVersion(); + const version = await getVersion(); console.log(`Using version ${version}`); @@ -60,7 +61,7 @@ const shortAbstractFile = (lang: string) => await dict.setIndex({ title: `${lang} Wikipedia [${date}] (v${version})`, - revision: `wikipedia_${new Date().toISOString()}`, + revision: `wikipedia_${version}`, format: 3, url: 'https://github.com/MarvNC/wikipedia-yomitan', description: `Wikipedia short abstracts from the DBPedia dataset available at https://databus.dbpedia.org/dbpedia/text/short-abstracts. @@ -162,23 +163,43 @@ function processLine(line: string, dict: Dictionary, lang: string) { } function readArgs() { - // Read arguments: node convertWikipedia.js [language] [date of dump] - const langInput = process.argv[2].toLowerCase() as keyof typeof languages; + const { values } = parseArgs({ + options: { + lang: { + type: 'string', + short: 'l', + }, + date: { + type: 'string', + short: 'd', + }, + }, + strict: true, + allowPositionals: false, + }); + + const langInput = ( + values.lang as string + )?.toLowerCase() as keyof typeof languages; + const dateInput = values.date as string; + // Assert language is valid - if (!languages[langInput]) { + if (!langInput || !languages[langInput]) { throw new Error( - `Language ${langInput} is not allowed. Allowed languages: ${Object.keys( + `Language ${langInput} is not allowed or not provided. Allowed languages: ${Object.keys( languages ).join(', ')}` ); } - const lang = languages[langInput]; + const lang = languages[langInput] as keyof typeof languages; - const dateInput = process.argv[3]; // Assert date is valid in format YYYY-MM-DD - if (!/^\d{4}-\d{2}-\d{2}$/.test(dateInput)) { - throw new Error(`Date ${dateInput} is not valid. Format: YYYY-MM-DD`); + if (!dateInput || !/^\d{4}-\d{2}-\d{2}$/.test(dateInput)) { + throw new Error( + `Date ${dateInput} is not valid or not provided. Format: YYYY-MM-DD` + ); } + return { lang, date: dateInput }; } diff --git a/src/util/getVersion.ts b/src/util/getVersion.ts index ed44417..e7c8d94 100644 --- a/src/util/getVersion.ts +++ b/src/util/getVersion.ts @@ -1,10 +1,18 @@ import { file } from 'bun'; +import { join } from 'path'; export async function getVersion(): Promise { - const packageJsonPath = `${import.meta.dir}/../package.json`; - const packageJson = await file(packageJsonPath).json(); - if (!packageJson.version) { - throw new Error('Could not get version from package.json'); + const packageJsonPath = join(process.cwd(), 'package.json'); + try { + const packageJson = await file(packageJsonPath).json(); + if (!packageJson.version) { + throw new Error('Version not found in package.json'); + } + return packageJson.version; + } catch (error) { + if (error instanceof Error) { + throw new Error(`Failed to read package.json: ${error.message}`); + } + throw error; } - return packageJson.version; }