mirror of
https://github.com/toast-ts/Daggerbot-TS.git
synced 2024-11-17 12:21:00 -05:00
Offload commitHashes()
to worker thread
This commit is contained in:
parent
ab59610aff
commit
c55bf8c6c0
@ -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(
|
||||||
|
10
src/helpers/CommitHashes.ts
Normal file
10
src/helpers/CommitHashes.ts
Normal 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))
|
Loading…
Reference in New Issue
Block a user