Skip to content

Commit

Permalink
add prettier & dev deps
Browse files Browse the repository at this point in the history
  • Loading branch information
tandpfun committed Mar 12, 2022
1 parent 1fb4e71 commit 5842fe3
Show file tree
Hide file tree
Showing 7 changed files with 603 additions and 140 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
icons/
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"arrowParens": "avoid",
"semi": true,
"tabWidth": 2
}
12 changes: 6 additions & 6 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require("fs");
const fs = require('fs');

const iconsDir = fs.readdirSync("./icons");
const iconsDir = fs.readdirSync('./icons');
const icons = {};
for (const icon of iconsDir) {
const name = icon.replace(".svg", "").toLowerCase();
icons[name] = String(fs.readFileSync(`./icons/${icon}`));
const name = icon.replace('.svg', '').toLowerCase();
icons[name] = String(fs.readFileSync(`./icons/${icon}`));
}

if (!fs.existsSync("./dist")) fs.mkdirSync("./dist");
fs.writeFileSync("./dist/icons.json", JSON.stringify(icons));
if (!fs.existsSync('./dist')) fs.mkdirSync('./dist');
fs.writeFileSync('./dist/icons.json', JSON.stringify(icons));
246 changes: 122 additions & 124 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,158 +1,156 @@
const icons = require("./dist/icons.json");
const iconNameList = [
...new Set(Object.keys(icons).map((i) => i.split("-")[0])),
];
const icons = require('./dist/icons.json');
const iconNameList = [...new Set(Object.keys(icons).map(i => i.split('-')[0]))];
const shortNames = {
js: "javascript",
ts: "typescript",
py: "python",
tailwind: "tailwindcss",
vue: "vuejs",
nuxt: "nuxtjs",
go: "golang",
cf: "cloudflare",
wasm: "webassembly",
postgres: "postgresql",
k8s: "kubernetes",
next: "nextjs",
mongo: "mongodb",
md: "markdown",
ps: "photoshop",
ai: "illustrator",
pr: "premiere",
ae: "aftereffects",
scss: "sass",
sc: "scala",
js: 'javascript',
ts: 'typescript',
py: 'python',
tailwind: 'tailwindcss',
vue: 'vuejs',
nuxt: 'nuxtjs',
go: 'golang',
cf: 'cloudflare',
wasm: 'webassembly',
postgres: 'postgresql',
k8s: 'kubernetes',
next: 'nextjs',
mongo: 'mongodb',
md: 'markdown',
ps: 'photoshop',
ai: 'illustrator',
pr: 'premiere',
ae: 'aftereffects',
scss: 'sass',
sc: 'scala',
};
const themedIcons = [
"nodejs",
"python",
"tailwindcss",
"vuejs",
"nuxtjs",
"figma",
"react",
"cloudflare",
"java",
"php",
"kotlin",
"dart",
"mysql",
"postgresql",
"redis",
"angular",
"deno",
"vim",
"nextjs",
"grafana",
"clojure",
"coffeescript",
"lua",
"markdown",
"r",
"unity",
"zig",
"workers",
"linux",
"jenkins",
"bash",
"haxe",
"godot",
"scala",
"regex",
'nodejs',
'python',
'tailwindcss',
'vuejs',
'nuxtjs',
'figma',
'react',
'cloudflare',
'java',
'php',
'kotlin',
'dart',
'mysql',
'postgresql',
'redis',
'angular',
'deno',
'vim',
'nextjs',
'grafana',
'clojure',
'coffeescript',
'lua',
'markdown',
'r',
'unity',
'zig',
'workers',
'linux',
'jenkins',
'bash',
'haxe',
'godot',
'scala',
'regex',
];

const ICONS_PER_LINE = 15;
const ONE_ICON = 48;
const SCALE = ONE_ICON / (300 - 44);

