Skip to content

Commit

Permalink
refactor: Fix some quirks in async flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Schurter committed Jun 27, 2023
1 parent 75de935 commit 58d3b8f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ function normalizeEnvVarName(str) {
)
}

export async function parseDataFromJsonFile(
export function parseDataFromJsonFile(
withSrcDir,
ifPathExists,
readFile,
parseJson,
) {
return await withSrcDir(async (prefixWithSrcDir) => {
return withSrcDir((prefixWithSrcDir) => {
const jsonFilePath = prefixWithSrcDir(JSON_FILE_NAME)

return await ifPathExists(
return ifPathExists(
jsonFilePath,
async () => parseJson(await readFile(jsonFilePath)),
{},
Expand Down
8 changes: 4 additions & 4 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const LANG_PATTERN = /^[a-z]{2}$/
const TRANSLATION_DIR_PATH = 'i18n'
const TRANSLATION_FILE_PREFIX = '.json'

export async function parseTranslations(
export function parseTranslations(
withSrcDir,
ifPathExists,
readFile,
Expand All @@ -14,16 +14,16 @@ export async function parseTranslations(
) {
assert.match(lang, LANG_PATTERN)

return await withSrcDir(async (prefixWithSrcDir) => {
return withSrcDir((prefixWithSrcDir) => {
const translationFilePath = join(
prefixWithSrcDir(TRANSLATION_DIR_PATH),
lang + TRANSLATION_FILE_PREFIX,
)

return await ifPathExists(
return ifPathExists(
translationFilePath,
async () => parseJson(await readFile(translationFilePath)),
{}
{},
)
})
}
39 changes: 28 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,37 @@ import { parseTranslations } from '#src/i18n'
import { parseJson } from '#src/utils/json'
import renderTemplate from '#src/renderTemplate/pug'

export default async function buildProject(srcDirPath, buildDirPath, lang = null) {
export default async function buildProject(
srcDirPath,
buildDirPath,
lang = null,
) {
await ifPathExists(buildDirPath, rmDir)

const withSrcDir = await withDir(srcDirPath)
const withBuildDir = await withScratchDir(buildDirPath)
const [withSrcDir, withBuildDir] = await (
Promise.all([
withDir(srcDirPath),
withScratchDir(buildDirPath),
])
)

const data = {
...(await parseDataFromJsonFile(withSrcDir, ifPathExists, readFile, parseJson)),
...parseDataFromEnv(process.env),
}

if (lang) {
data.t = await parseTranslations(withSrcDir, ifPathExists, readFile, parseJson, lang)
}
// This will fetch data from 3 different sources simultaneously, filter out
// empty results, and merge everything into a final single object (this is not
// overly readable, but it allows for «parallel» asynchronous fetching of
// data):
const data = await (
Promise.all([
parseDataFromJsonFile(withSrcDir, ifPathExists, readFile, parseJson),
parseDataFromEnv(process.env),
lang
? parseTranslations(withSrcDir, ifPathExists, readFile, parseJson, lang)
.then((dictionary) => ({ i18n: dictionary }))
: {}
,
])
.then((objects) => objects.filter((obj) => Object.keys(obj).length))
.then((objects) => Object.assign({}, ...objects))
)

return Promise.all([
renderIndex(withSrcDir, withBuildDir, readFile, writeFile, renderTemplate, data),
Expand Down
13 changes: 4 additions & 9 deletions tests/i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@ describe('#src/i18n', () => {
})
})

it('throws if an invalid `lang` parameter is passed', async () => {
await assert.rejects(
parseTranslations(
withDir(tmpdir()),
ifPathExists,
readFile,
parseJson,
'fAiL',
)
it('throws if an invalid `lang` parameter is passed', () => {
assert.throws(
() => parseTranslations(withDir(tmpdir()), ifPathExists, readFile, parseJson, 'fAiL'),
assert.AssertionError
)
})
})
Expand Down
17 changes: 4 additions & 13 deletions tests/utils/fs.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it } from 'node:test'
import assert from 'node:assert'
import { withTempDir, generateNonExistentPath, noop } from '#tests/helpers'
import * as fs from 'node:fs/promises'
import { join } from 'node:path'
import { withTempDir, generateNonExistentPath, noop } from '#tests/helpers'
import {
withDir,
withScratchDir,
Expand All @@ -22,10 +22,7 @@ describe('#src/utils/fs', () => {

await fs.mkdir(dirPath)
await withDir(dirPath)(async (prefixWithDir) => {
await fs.writeFile(
prefixWithDir(fileName),
'Hello fren!',
)
await fs.writeFile(prefixWithDir(fileName), 'Hello fren!')
})

await assert.doesNotReject(fs.access(join(dirPath, fileName)))
Expand All @@ -44,10 +41,7 @@ describe('#src/utils/fs', () => {
const fileName = 'test.txt'

await withScratchDir(dirPath)(async (prefixWithDir) => {
await fs.writeFile(
prefixWithDir(fileName),
'All your base are belong to us.',
)
await fs.writeFile(prefixWithDir(fileName), 'All your base are belong to us.')
})

await assert.doesNotReject(fs.access(dirPath))
Expand All @@ -62,10 +56,7 @@ describe('#src/utils/fs', () => {

await fs.mkdir(dirPath)
await withScratchDir(dirPath)(async (prefixWithDir) => {
await fs.writeFile(
prefixWithDir(fileName),
'print(\'Wow, such fren, very noyce.\')',
)
await fs.writeFile(prefixWithDir(fileName), 'print(\'Wow, such fren, very noyce.\')')
})

await assert.doesNotReject(fs.access(join(dirPath, fileName)))
Expand Down

0 comments on commit 58d3b8f

Please sign in to comment.