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

projects screenshot & enhance component list design #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
This is repository with examples of simple UI components. The repository is based on Next.js and React.js.

![screenshot](./docs/screenshot.png)

# installation

* Clone the repo with
Expand Down
Binary file added docs/screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,431 changes: 1,291 additions & 140 deletions index.html

Large diffs are not rendered by default.

115 changes: 87 additions & 28 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,41 +1,100 @@
import fs from "fs";
import glob from "fast-glob";

async function start() {
const HTMLFiles = await glob(["./**/*.html"], {
ignore: ["./index.html", "**/node_modules/**"],
});
HTMLFiles.sort();
const result = HTMLFiles.map((filepath) => {
const arr = filepath.split("/");
if (arr[0] === "react-examples") {
return `<li><a href="${filepath}">react-examples/${arr[2]}</a></li>`;
}
return `<li><a href="${filepath}">${
arr[1].startsWith("index.") ? arr[0] : filepath
}</a></li>`;
}).join("\n");
fs.writeFile(
"./index.html",
`
import fs from 'fs'
import glob from 'fast-glob'
import puppeteer from 'puppeteer'
import path from 'path'

const SCREENSHOT_DIR = './screenshot'
const INDEX_HTML_PATH = './index.html'

const browser = await puppeteer.launch().catch((err) => {
console.error('Error launching browser:', err)
process.exit(1)
})

const takeScreenshot = async (fileUrl, imageName) => {
if (!fs.existsSync(SCREENSHOT_DIR)) {
fs.mkdirSync(SCREENSHOT_DIR)
}

const page = await browser.newPage()
try {
const scriptDir = path.dirname(new URL(import.meta.url).pathname)
const filepath = path.join(scriptDir, fileUrl)
await page.goto('file://' + filepath)

const screenshotPath = path.join(
SCREENSHOT_DIR,
`screenshot-${imageName}.png`
)
await page.screenshot({ path: screenshotPath })
} catch (error) {
console.error(`Error taking screenshot for ${fileUrl}:`, error)
} finally {
await page.close()
}
}

const getFileName = (filepath) => {
const splits = filepath.split('/')

if (splits[0] === 'react-examples') {
return `react-examples/${splits[2]}`
}
return splits[1].startsWith('index.') ? splits[0] : filepath
}

const generateComponentCard = (filepath, fileName, imageName) => `
<li class="cards__card">
<a href="${filepath}" class="cards__card-link"
><div class="cards__card-image-cont">
<img src="./${SCREENSHOT_DIR}/screenshot-${imageName}.png" alt="screenshot">
</div>
<h2 class="cards__card-info">${fileName}</h2></a
>
</li>`

const generateHtmlFile = (components) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./style.css">
<title>ui</title>
</head>
<body>
<!-- generated by ./index.mjs -->
<h1>ui</h1>
<ul>
${result}
<h1 class="main-header">UI</h1>
<ul class="cards">
${components}
</ul>
</body>
</html>
`,
(err) => {}
);
</html>`

const start = async () => {
try {
const HTMLFiles = await glob(['./**/*.html'], {
ignore: [INDEX_HTML_PATH, '**/node_modules/**'],
})

HTMLFiles.sort()

const components = await Promise.all(
HTMLFiles.map(async (filepath, index) => {
const fileName = getFileName(filepath)
await takeScreenshot(filepath, index).then(() => {
console.log(`Created ${filepath} | With screenshot-${index}.png`);
})
return generateComponentCard(filepath, fileName, index)
})
)

fs.writeFileSync(INDEX_HTML_PATH, generateHtmlFile(components.join('\n')))
} catch (err) {
console.error(err)
} finally {
await browser.close()
}
}

start();
start()