Skip to content

Commit

Permalink
functional ui, sql gen
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-m-song committed May 27, 2024
1 parent 95b4c3e commit 723eaa9
Show file tree
Hide file tree
Showing 46 changed files with 8,295 additions and 4,290 deletions.
51 changes: 0 additions & 51 deletions .eslintrc.yml

This file was deleted.

6 changes: 6 additions & 0 deletions .jest/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
process.env.DISCORD_CLIENT_ID = "mock";
process.env.DISCORD_BOT_TOKEN = "mock";
process.env.YOUTUBE_API_KEY = "mock";
process.env.MINIO_ACCESS_KEY = "mock";
process.env.MINIO_SECRET_KEY = "mock";
process.env.CACHE_DATABASE_URL = ":memory:";
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arrowParens": "always",
"endOfLine": "lf",
"printWidth": 100,
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all"
}
7 changes: 0 additions & 7 deletions .prettierrc.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"[typescript]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
},
"testing.openTesting": "neverOpen"
}
62 changes: 62 additions & 0 deletions bin/generate-queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import fs from "fs";

const [source, destination] = process.argv.slice(2);
console.log({ source, destination });

console.log("reading from source...");
const raw = fs.readFileSync(source).toString();
const rawQueries = raw.split(/\n{2,}/g);

console.log("parsing and transforming...");
const set = new Set();
const uniques = (inputs?: any[]) => Array.from(new Set(inputs));
const transformed = rawQueries.map((raw, i) => {
const [header, ...lines] = raw.split("\n");
const name = header
.replace(/^-- name:\s*/, "")
.replace(/:.*$/, "")
.trim();
const capitalised = name[0].toUpperCase() + name.slice(1);

// const execType = header.split(/\s+/g).pop()?.replace(/^:/, "");
if (set.has(name)) {
throw new Error(`query ${name} was already defined (index: ${i})`);
}
set.add(name);

const query = lines
.join(" ")
.trim()
.replace(/\s+/g, " ")
.replace(/;$/, "")
.replace(/\(\s+/g, "(")
.replace(/\s+\)/g, ")");

const args = uniques(
query.match(/\$[a-z0-9]+/gi)?.map((arg) => arg.slice(1)),
);
const table = uniques(
query
.match(/(FROM|INTO|UPDATE)\s+([a-z0-9]+)/gi)
?.map((match) => match.replace(/(FROM|INTO|UPDATE)\s+/, "")),
);
console.log({ name, args, table, query });

const tableComment = `tables: ${table.map((i) => `\`${i}\``).join(", ")}`;
const argComment = `args: ${args.map((i) => `\`${i}\``).join(", ") || "N/A"}`;
const sqlDeclaration = `export const ${name} = \`${query}\`;`;
return [
"/**",
" * " + tableComment,
" *",
" * " + argComment,
" */",
sqlDeclaration,
].join("\n");
});

console.log("writing to destination...");
fs.writeFileSync(
destination,
transformed.toSorted((a, b) => a.localeCompare(b)).join("\n\n") + "\n",
);
35 changes: 35 additions & 0 deletions bin/query-yt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import "dotenv/config";
import { Youtube } from "../src/lib/Youtube";

(async () => {
const video = await Youtube.get("lYxE_whU9ak");
console.log({
thumbnail: video.items?.[0].snippet?.thumbnails?.default?.url,
videoId: video.items?.[0].id,
title: video.items?.[0].snippet?.title,
channelId: video.items?.[0].snippet?.channelId,
channelTitle: video.items?.[0].snippet?.channelTitle,
});

const playlist = await Youtube.list("PLWIol4T7LuSj4tNt8DvtHBE0c3UUiGGQ7");
console.log(
playlist.items?.map((item) => ({
thumbnail: item.snippet?.thumbnails?.default?.url,
videoId: item.snippet?.resourceId?.videoId,
title: item.snippet?.title,
channelId: item.snippet?.channelId,
channelTitle: item.snippet?.channelTitle,
})),
);

const search = await Youtube.search("deadmau5 xyz", 7);
console.log(
search.items?.map((item) => ({
thumbnail: search.items?.[0].snippet?.thumbnails?.default?.url,
videoId: item.id?.videoId,
title: item.snippet?.title,
channelId: item.snippet?.channelId,
channelTitle: item.snippet?.channelTitle,
})),
);
})();
75 changes: 75 additions & 0 deletions bin/run-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import "dotenv/config";
import "source-map-support/register";
import { Song, SongDAO, QueueDAO } from "../src/lib/cache";
import { server } from "../src/server/server";
import { config } from "../src/config";

