-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement HTML template components
- Loading branch information
Showing
13 changed files
with
231 additions
and
136 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
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
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,98 @@ | ||
<template id="search_helpers_default"> | ||
<div class="line" data-value="version:"><b>version:</b> | ||
<p>semver</p> | ||
</div> | ||
<div class="line" data-value="package:"><b>package:</b> | ||
<p>name</p> | ||
</div> | ||
<div class="line" data-value="license:"><b>license:</b> | ||
<p>name</p> | ||
</div> | ||
<div class="line" data-value="ext:"><b>ext:</b> | ||
<p>file extension</p> | ||
</div> | ||
<div class="line" data-value="builtin:"><b>builtin:</b> | ||
<p>node.js module</p> | ||
</div> | ||
<div class="line" data-value="author:"><b>author:</b> | ||
<p>name/email</p> | ||
</div> | ||
<div class="line" data-value="flag:"><b>flag:</b> | ||
<p>name</p> | ||
</div> | ||
<div class="line" data-value="size:"><b>size:</b> | ||
<p>size</p> | ||
</div> | ||
</template> | ||
|
||
<template id="search_helpers_flags"> | ||
<div class="line" data-value="hasExternalCapacity"> | ||
<p>🌍 hasExternalCapacity</p> | ||
</div> | ||
<div class="line" data-value="hasIndirectDependencies"> | ||
<p>🌲 hasIndirectDependencies</p> | ||
</div> | ||
<div class="line" data-value="hasWarnings"> | ||
<p>⚠️ hasWarnings</p> | ||
</div> | ||
<div class="line" data-value="hasNoLicense"> | ||
<p>📜 hasNoLicense</p> | ||
</div> | ||
<div class="line" data-value="hasMultipleLicenses"> | ||
<p>📚 hasMultipleLicenses</p> | ||
</div> | ||
<div class="line" data-value="hasMinifiedCode"> | ||
<p>🔬 hasMinifiedCode</p> | ||
</div> | ||
<div class="line" data-value="hasCustomResolver"> | ||
<p>💎 hasCustomResolver</p> | ||
</div> | ||
<div class="line" data-value="hasMissingOrUnusedDependency"> | ||
<p>👀 hasMissingOrUnusedDependency</p> | ||
</div> | ||
<div class="line" data-value="hasScript"> | ||
<p>📦 hasScript</p> | ||
</div> | ||
<div class="line" data-value="hasNativeCode"> | ||
<p>🐲 hasNativeCode</p> | ||
</div> | ||
<div class="line" data-value="hasBannedFile"> | ||
<p>⚔️ hasBannedFile</p> | ||
</div> | ||
<div class="line" data-value="isGit"> | ||
<p>☁️ isGit</p> | ||
</div> | ||
<div class="line" data-value="isDeprecated"> | ||
<p>⛔️ isDeprecated</p> | ||
</div> | ||
<div class="line" data-value="isOutdated"> | ||
<p>⌚️ isOutdated</p> | ||
</div> | ||
<div class="line" data-value="hasManyPublishers"> | ||
<p>👥 hasManyPublishers</p> | ||
</div> | ||
<div class="line" data-value="isDead"> | ||
<p>💀 isDead</p> | ||
</div> | ||
<div class="line" data-value="hasVulnerabilities"> | ||
<p>🚨 hasVulnerabilities</p> | ||
</div> | ||
<div class="line" data-value="hasDuplicate"> | ||
<p>🎭 hasDuplicate</p> | ||
</div> | ||
</template> | ||
|
||
<template id="searchbar-content"> | ||
<div id="searchbar"> | ||
<i class="icon-globe-alt-outline"></i> | ||
<div class="search-items"></div> | ||
<input type="text" placeholder="[[=z.token('searchbar_placeholder')]]" id="search-bar-input" autocomplete="off" | ||
autocorrect="off" autocapitalize="off" spellcheck="false"></input> | ||
|
||
<div class="search-result-background"> | ||
<div class="search-result-pannel" id="package-list"> | ||
<div class="helpers"></div> | ||
</div> | ||
</div> | ||
</div> | ||
</template> |
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
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,96 @@ | ||
// Import Node.js Dependencies | ||
import path from "node:path"; | ||
import fs from "node:fs/promises"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
// Import Third-party Dependencies | ||
import zup from "zup"; | ||
import * as i18n from "@nodesecure/i18n"; | ||
import chokidar from "chokidar"; | ||
|
||
// Import Internal Dependencies | ||
import { logger } from "./logger.js"; | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
const kProjectRootDir = path.join(__dirname, "..", ".."); | ||
const kComponentsDir = path.join(kProjectRootDir, "public", "components"); | ||
|
||
export class ViewBuilder { | ||
#cached = null; | ||
|
||
constructor(options = {}) { | ||
const { autoReload = false } = options; | ||
|
||
if (autoReload) { | ||
this.#enableWatcher(); | ||
} | ||
} | ||
|
||
async #enableWatcher() { | ||
logger.info(`[ViewBuilder] autoReload is enabled`); | ||
|
||
const watcher = chokidar.watch(kComponentsDir, { | ||
persistent: false, | ||
awaitWriteFinish: true, | ||
ignored: (path, stats) => stats?.isFile() && !path.endsWith(".html") | ||
}); | ||
watcher.on("change", (filePath) => this.#freeCache(filePath)); | ||
} | ||
|
||
async #freeCache( | ||
filePath | ||
) { | ||
logger.info(`[ViewBuilder] the cache has been released`); | ||
logger.info(`[ViewBuilder](filePath: ${filePath})`); | ||
|
||
this.#cached = null; | ||
} | ||
|
||
async #build() { | ||
if (this.#cached) { | ||
return this.#cached; | ||
} | ||
|
||
let HTMLStr = await fs.readFile( | ||
path.join(kProjectRootDir, "views", "index.html"), | ||
"utf-8" | ||
); | ||
|
||
const componentsPromises = []; | ||
for await ( | ||
const htmlComponentPath of fs.glob("**/*.html", { cwd: kComponentsDir }) | ||
) { | ||
componentsPromises.push( | ||
fs.readFile( | ||
path.join(kComponentsDir, htmlComponentPath), | ||
"utf-8" | ||
) | ||
); | ||
} | ||
const components = await Promise.all( | ||
componentsPromises | ||
); | ||
HTMLStr += components.reduce((prev, curr) => prev + curr, ""); | ||
|
||
this.#cached = HTMLStr; | ||
logger.info(`[ViewBuilder] the cache has been hydrated`); | ||
|
||
return HTMLStr; | ||
} | ||
|
||
/** | ||
* @returns {Promise<string>} | ||
*/ | ||
async render() { | ||
const i18nLangName = await i18n.getLocalLang(); | ||
|
||
const HTMLStr = await this.#build(); | ||
const templateStr = zup(HTMLStr)({ | ||
lang: i18n.getTokenSync("lang"), | ||
i18nLangName, | ||
token: (tokenName) => i18n.getTokenSync(`ui.${tokenName}`) | ||
}); | ||
|
||
return templateStr; | ||
} | ||
} |
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
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
Oops, something went wrong.