function generateSvg(iconNames) {
const iconSvgList = iconNames.map((i) => icons[i]);
const iconSvgList = iconNames.map(i => icons[i]);

const length = Math.min(ICONS_PER_LINE * 300, iconNames.length * 300) - 44;
const height = Math.ceil(iconSvgList.length / ICONS_PER_LINE) * 300 - 44;
const scaledHeight = height * SCALE;
const scaledWidth = length * SCALE;
const length = Math.min(ICONS_PER_LINE * 300, iconNames.length * 300) - 44;
const height = Math.ceil(iconSvgList.length / ICONS_PER_LINE) * 300 - 44;
const scaledHeight = height * SCALE;
const scaledWidth = length * SCALE;

return `
return `
<svg width="${scaledWidth}" height="${scaledHeight}" viewBox="0 0 ${length} ${height}" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
${iconSvgList
.map(
(i, index) =>
`
.map(
(i, index) =>
`
<g transform="translate(${(index % ICONS_PER_LINE) * 300}, ${
Math.floor(index / ICONS_PER_LINE) * 300
})">
Math.floor(index / ICONS_PER_LINE) * 300
})">
${i}
</g>
`
)
.join(" ")}
)
.join(' ')}
</svg>
`;
}

function parseShortNames(names, theme = "dark") {
return names.map((name) => {
if (iconNameList.includes(name))
return name + (themedIcons.includes(name) ? `-${theme}` : "");
else if (name in shortNames)
return (
shortNames[name] +
(themedIcons.includes(shortNames[name]) ? `-${theme}` : "")
);
});
function parseShortNames(names, theme = 'dark') {
return names.map(name => {
if (iconNameList.includes(name))
return name + (themedIcons.includes(name) ? `-${theme}` : '');
else if (name in shortNames)
return (
shortNames[name] +
(themedIcons.includes(shortNames[name]) ? `-${theme}` : '')
);
});
}

async function handleRequest(request) {
const { pathname, searchParams } = new URL(request.url);
const { pathname, searchParams } = new URL(request.url);

const path = pathname.replace(/^\/|\/$/g, "");
const path = pathname.replace(/^\/|\/$/g, '');

if (path === "icons") {
const iconParam = searchParams.get("i") || searchParams.get("icons");
if (!iconParam)
return new Response("You didn't specify any icons!", { status: 400 });
const theme = searchParams.get("t") || searchParams.get("theme");
if (theme && theme !== "dark" && theme !== "light")
return new Response('Theme must be either "light" or "dark"', {
status: 400,
});
if (path === 'icons') {
const iconParam = searchParams.get('i') || searchParams.get('icons');
if (!iconParam)
return new Response("You didn't specify any icons!", { status: 400 });
const theme = searchParams.get('t') || searchParams.get('theme');
if (theme && theme !== 'dark' && theme !== 'light')
return new Response('Theme must be either "light" or "dark"', {
status: 400,
});

let iconShortNames = [];
if (iconParam === "all") iconShortNames = iconNameList;
else iconShortNames = iconParam.split(",");
let iconShortNames = [];
if (iconParam === 'all') iconShortNames = iconNameList;
else iconShortNames = iconParam.split(',');

const iconNames = parseShortNames(iconShortNames, theme || undefined);
if (!iconNames)
return new Response("You didn't format the icons param correctly!", {
status: 400,
});
const iconNames = parseShortNames(iconShortNames, theme || undefined);
if (!iconNames)
return new Response("You didn't format the icons param correctly!", {
status: 400,
});

const svg = generateSvg(iconNames);
const svg = generateSvg(iconNames);

return new Response(svg, { headers: { "Content-Type": "image/svg+xml" } });
} else if (path === "api/icons") {
return new Response(JSON.stringify(iconNameList), {
headers: {
"content-type": "application/json;charset=UTF-8",
},
});
} else if (path === "api/svgs") {
return new Response(JSON.stringify(icons), {
headers: {
"content-type": "application/json;charset=UTF-8",
},
});
} else {
return fetch(request);
}
return new Response(svg, { headers: { 'Content-Type': 'image/svg+xml' } });
} else if (path === 'api/icons') {
return new Response(JSON.stringify(iconNameList), {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
});
} else if (path === 'api/svgs') {
return new Response(JSON.stringify(icons), {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
});
} else {
return fetch(request);
}
}

addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
addEventListener('fetch', event => {
event.respondWith(
handleRequest(event.request).catch(
err => new Response(err.stack, { status: 500 })
)
);
});
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
"dev": "node build.js && miniflare --watch",
"build": "node build.js",
"deploy": "node build.js && wrangler publish"
},
"devDependencies": {
"@cloudflare/wrangler": "^1.19.8",
"miniflare": "^2.3.0",
"prettier": "^2.5.1"
}
}
Loading

0 comments on commit 5842fe3

Please sign in to comment.