Skip to content

Commit

Permalink
refactor: Move some things around
Browse files Browse the repository at this point in the history
  • Loading branch information
fabschurt committed Apr 15, 2024
1 parent b74da75 commit f3d010a
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 210 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@fabschurt/microgen",
"author": "Fabien Schurter",
"license": "MIT",
"type": "module",
"main": "src/main.js",
"license": "MIT",
"main": "./src/main.js",
"imports": {
"#src/*": "./src/*.js",
"#tests/helpers": "./tests/_helpers.js"
"#tests/helpers": "./tests/.lib/helpers.js"
},
"bin": {
"microgen": "./bin/cli.js"
Expand Down
10 changes: 10 additions & 0 deletions src/adapter/pug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const TEMPLATE_EXT = '.pug'

export const renderTemplate = (
(renderPug, readFile) => (
(templateBasePath, data) => (
readFile(templateBasePath + TEMPLATE_EXT)
.then((template) => renderPug(template, data))
)
)
)
13 changes: 0 additions & 13 deletions src/adapter/renderTemplate/pug.js

This file was deleted.

52 changes: 25 additions & 27 deletions src/domain/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,36 @@ const INDEX_TEMPLATE_BASENAME = 'index'
const INDEX_OUTPUT_NAME = 'index.html'
const ASSETS_DIR_NAME = 'assets'

export function renderProjectIndex(
withSrcDir,
withBuildDir,
writeFile,
renderTemplate,
data,
) {
return withSrcDir((prefixWithSrcDir) => {
return withBuildDir(async (prefixWithBuildDir) => {
return writeFile(
prefixWithBuildDir(INDEX_OUTPUT_NAME),
await renderTemplate(
prefixWithSrcDir(INDEX_TEMPLATE_BASENAME),
data,
),
)
})
})
}
export const renderProjectIndex = (
(withSrcDir, withBuildDir, writeFile, renderTemplate) => (
(data) => (
withSrcDir((prefixWithSrcDir) => (
withBuildDir(async (prefixWithBuildDir) => (
writeFile(
prefixWithBuildDir(INDEX_OUTPUT_NAME),
await renderTemplate(
prefixWithSrcDir(INDEX_TEMPLATE_BASENAME),
data,
),
)
))
))
)
)
)

export function copyProjectAssetsDir(
export const copyProjectAssetsDir = (
withSrcDir,
withBuildDir,
copyDir,
ifPathExists,
) {
return withSrcDir((prefixWithSrcDir) => {
return withBuildDir((prefixWithBuildDir) => {
return ifPathExists(
) => (
withSrcDir((prefixWithSrcDir) => (
withBuildDir((prefixWithBuildDir) => (
ifPathExists(
prefixWithSrcDir(ASSETS_DIR_NAME),
(dirPath) => copyDir(dirPath, prefixWithBuildDir(ASSETS_DIR_NAME)),
)
})
})
}
))
))
)
37 changes: 18 additions & 19 deletions src/domain/data.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
const DATA_FILE_PATH = 'data.json'
const ENV_VAR_PLACEHOLDER_PATTERN = /^%(.+)%$/

function captureEnvVarName(value) {
const captureEnvVarName = (value) => {
const match = value.match?.(ENV_VAR_PLACEHOLDER_PATTERN)

return match ? match[1] : null
}

export function parseProjectData(parseJSONFile, withSrcDir) {
return withSrcDir((prefixWithSrcDir) => parseJSONFile(prefixWithSrcDir(DATA_FILE_PATH)))
}
export const parseProjectData = (parseJSONFile, withSrcDir) => (
withSrcDir((prefixWithSrcDir) => parseJSONFile(prefixWithSrcDir(DATA_FILE_PATH)))
)

export function mergeDataWithEnvVars(
deepCloneObject,
transformObjectValues,
data,
envVars,
) {
return transformObjectValues(
deepCloneObject(data),
(obj, key) => {
const envVarName = captureEnvVarName(obj[key])
export const mergeDataWithEnvVars = (
(deepCloneObject, transformObjectValues) => (
(data, envVars) => (
transformObjectValues(
deepCloneObject(data),
(obj, key) => {
const envVarName = captureEnvVarName(obj[key])

if (envVarName) {
obj[key] = envVars[envVarName] ?? null
}
}
if (envVarName) {
obj[key] = envVars[envVarName] ?? null
}
},
)
)
)
}
)
24 changes: 14 additions & 10 deletions src/domain/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ const LANG_PATTERN = /^[a-z]{2}$/
const TRANSLATION_DIR_PATH = 'i18n'
const TRANSLATION_FILE_EXT = '.json'

export function parseProjectTranslations(parseJSONFile, withSrcDir, lang) {
assert.match(lang, LANG_PATTERN, `'${lang}' is not a valid language code.`)
export const parseProjectTranslations = (
(parseJSONFile, withSrcDir) => (
(lang) => {
assert.match(lang, LANG_PATTERN, `'${lang}' is not a valid language code.`)

return withSrcDir((prefixWithSrcDir) => {
const translationFilePath = join(
prefixWithSrcDir(TRANSLATION_DIR_PATH),
lang + TRANSLATION_FILE_EXT,
)
return withSrcDir((prefixWithSrcDir) => {
const translationFilePath = join(
prefixWithSrcDir(TRANSLATION_DIR_PATH),
lang + TRANSLATION_FILE_EXT,
)

return parseJSONFile(translationFilePath)
})
}
return parseJSONFile(translationFilePath)
})
}
)
)
82 changes: 39 additions & 43 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,18 @@ import {
mergeObjectList,
accessObjectProp,
} from '#src/utils/object'
import renderTemplate from '#src/adapter/renderTemplate/pug'
import { renderTemplate } from '#src/adapter/pug'
import { format } from 'node:util'
import { render as renderPug } from 'pug'

function parseData(parseJSONFile, withSrcDir, lang = null, envVars = []) {
return Promise.all([
parseProjectData(parseJSONFile, withSrcDir)
.then((data) => mergeDataWithEnvVars(
deepCloneObject,
transformObjectValues,
data,
envVars,
))
,
lang
? (
parseProjectTranslations(parseJSONFile, withSrcDir, lang)
.then((dictionary) => ({
_: {
trans: (transKey, ...args) => (
format(
accessObjectProp(dictionary, transKey),
...args,
)
)
},
}))
) : {}
,
])
.then(cleanUpObjectList)
.then(mergeObjectList)
}

export default async function main(
export default (async (
srcDirPath,
buildDirPath,
lang = null,
envVars = {},
) {
) => {
const _parseJSONFile = parseJSONFile(ifPathExists, readFile)

await ifPathExists(buildDirPath, rmDir)

const [withSrcDir, withBuildDir] = await Promise.all([
Expand All @@ -66,21 +38,45 @@ export default async function main(
])

return (
parseData(
(filePath) => parseJSONFile(ifPathExists, readFile, filePath),
withSrcDir,
lang,
envVars,
)
Promise.all([
parseProjectData(_parseJSONFile, withSrcDir)
.then(
(data) => mergeDataWithEnvVars(
deepCloneObject,
transformObjectValues,
)(
data,
envVars,
)
)
,
lang
? (
parseProjectTranslations(_parseJSONFile, withSrcDir)(lang)
.then((dictionary) => ({
_: {
trans: (msgID, ...args) => (
format(
accessObjectProp(dictionary, msgID),
...args,
)
)
},
}))
)
: {}
,
])
.then(cleanUpObjectList)
.then(mergeObjectList)
.then((data) => (
Promise.all([
renderProjectIndex(
withSrcDir,
withBuildDir,
writeFile,
(templateBasePath, data) => renderTemplate(renderPug, readFile, templateBasePath, data),
data,
),
renderTemplate(renderPug, readFile),
)(data),
copyProjectAssetsDir(
withSrcDir,
withBuildDir,
Expand All @@ -90,4 +86,4 @@ export default async function main(
])
))
)
}
})
Loading

0 comments on commit f3d010a

Please sign in to comment.