-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split code to separate files (stage 1) (#2)
* refactor: split code to separate files (stage 1) * 1.0.1
- Loading branch information
Showing
9 changed files
with
166 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { readFile, readdir } from "fs/promises"; | ||
|
||
import { DEFAULTS, SRC_CODE_PATTERN } from "./constants"; | ||
import { Config, SatusCode, TemplateVariables } from "./interfaces"; | ||
import { renderPage, renderSnippet } from "./render"; | ||
import { readJson } from "./fs"; | ||
|
||
export async function compile(config: Config) { | ||
const pkg = await readJson(DEFAULTS.PACKAGE); | ||
let vars: TemplateVariables = { | ||
locale: config.locale, | ||
version: pkg.version, | ||
}; | ||
|
||
const template = await readFile(`${DEFAULTS.THEMES}/${config.theme}/template.html`).then(String); | ||
|
||
const codesVars = new Map<SatusCode, TemplateVariables>(); | ||
await readdir(`${DEFAULTS.SRC}/${config.locale}/`).then((files) => { | ||
return Promise.all( | ||
files.map(async (file) => { | ||
const srcVars: TemplateVariables = await readJson(`${DEFAULTS.SRC}/${config.locale}/${file}`); | ||
const match = file.match(SRC_CODE_PATTERN); | ||
if (match) { | ||
codesVars.set(Number(match[0]), srcVars); | ||
} else { | ||
vars = { ...vars, ...srcVars }; | ||
} | ||
}) | ||
); | ||
}); | ||
|
||
if (codesVars.size > 0) { | ||
await Promise.all(Array.from(codesVars).map(([code, codeVars]) => renderPage(template, { ...vars, ...codeVars, code }))).then(() => { | ||
console.log(`INFO: ${codesVars.size} pages were successfully created`); | ||
}); | ||
|
||
await readdir(`${DEFAULTS.SNIPPETS}/`).then((files) => { | ||
return Promise.all( | ||
files.map(async (file) => { | ||
const snippet = await readFile(`${DEFAULTS.SNIPPETS}/${file}`).then(String); | ||
return renderSnippet(file, snippet, { codes: Array.from(codesVars.keys()) }); | ||
}) | ||
); | ||
}); | ||
} else { | ||
throw new Error("No source data to render error pages"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { access, cp, readFile, rm, mkdir } from "fs/promises"; | ||
|
||
import { Config } from "./interfaces"; | ||
import { sourceStyleFilter } from "./style"; | ||
|
||
export async function readJson(path: string) { | ||
return await readFile(path).then(String).then(JSON.parse); | ||
} | ||
|
||
export async function existenceCheck(path: string) { | ||
let exists = false; | ||
try { | ||
await access(path); | ||
exists = true; | ||
} catch (_) {} | ||
} | ||
|
||
export async function readConfig(path: string): Promise<Config> { | ||
const config = await readJson(path); | ||
|
||
if (!config.theme) { | ||
throw new Error(`Please set theme in your configuration: ${path}`); | ||
} else if (!config.locale) { | ||
throw new Error(`Please set locale in your configuration: ${path}`); | ||
} | ||
|
||
return config; | ||
} | ||
|
||
export async function flush(path: string) { | ||
console.log(`INFO: prepare build directory '${path}'`); | ||
|
||
await rm(path, { force: true, recursive: true }); | ||
await mkdir(path, { recursive: true }); | ||
} | ||
|
||
export async function copyAssets(src: string, dst: string) { | ||
console.log(`INFO: copying assets to build directory`); | ||
|
||
if (existenceCheck(src)) { | ||
await cp(src, dst, { | ||
recursive: true, | ||
filter: sourceStyleFilter, | ||
}); | ||
} else { | ||
console.log(`INFO: no assets in current theme directory`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,4 @@ export interface TemplateVariables { | |
|
||
export interface SnippetVariables { | ||
codes: SatusCode[]; | ||
locale: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { writeFile } from "fs/promises"; | ||
import { render } from "mustache"; | ||
|
||
import { DEFAULTS } from "./constants"; | ||
import { SnippetVariables, TemplateVariables } from "./interfaces"; | ||
|
||
export async function renderPage(template: string, vars: TemplateVariables) { | ||
if (!vars.code) { | ||
throw new Error("No code variable to render page"); | ||
} else if (!vars.locale) { | ||
throw new Error("No locale variable to render page"); | ||
} | ||
|
||
const path = `${DEFAULTS.DIST}/${vars.code}.html`; | ||
|
||
console.log(`INFO: render '${path}' page`); | ||
await writeFile(path, render(template, vars), { flag: "w+" }); | ||
} | ||
|
||
export async function renderSnippet(name: string, template: string, vars: SnippetVariables) { | ||
if (!vars.codes) { | ||
throw new Error("No codes list to render config snippet"); | ||
} | ||
|
||
const path = `${DEFAULTS.DIST}/${name}`; | ||
|
||
console.log(`INFO: render '${path}' config snippet`); | ||
await writeFile(path, render(template, vars), { flag: "w+" }); | ||
console.log(`INFO: config snippet '${name}' was successfully created`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { promisify } from "util"; | ||
import { exec } from "child_process"; | ||
|
||
import { DEFAULTS } from "./constants"; | ||
import { Config } from "./interfaces"; | ||
|
||
export const TAILWIND_STYLE = /\.tcss$/i; | ||
|
||
export async function buildTailwind(config: Config) { | ||
if (config.tailwind) { | ||
const input = `${DEFAULTS.THEMES}/${config.theme}/${DEFAULTS.ASSETS}/css/${DEFAULTS.TAILWIND_ENTRY}`; | ||
const output = `${DEFAULTS.DIST}/${DEFAULTS.ASSETS}/css/${DEFAULTS.TAILWIND_ENTRY.replace(".tcss", ".css")}`; | ||
const cmd = `INPUT="${input}" OUTPUT="${output}" npm run build:tailwind`; | ||
|
||
console.log(`INFO: build Tailwind CSS styles`); | ||
console.log(`INFO: run '${cmd}'`); | ||
|
||
await promisify(exec)(cmd).then(() => { | ||
console.log(`INFO: Tailwind CSS styles were built`); | ||
}); | ||
} else { | ||
console.log(`WARN: Tailwind CSS was disabled in config`); | ||
} | ||
} | ||
|
||
export function sourceStyleFilter(path: string): boolean { | ||
// Tailwind styles filter | ||
return !TAILWIND_STYLE.test(path); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters