Skip to content

Commit

Permalink
Use bun args
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Jul 11, 2024
1 parent 9a8ede8 commit 919a97c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 31 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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}`);

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 };
}
18 changes: 13 additions & 5 deletions src/util/getVersion.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { file } from 'bun';
import { join } from 'path';

export async function getVersion(): Promise<string> {
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;
}

0 comments on commit 919a97c

Please sign in to comment.