Skip to content

Commit

Permalink
Modularize more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvNC committed Feb 11, 2024
1 parent 131c675 commit 64e1768
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ export {
COMPRESSED_IMAGES_FOLDER,
IMAGE_RESIZE_WIDTH,
};
export const dataFolder = './csvs';
export const exportDirectory = './dist';
31 changes: 9 additions & 22 deletions src/convertToTermDictionary.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import { Dictionary, DictionaryIndex } from 'yomichan-dict-builder';
import path from 'path';
import fs from 'fs';

import { getCSVInfo } from './util/csv/csvHandler.js';
import { parseCSVEntries } from './util/csv/parseCsvEntriesToJson.js';
import { convertEntryToYomitanTerms } from './util/yomitan/convertEntryToYomitanTerms.js';
import { findLabelValues } from './util/entryParse/parseLabels.js';
import { addYomitanTags } from './util/addYomitanTags.js';
import { getAllImageURLs } from './util/entryParse/findImages.js';
import { downloadImages } from './util/imageHandler/downloadImages.js';
import { addYomitanImages } from './util/addYomitanImages.js';
import { IMAGE_FOLDER, COMPRESSED_IMAGES_FOLDER, IMAGE_RESIZE_WIDTH } from './constants.js';
import {
IMAGE_FOLDER,
COMPRESSED_IMAGES_FOLDER,
IMAGE_RESIZE_WIDTH,
} from './constants.js';
import { compressImages } from './util/imageHandler/compressImages.js';

const dataFolder = './csvs';
const exportDirectory = './dist';
import { dataFolder, exportDirectory } from './constants.js';
import { getVersion } from './util/getVersion.js';
import { readAndParseCSVs } from './util/readAndParseCSVs.js';

(async () => {
const { allCsv, dateString } = await getCSVInfo(dataFolder);
const allCsvPath = path.join(dataFolder, allCsv);
const dictionaryEntries = await parseCSVEntries(allCsvPath);
console.log(`Found ${dictionaryEntries.length} entries.`);
const { dictionaryEntries, dateString } = await readAndParseCSVs(dataFolder);

const uniqueLabels = findLabelValues(dictionaryEntries);

Expand Down Expand Up @@ -72,13 +69,3 @@ const exportDirectory = './dist';
await dictionary.export(exportDirectory);
console.log(`Exported dictionary to ${exportDirectory}.`);
})();

/**
* Get the version from the package.json file.
* @returns {string} The version.
*/
function getVersion() {
const packageJsonPath = path.join(process.cwd(), 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
return packageJson.version;
}
12 changes: 12 additions & 0 deletions src/util/getVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'path';
import fs from 'fs';

/**
* Get the version from the package.json file.
* @returns {string} The version.
*/
export function getVersion() {
const packageJsonPath = path.join(process.cwd(), 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
return packageJson.version;
}
16 changes: 16 additions & 0 deletions src/util/readAndParseCSVs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'path';
import { getCSVInfo } from './csv/csvHandler.js';
import { parseCSVEntries } from './csv/parseCsvEntriesToJson.js';

/**
* @param {string} dataFolder
*/
export async function readAndParseCSVs(dataFolder) {
const { allCsv, dateString } = await getCSVInfo(dataFolder);
const dictionaryEntries = await parseCSVEntries(
path.join(dataFolder, allCsv)
);
console.log(`Found ${dictionaryEntries.length} entries.`);

return { dictionaryEntries, dateString };
}

0 comments on commit 64e1768

Please sign in to comment.