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

web #45

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft

web #45

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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'],
},
],
};
50 changes: 0 additions & 50 deletions .eslintrc.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
on: workflow_call

jobs:
test:
runs-on: self-hosted

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc

- name: Install dependencies
run: npm install

- name: Check generated files are up to date
run: |
npm run generate
git diff --exit-code

- name: Lint
run: npm run lint

- name: Test
run: npm run test:ci
7 changes: 7 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: On push

on: push

jobs:
test:
uses: ./.github/workflows/_test.yml
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"
}
50 changes: 50 additions & 0 deletions bin/generate-queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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.sort((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 });
})();
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"],
};
7 changes: 4 additions & 3 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"restartable": "r",
"delay": 1000,
"ext": "ts",
"watch": ["src/**"],
"exec": "ts-node src/index.ts"
"ext": "ts,tsx,css",
"watch": [
"src/**"
]
}
Loading