1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-17 00:10:58 -05:00

Offload commitHashes() to worker thread

This commit is contained in:
toast-ts 2023-10-16 04:32:29 +11:00
parent cb912c9f55
commit b3b4002afc
2 changed files with 22 additions and 11 deletions

View File

@ -1,3 +1,7 @@
interface CommitHashes {
localHash: string,
remoteHash: string
}
import Discord from 'discord.js'; import Discord from 'discord.js';
import pkg from 'typescript'; import pkg from 'typescript';
import MessageTool from '../helpers/MessageTool.js'; import MessageTool from '../helpers/MessageTool.js';
@ -10,14 +14,11 @@ import os from 'node:os';
import {Octokit} from '@octokit/rest'; import {Octokit} from '@octokit/rest';
import {createTokenAuth} from '@octokit/auth-token'; import {createTokenAuth} from '@octokit/auth-token';
import {readFileSync} from 'node:fs'; import {readFileSync} from 'node:fs';
import {execSync} from 'node:child_process'; import {Worker} from 'node:worker_threads';
const packageJson = JSON.parse(readFileSync('package.json', 'utf8')); const packageJson = JSON.parse(readFileSync('package.json', 'utf8'));
function commitHashes() { const workerThread = new Worker(new URL('../helpers/CommitHashes.js', import.meta.url));
const localHash = execSync('git rev-parse HEAD').toString().trim().slice(0, 7); const hashData = await new Promise<CommitHashes>(resolve=>workerThread.on('message', (data:CommitHashes)=>resolve(data)));
const remoteHash = execSync('git ls-remote origin HEAD').toString().split('\t')[0].slice(0, 7);
return { localHash, remoteHash };
}
export default { export default {
async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
@ -59,15 +60,15 @@ export default {
let githubRepo = {owner: 'AnxietyisReal', repo: 'Daggerbot-TS', ref: 'HEAD'}; let githubRepo = {owner: 'AnxietyisReal', repo: 'Daggerbot-TS', ref: 'HEAD'};
const octokit = new Octokit({auth: token, timeZone: 'Australia/NSW', userAgent: 'Daggerbot-TS'}); const octokit = new Octokit({auth: token, timeZone: 'Australia/NSW', userAgent: 'Daggerbot-TS'});
const github = { const github = {
remoteCommit: await octokit.repos.getCommit({...githubRepo, ref: commitHashes().remoteHash}), remoteCommit: await octokit.repos.getCommit({...githubRepo, ref: hashData.remoteHash}),
localCommit: await octokit.repos.getCommit({...githubRepo, ref: commitHashes().localHash}), localCommit: await octokit.repos.getCommit({...githubRepo, ref: hashData.localHash}),
} }
embed.addFields( embed.addFields(
{ {
name: '> __Repository__', value: MessageTool.concatMessage( name: '> __Repository__', value: MessageTool.concatMessage(
`**Local:** [${commitHashes().localHash}](${github.localCommit.data.html_url})`, `**Local:** [${hashData.localHash}](${github.localCommit.data.html_url})`,
`**Remote:** [${commitHashes().remoteHash}](${github.remoteCommit.data.html_url})`, `**Remote:** [${hashData.remoteHash}](${github.remoteCommit.data.html_url})`
) )
}, },
{name: '> __Dependencies__', value: MessageTool.concatMessage( {name: '> __Dependencies__', value: MessageTool.concatMessage(
@ -90,4 +91,4 @@ export default {
data: new Discord.SlashCommandBuilder() data: new Discord.SlashCommandBuilder()
.setName('statistics') .setName('statistics')
.setDescription('See a list of commands ordered by their usage or host stats') .setDescription('See a list of commands ordered by their usage or host stats')
} }

View File

@ -0,0 +1,10 @@
import {parentPort} from 'node:worker_threads';
import {execSync} from 'node:child_process';
async function commitHashes() {
const localHash = execSync('git rev-parse HEAD').toString().trim().slice(0, 7);
const remoteHash = execSync('git ls-remote origin HEAD').toString().split('\t')[0].slice(0, 7);
return { localHash, remoteHash };
}
commitHashes().then(data=>parentPort.postMessage(data))