Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: Fix some CS issues #33

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions src/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { join } from 'node:path'

const INDEX_TEMPLATE_NAME = 'index.pug'
const INDEX_OUTPUT_NAME = 'index.html'
const ASSET_DIR_NAME = 'assets'
Expand Down Expand Up @@ -44,31 +42,22 @@ export function renderIndex(
) {
return withSrcDir((prefixWithSrcDir) => {
return withBuildDir(async (prefixWithBuildDir) => {
const renderedTemplate = renderTemplate(
await readFile(prefixWithSrcDir(INDEX_TEMPLATE_NAME)),
data,
)

return writeFile(
prefixWithBuildDir(INDEX_OUTPUT_NAME),
renderedTemplate,
renderTemplate(
await readFile(prefixWithSrcDir(INDEX_TEMPLATE_NAME)),
data,
),
)
})
})
}

export function copyAssetDir(
withSrcDir,
withBuildDir,
copyDir,
ifPathExists,
) {
export function copyAssetDir(withSrcDir, withBuildDir, copyDir, ifPathExists) {
return withSrcDir((prefixWithSrcDir) => {
return withBuildDir((prefixWithBuildDir) => {
const assetDirPath = prefixWithSrcDir(ASSET_DIR_NAME)

return ifPathExists(
assetDirPath,
prefixWithSrcDir(ASSET_DIR_NAME),
(path) => copyDir(path, prefixWithBuildDir(ASSET_DIR_NAME)),
)
})
Expand Down
11 changes: 5 additions & 6 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ function normalizeEnvVarName(str) {
)
}

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

Expand All @@ -26,6 +21,10 @@ export function parseDataFromJsonFile(
})
}

/**
* Extracts env vars whose name starts with `ENV_VAR_PREFIX`, converts this name
* to snake case, and returns a normalized dictionary of these extracted vars.
*/
export function parseDataFromEnv(envObject) {
return (
Object.fromEntries(
Expand Down
10 changes: 2 additions & 8 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ const LANG_PATTERN = /^[a-z]{2}$/
const TRANSLATION_DIR_PATH = 'i18n'
const TRANSLATION_FILE_PREFIX = '.json'

export function parseTranslations(
withSrcDir,
ifPathExists,
readFile,
parseJson,
lang,
) {
export function parseTranslations(withSrcDir, ifPathExists, readFile, parseJson, lang) {
assert.match(lang, LANG_PATTERN)

return withSrcDir((prefixWithSrcDir) => {
Expand All @@ -23,7 +17,7 @@ export function parseTranslations(
return ifPathExists(
translationFilePath,
async () => parseJson(await readFile(translationFilePath)),
{}
{},
)
})
}
16 changes: 9 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ 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, withBuildDir] = await (
Promise.all([
withDir(srcDirPath),
withScratchDir(buildDirPath),
])
)
const [withSrcDir, withBuildDir] = await Promise.all([
withDir(srcDirPath),
withScratchDir(buildDirPath),
])

const data = await parseData(
withSrcDir,
Expand Down
9 changes: 2 additions & 7 deletions tests/data.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 } from '#tests/helpers'
import { mkdir, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { withTempDir } from '#tests/helpers'
import { withDir, ifPathExists, readFile } from '#src/utils/fs'
import { parseJson } from '#src/utils/json'
import { parseDataFromJsonFile, parseDataFromEnv } from '#src/data'
Expand All @@ -18,12 +18,7 @@ describe('#src/data', () => {
await writeFile(jsonFilePath, '{"stuff": ["thing", "trinket", "gizmo"]}')

assert.deepStrictEqual(
await parseDataFromJsonFile(
withDir(dirPath),
ifPathExists,
readFile,
parseJson,
),
await parseDataFromJsonFile(withDir(dirPath), ifPathExists, readFile, parseJson),
{
stuff: [
'thing',
Expand Down
2 changes: 1 addition & 1 deletion tests/i18n.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, it } from 'node:test'
import assert from 'node:assert'
import { withTempDir } from '#tests/helpers'
import { join } from 'node:path'
import { mkdir, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { withTempDir } from '#tests/helpers'
import { withDir, ifPathExists, readFile } from '#src/utils/fs'
import { parseJson } from '#src/utils/json'
import { parseTranslations } from '#src/i18n'
Expand Down