(async () => {
const songs = [
{
thumbnail: "https://i.ytimg.com/vi/JUmpYJau5Vg/default.jpg",
videoId: "JUmpYJau5Vg",
title: "deadmau5 - XYZ",
channelId: "UCYEK6xds6eo-3tr4xRdflmQ",
channelTitle: "deadmau5",
},
{
thumbnail: "https://i.ytimg.com/vi/JUmpYJau5Vg/default.jpg",
videoId: "-xng12qdhDw",
title: "deadmau5 - XYZ (NERO Remix)",
channelId: "UCCbpTuRINyfjtwFkjHuII1w",
channelTitle: "mau5trap",
},
{
thumbnail: "https://i.ytimg.com/vi/JUmpYJau5Vg/default.jpg",
videoId: "N22HHpbYE88",
title: "deadmau5 - XYZ",
channelId: "UCeuT6VsBplPygtAgJ2Wpeqw",
channelTitle: "Obsessive Progressive",
},
{
thumbnail: "https://i.ytimg.com/vi/JUmpYJau5Vg/default.jpg",
videoId: "RSeqqi9-LQM",
title: "Deadmau5 XYZ Extended",
channelId: "UCXMF0L-H7_hbhvW6i4eAOBQ",
channelTitle: "Hubie Fix",
},
{
thumbnail: "https://i.ytimg.com/vi/JUmpYJau5Vg/default.jpg",
videoId: "pgZ0Ja5o3hc",
title: "XYZ – deadmau5 (Twitch live vers.) [No Mastering]",
channelId: "UCCpLIH3yIf7JYOwMalcsYRA",
channelTitle: "PotatoBadBoy",
},
{
thumbnail: "https://i.ytimg.com/vi/JUmpYJau5Vg/default.jpg",
videoId: "p_SpHwMsTWU",
title: "deadmau5 - XYZ (NERO Remix)",
channelId: "UC9UTBXS_XpBCUIcOG7fwM8A",
channelTitle: "UKF",
},
{
thumbnail: "https://i.ytimg.com/vi/JUmpYJau5Vg/default.jpg",
videoId: "EH0nM5GiK6o",
title: "deadmau5 - XYZ (NERO Remix)",
channelId: "UC3ifTl5zKiCAhHIBQYcaTeg",
channelTitle: "Proximity",
},
].map(
(raw, i): Song => ({
artistId: raw.channelId,
artistTitle: raw.channelTitle,
cachedAt: Date.now(),
artistUrl: `https://youtube.com/channel/${raw.channelId}`,
duration: i + 60,
songId: raw.videoId,
songTitle: raw.title,
songUrl: `https://youtube.com/watch?v=${raw.videoId}`,
thumbnail: raw.thumbnail,
}),
);
await SongDAO.put(...songs);
await QueueDAO.enqueue("test", songs[0].songId);
await SongDAO.list();
server.listen({ port: config.webPort });
})();
64 changes: 64 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module.exports = [
{
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
env: {
node: true,
browser: true,
},
plugins: ["import", "@typescript-eslint", "prettier"],
parser: "@typescript-eslint/parser",
rules: {
"import/order": [
"warn",
{
pathGroups: [
{
pattern: "~/**",
group: "parent",
position: "before",
},
],
groups: [
["builtin", "external"],
["parent", "sibling", "index"],
],
"newlines-between": "always",
alphabetize: {
order: "asc",
caseInsensitive: true,
},
},
],
"@typescript-eslint/no-explicit-any": ["off", {}],
"@typescript-eslint/ban-types": [
"error",
{
types: {
Function: false,
"extend-defaults": true,
},
},
],
"no-unused-vars": ["off"],
"@typescript-eslint/no-unused-vars": [
"warn",
{
vars: "all",
args: "after-used",
ignoreRestSiblings: false,
argsIgnorePattern: "^_",
},
],
},
overrides: [
{
files: ["**/*.test.ts", "**/*.test.tsx"],
plugins: ["jest"],
},
],
},
];
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
setupFiles: ["<rootDir>/.jest/setup.js"],
};
Loading

0 comments on commit 723eaa9

Please sign in to comment.