Skip to content

Commit

Permalink
Merge pull request #139 from JacobLinCool/monorepo
Browse files Browse the repository at this point in the history
refactor: move to monorepo and use hono for workers
  • Loading branch information
JacobLinCool authored Dec 14, 2024
2 parents b9782dc + 853b0b4 commit 7a57820
Show file tree
Hide file tree
Showing 54 changed files with 6,270 additions and 3,870 deletions.
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default tseslint.config(
...tseslint.configs.recommended,
prettier,
{
ignores: ["dist/**.js", "lib/**.js"],
ignores: ["packages/*/dist", "packages/cloudflare-worker/worker-configuration.d.ts"],
},
);
56 changes: 23 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "leetcode-card",
"private": true,
"name": "leetcode-card-monorepo",
"version": "1.0.1",
"description": "Show your dynamically generated LeetCode stats on your GitHub profile or your website!",
"license": "MIT",
Expand All @@ -13,48 +14,37 @@
"lib"
],
"scripts": {
"test": "vitest --coverage --coverage.include src",
"prepare": "husky",
"dev": "wrangler dev",
"build:worker": "esbuild src/cloudflare-worker/index.ts --outfile=dist/worker.js --bundle --minify --format=esm --loader:.html=text --keep-names",
"build:package": "esbuild src/core/index.ts --outfile=lib/index.js --bundle --format=esm --keep-names",
"build:image": "selflare compile --script dist/worker.js && docker compose build",
"build": "npm run build:worker && npm run build:package",
"test": "vitest --coverage --coverage.include packages/*/src",
"build:worker": "pnpm run --filter cloudflare-worker build",
"build:package": "pnpm run --filter leetcode-card build",
"build:image": "selflare compile --script packages/cloudflare-worker/dist/worker.js && docker compose build",
"build": "pnpm -r build",
"format": "prettier --write . --ignore-path .gitignore",
"lint": "eslint .",
"deploy": "wrangler deploy",
"tail": "wrangler tail"
"lint": "eslint ."
},
"keywords": [
"leetcode",
"stats",
"card"
],
"dependencies": {
"itty-router": "4.0.27",
"leetcode-query": "1.1.0",
"nano-font": "0.3.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240222.0",
"@eslint/js": "^8.57.0",
"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"@vitest/coverage-v8": "^1.3.1",
"esbuild": "^0.20.1",
"eslint": "^8.57.0",
"@eslint/js": "^9.17.0",
"@types/node": "^22.10.2",
"@typescript-eslint/eslint-plugin": "^8.18.0",
"@typescript-eslint/parser": "^8.18.0",
"@vitest/coverage-v8": "^2.1.8",
"eslint": "^9.17.0",
"eslint-config-prettier": "^9.1.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"prettier-plugin-organize-imports": "^3.2.4",
"selflare": "^1.0.0",
"husky": "^9.1.7",
"lint-staged": "^15.2.11",
"prettier": "^3.4.2",
"prettier-plugin-organize-imports": "^4.1.0",
"selflare": "^1.1.2",
"tsup": "8.0.2",
"typescript": "^5.3.3",
"typescript-eslint": "^7.1.0",
"vitest": "^1.3.1",
"wrangler": "^3.30.1"
"typescript": "^5.7.2",
"typescript-eslint": "^8.18.0",
"vitest": "^2.1.8"
},
"repository": {
"type": "git",
Expand All @@ -64,7 +54,7 @@
"url": "https://github.com/JacobLinCool/LeetCode-Stats-Card/issues"
},
"homepage": "https://github.com/JacobLinCool/LeetCode-Stats-Card#readme",
"packageManager": "pnpm@8.15.4",
"packageManager": "pnpm@9.15.0",
"lint-staged": {
"*.{js,ts}": [
"eslint --fix",
Expand Down
172 changes: 172 additions & 0 deletions packages/cloudflare-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# wrangler project

.dev.vars
.wrangler/
4 changes: 4 additions & 0 deletions packages/cloudflare-worker/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "*.html" {
const content: string;
export default content;
}
25 changes: 25 additions & 0 deletions packages/cloudflare-worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"private": true,
"name": "cloudflare-worker",
"version": "0.0.0",
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev",
"build": "esbuild src/index.ts --outfile=dist/worker.js --bundle --format=esm --loader:.html=text --keep-names",
"test": "vitest",
"cf-typegen": "wrangler types"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.5.36",
"@cloudflare/workers-types": "^4.20241205.0",
"esbuild": "^0.24.0",
"typescript": "^5.7.2",
"vitest": "2.1.8",
"wrangler": "^3.95.0"
},
"dependencies": {
"hono": "^4.6.14",
"leetcode-card": "workspace:*"
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { supported as themes } from "../../core/exts/theme";
import { supported as themes } from "leetcode-card";
import html from "./demo.html";
import { fonts } from "./google-fonts";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { Router } from "itty-router";
import { Config, Generator } from "../core";
import { Hono } from "hono";
import { cors } from "hono/cors";
import { Config, Generator } from "leetcode-card";
import demo from "./demo";
import Header from "./headers";
import { sanitize } from "./sanitize";

const router = Router();
const app = new Hono().use("*", cors());

router.get("/favicon.ico", async () => {
return Response.redirect(
app.get("/favicon.ico", (c) => {
return c.redirect(
"https://raw.githubusercontent.com/JacobLinCool/leetcode-stats-card/main/favicon/leetcode.ico",
301,
);
});

async function generate(config: Record<string, string>, req: Request): Promise<Response> {
async function generate(
config: Record<string, string>,
header: Record<string, string>,
): Promise<Response> {
let sanitized: Config;
try {
sanitized = sanitize(config);
Expand All @@ -27,9 +31,7 @@ async function generate(config: Record<string, string>, req: Request): Promise<R
const cache_time = parseInt(config.cache || "300") ?? 300;
const cache = await caches.open("leetcode");

const generator = new Generator(cache, {
"user-agent": req.headers.get("user-agent") || "Unknown",
});
const generator = new Generator(cache, header);
generator.verbose = true;

const headers = new Header().add("cors", "svg");
Expand All @@ -39,26 +41,34 @@ async function generate(config: Record<string, string>, req: Request): Promise<R
}

// handle path variable
router.get("/:username", async (request) => {
request.query.username = request.params.username;
return await generate(request.query as never, request);
app.get("/:username", async (c) => {
const username = c.req.param("username");
const query = Object.fromEntries(new URL(c.req.url).searchParams);
query.username = username;
return await generate(query, {
"user-agent": c.req.header("user-agent") || "Unknown",
});
});

// handle query string
router.get("*", async (req: { query: Record<string, string> }) => {
if (!req.query.username) {
app.get("*", async (c) => {
const query = Object.fromEntries(new URL(c.req.url).searchParams);

if (!query.username) {
return new Response(demo, {
headers: new Header().add("cors", "html"),
});
}

return await generate(req.query, req as unknown as Request);
return await generate(query, {
"user-agent": c.req.header("user-agent") || "Unknown",
});
});

// 404 for all other routes
router.all("*", () => new Response("Not Found.", { status: 404 }));
app.all("*", () => new Response("Not Found.", { status: 404 }));

export async function handle(request: Request): Promise<Response> {
console.log(`${request.method} ${request.url}`);
return router.handle(request);
return app.fetch(request);
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 7a57820

Please sign in to comment.