From f861e6a269ba9f62700776d3cd13b644a9e836d4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 20 Sep 2023 16:38:54 +0200 Subject: refactor: use named export for everything except pages Next expect a default export for pages so only those components should use default exports. Everything else should use named exports to reduce the number of import statements. --- src/utils/helpers/author.ts | 3 +- src/utils/helpers/i18n.ts | 49 ---------------- src/utils/helpers/images.ts | 3 +- src/utils/helpers/index.ts | 7 +++ src/utils/helpers/pages.ts | 12 ++-- src/utils/helpers/projects.ts | 102 ---------------------------------- src/utils/helpers/rss.ts | 4 +- src/utils/helpers/schema-org.ts | 2 +- src/utils/helpers/server/i18n.ts | 31 +++++++++++ src/utils/helpers/server/index.ts | 2 + src/utils/helpers/server/projects.ts | 105 +++++++++++++++++++++++++++++++++++ 11 files changed, 156 insertions(+), 164 deletions(-) delete mode 100644 src/utils/helpers/i18n.ts create mode 100644 src/utils/helpers/index.ts delete mode 100644 src/utils/helpers/projects.ts create mode 100644 src/utils/helpers/server/i18n.ts create mode 100644 src/utils/helpers/server/index.ts create mode 100644 src/utils/helpers/server/projects.ts (limited to 'src/utils/helpers') diff --git a/src/utils/helpers/author.ts b/src/utils/helpers/author.ts index 2892524..a5e9bc6 100644 --- a/src/utils/helpers/author.ts +++ b/src/utils/helpers/author.ts @@ -1,5 +1,4 @@ -import { type Author, type ContentKind } from '../../types/app'; -import { type RawAuthor } from '../../types/raw-data'; +import { type Author, type ContentKind, type RawAuthor } from '../../types'; /** * Convert author raw data to regular data. diff --git a/src/utils/helpers/i18n.ts b/src/utils/helpers/i18n.ts deleted file mode 100644 index 35d495e..0000000 --- a/src/utils/helpers/i18n.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { createIntl, createIntlCache, IntlShape } from '@formatjs/intl'; -import { readFile } from 'fs/promises'; -import path from 'path'; -import { settings } from '../config'; - -export type Messages = { [key: string]: string }; - -export const defaultLocale = settings.locales.defaultLocale; - -/** - * Load the translation for the provided locale. - * - * @param currentLocale - The current locale. - * @returns {Promise} The translated strings. - */ -export async function loadTranslation( - currentLocale: string | undefined -): Promise { - const locale: string = currentLocale || defaultLocale; - - const languagePath = path.join(process.cwd(), `lang/${locale}.json`); - - try { - const contents = await readFile(languagePath, 'utf8'); - return JSON.parse(contents); - } catch (error) { - console.error( - 'Error: Could not load compiled language files. Please run `yarn run i18n:compile` first."' - ); - throw error; - } -} - -/** - * Create an Intl object to be used outside components. - * - * @returns {>} The Intl object. - */ -export async function getIntlInstance(): Promise> { - try { - const cache = createIntlCache(); - const messages = await loadTranslation(defaultLocale); - - return createIntl({ locale: defaultLocale, messages }, cache); - } catch (error) { - console.error('Error: Could not create an Intl instance.'); - throw error; - } -} diff --git a/src/utils/helpers/images.ts b/src/utils/helpers/images.ts index bff50c3..6e0c2c5 100644 --- a/src/utils/helpers/images.ts +++ b/src/utils/helpers/images.ts @@ -1,5 +1,4 @@ -import { Image } from '../../types/app'; -import { RawCover } from '../../types/raw-data'; +import { type Image, type RawCover } from '../../types'; /** * Retrieve an Image object from raw data. diff --git a/src/utils/helpers/index.ts b/src/utils/helpers/index.ts new file mode 100644 index 0000000..1a35583 --- /dev/null +++ b/src/utils/helpers/index.ts @@ -0,0 +1,7 @@ +export * from './author'; +export * from './dates'; +export * from './images'; +export * from './pages'; +export * from './rss'; +export * from './schema-org'; +export * from './strings'; diff --git a/src/utils/helpers/pages.ts b/src/utils/helpers/pages.ts index d6d44d2..6b27b6d 100644 --- a/src/utils/helpers/pages.ts +++ b/src/utils/helpers/pages.ts @@ -1,13 +1,13 @@ -import { type Post } from '../../components/organisms/layout/posts-list'; -import { type LinksListItems } from '../../components/organisms/widgets/links-list-widget'; -import { getArticleFromRawData } from '../../services/graphql/articles'; -import { type Article, type PageLink } from '../../types/app'; -import { EdgesResponse } from '../../types/graphql/queries'; +import { type LinksListItems, type Post } from '../../components'; +import { getArticleFromRawData } from '../../services/graphql'; import { + type Article, + type EdgesResponse, + type PageLink, type RawArticle, type RawThematicPreview, type RawTopicPreview, -} from '../../types/raw-data'; +} from '../../types'; import { getImageFromRawData } from './images'; /** diff --git a/src/utils/helpers/projects.ts b/src/utils/helpers/projects.ts deleted file mode 100644 index 0e4de7d..0000000 --- a/src/utils/helpers/projects.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { readdirSync } from 'node:fs'; -import path from 'node:path'; -import { ProjectCard, ProjectPreview } from '../../types/app'; -import { MDXProjectMeta } from '../../types/mdx'; - -/** - * Retrieve all the projects filename. - * - * @returns {string[]} An array of filenames. - */ -export const getProjectFilenames = (): string[] => { - const projectsDirectory = path.join(process.cwd(), 'src/content/projects'); - const filenames = readdirSync(projectsDirectory); - - return filenames.map((filename) => filename.replace(/\.mdx$/, '')); -}; - -/** - * Retrieve the data of a project by filename. - * - * @param {string} filename - The project filename. - * @returns {Promise} - */ -export const getProjectData = async ( - filename: string -): Promise => { - try { - const { - meta, - }: { - meta: MDXProjectMeta; - } = await import(`../../content/projects/${filename}.mdx`); - - const { dates, intro, title, ...projectMeta } = meta; - const { publication, update } = dates; - const cover = await import(`../../../public/projects/${filename}.jpg`); - - return { - id: filename, - intro, - meta: { - ...projectMeta, - dates: { publication, update }, - // Dynamic import source does not work so I use it only to get sizes - cover: { - ...cover.default, - alt: `${title} image`, - src: `/projects/${filename}.jpg`, - title, - }, - }, - slug: filename, - title: title, - }; - } catch (err) { - console.error(err); - throw err; - } -}; - -/** - * Retrieve all the projects data using filenames. - * - * @param {string[]} filenames - The filenames without extension. - * @returns {Promise} - An array of projects data. - */ -export const getProjectsData = async ( - filenames: string[] -): Promise => { - return Promise.all( - filenames.map(async (filename) => { - const { id, meta, slug, title } = await getProjectData(filename); - const { cover, dates, tagline, technologies } = meta; - return { id, meta: { cover, dates, tagline, technologies }, slug, title }; - }) - ); -}; - -/** - * Method to sort an array of projects by publication date. - * - * @param {ProjectCard} a - A single project. - * @param {ProjectCard} b - A single project. - * @returns The result used by Array.sort() method: 1 || -1 || 0. - */ -const sortProjectsByPublicationDate = (a: ProjectCard, b: ProjectCard) => { - if (a.meta.dates.publication < b.meta.dates.publication) return 1; - if (a.meta.dates.publication > b.meta.dates.publication) return -1; - return 0; -}; - -/** - * Retrieve all projects in content folder sorted by publication date. - * - * @returns {Promise} An array of projects. - */ -export const getProjectsCard = async (): Promise => { - const filenames = getProjectFilenames(); - const projects = await getProjectsData(filenames); - - return [...projects].sort(sortProjectsByPublicationDate); -}; diff --git a/src/utils/helpers/rss.ts b/src/utils/helpers/rss.ts index 41a036c..28f3c7b 100644 --- a/src/utils/helpers/rss.ts +++ b/src/utils/helpers/rss.ts @@ -3,8 +3,8 @@ import { getArticleFromRawData, getArticles, getTotalArticles, -} from '../../services/graphql/articles'; -import { Article } from '../../types/app'; +} from '../../services/graphql'; +import { type Article } from '../../types'; import { settings } from '../../utils/config'; /** diff --git a/src/utils/helpers/schema-org.ts b/src/utils/helpers/schema-org.ts index 5d400c8..82f99c2 100644 --- a/src/utils/helpers/schema-org.ts +++ b/src/utils/helpers/schema-org.ts @@ -7,7 +7,7 @@ import { Graph, WebPage, } from 'schema-dts'; -import { Dates } from '../../types/app'; +import { type Dates } from '../../types'; import { settings } from '../../utils/config'; export type GetBlogSchemaProps = { diff --git a/src/utils/helpers/server/i18n.ts b/src/utils/helpers/server/i18n.ts new file mode 100644 index 0000000..dbbc4e5 --- /dev/null +++ b/src/utils/helpers/server/i18n.ts @@ -0,0 +1,31 @@ +import { readFile } from 'fs/promises'; +import path from 'path'; +import { settings } from '../../config'; + +export type Messages = { [key: string]: string }; + +export const defaultLocale = settings.locales.defaultLocale; + +/** + * Load the translation for the provided locale. + * + * @param currentLocale - The current locale. + * @returns {Promise} The translated strings. + */ +export async function loadTranslation( + currentLocale: string | undefined +): Promise { + const locale: string = currentLocale ?? defaultLocale; + + const languagePath = path.join(process.cwd(), `lang/${locale}.json`); + + try { + const contents = await readFile(languagePath, 'utf8'); + return JSON.parse(contents); + } catch (error) { + console.error( + 'Error: Could not load compiled language files. Please run `yarn run i18n:compile` first."' + ); + throw error; + } +} diff --git a/src/utils/helpers/server/index.ts b/src/utils/helpers/server/index.ts new file mode 100644 index 0000000..56ebad5 --- /dev/null +++ b/src/utils/helpers/server/index.ts @@ -0,0 +1,2 @@ +export * from './i18n'; +export * from './projects'; diff --git a/src/utils/helpers/server/projects.ts b/src/utils/helpers/server/projects.ts new file mode 100644 index 0000000..ed73da8 --- /dev/null +++ b/src/utils/helpers/server/projects.ts @@ -0,0 +1,105 @@ +import { readdirSync } from 'fs'; +import path from 'path'; +import { + type MDXProjectMeta, + type ProjectCard, + type ProjectPreview, +} from '../../../types'; + +/** + * Retrieve all the projects filename. + * + * @returns {string[]} An array of filenames. + */ +export const getProjectFilenames = (): string[] => { + const projectsDirectory = path.join(process.cwd(), 'src/content/projects'); + const filenames = readdirSync(projectsDirectory); + + return filenames.map((filename) => filename.replace(/\.mdx$/, '')); +}; + +/** + * Retrieve the data of a project by filename. + * + * @param {string} filename - The project filename. + * @returns {Promise} + */ +export const getProjectData = async ( + filename: string +): Promise => { + try { + const { + meta, + }: { + meta: MDXProjectMeta; + } = await import(`../../../content/projects/${filename}.mdx`); + + const { dates, intro, title, ...projectMeta } = meta; + const { publication, update } = dates; + const cover = await import(`../../../../public/projects/${filename}.jpg`); + + return { + id: filename, + intro, + meta: { + ...projectMeta, + dates: { publication, update }, + // Dynamic import source does not work so I use it only to get sizes + cover: { + ...cover.default, + alt: `${title} image`, + src: `/projects/${filename}.jpg`, + title, + }, + }, + slug: filename, + title: title, + }; + } catch (err) { + console.error(err); + throw err; + } +}; + +/** + * Retrieve all the projects data using filenames. + * + * @param {string[]} filenames - The filenames without extension. + * @returns {Promise} - An array of projects data. + */ +export const getProjectsData = async ( + filenames: string[] +): Promise => { + return Promise.all( + filenames.map(async (filename) => { + const { id, meta, slug, title } = await getProjectData(filename); + const { cover, dates, tagline, technologies } = meta; + return { id, meta: { cover, dates, tagline, technologies }, slug, title }; + }) + ); +}; + +/** + * Method to sort an array of projects by publication date. + * + * @param {ProjectCard} a - A single project. + * @param {ProjectCard} b - A single project. + * @returns The result used by Array.sort() method: 1 || -1 || 0. + */ +const sortProjectsByPublicationDate = (a: ProjectCard, b: ProjectCard) => { + if (a.meta.dates.publication < b.meta.dates.publication) return 1; + if (a.meta.dates.publication > b.meta.dates.publication) return -1; + return 0; +}; + +/** + * Retrieve all projects in content folder sorted by publication date. + * + * @returns {Promise} An array of projects. + */ +export const getProjectsCard = async (): Promise => { + const filenames = getProjectFilenames(); + const projects = await getProjectsData(filenames); + + return [...projects].sort(sortProjectsByPublicationDate); +}; -- cgit v1.2.3