Skip to content

Commit

Permalink
use import.meta.dirname, suppress grammar script output unless errored
Browse files Browse the repository at this point in the history
  • Loading branch information
IRONM00N committed Feb 24, 2024
1 parent 176e4dc commit 531a5fa
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 238 deletions.
5 changes: 5 additions & 0 deletions grammar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
grammar() {
yarn nearleyc lib/dice/dice-grammar.ne -o dist/lib/dice/dice-grammar.umd.js && yarn rollup -c rollup-nearley.js
}

output=$(grammar 2>&1 | tr -d '\000') || echo $output
15 changes: 6 additions & 9 deletions lib/extensions/discord-akairo/TanzaniteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import assert from 'node:assert';
import type { EventEmitter } from 'node:events';
import path from 'node:path';
import readline from 'node:readline';
import { fileURLToPath } from 'node:url';
import { Sequelize, type Options as SequelizeOptions, type Sequelize as SequelizeType } from 'sequelize';
import { ExtendedGuild } from '../discord.js/ExtendedGuild.js';
import { ExtendedGuildMember } from '../discord.js/ExtendedGuildMember.js';
Expand Down Expand Up @@ -145,8 +144,6 @@ const rl = readline.createInterface({
terminal: false
});

const __dirname = path.dirname(fileURLToPath(import.meta.url));

/**
* The main hub for interacting with the Discord API.
*/
Expand Down Expand Up @@ -279,17 +276,17 @@ export class TanzaniteClient<Ready extends boolean = boolean> extends AkairoClie

/* =-=-= handlers =-=-= */
this.listenerHandler = new BotListenerHandler(this, {
directory: path.join(__dirname, '../../../src/listeners'),
directory: path.join(import.meta.dirname, '../../../src/listeners'),
extensions: ['.js'],
automateCategories: true
});
this.inhibitorHandler = new BotInhibitorHandler(this, {
directory: path.join(__dirname, '../../../src/inhibitors'),
directory: path.join(import.meta.dirname, '../../../src/inhibitors'),
extensions: ['.js'],
automateCategories: true
});
this.taskHandler = new BotTaskHandler(this, {
directory: path.join(__dirname, '../../../src/tasks'),
directory: path.join(import.meta.dirname, '../../../src/tasks'),
extensions: ['.js'],
automateCategories: true
});
Expand Down Expand Up @@ -320,7 +317,7 @@ export class TanzaniteClient<Ready extends boolean = boolean> extends AkairoClie
};

this.commandHandler = new BotCommandHandler(this, {
directory: path.join(__dirname, '../../../src/commands'),
directory: path.join(import.meta.dirname, '../../../src/commands'),
extensions: ['.js'],
prefix: async ({ guild }: Message) => {
if (this.config.isDevelopment) return 'dev ';
Expand Down Expand Up @@ -352,7 +349,7 @@ export class TanzaniteClient<Ready extends boolean = boolean> extends AkairoClie
aliasReplacement: /-/g
});
this.contextMenuCommandHandler = new ContextMenuCommandHandler(this, {
directory: path.join(__dirname, '../../../src/context-menu-commands'),
directory: path.join(import.meta.dirname, '../../../src/context-menu-commands'),
extensions: ['.js'],
automateCategories: true
});
Expand Down Expand Up @@ -478,7 +475,7 @@ export class TanzaniteClient<Ready extends boolean = boolean> extends AkairoClie
}

public registerFonts() {
return GlobalFonts.loadFontsFromDir(path.join(__dirname, '../../../assets/fonts'));
return GlobalFonts.loadFontsFromDir(path.join(import.meta.dirname, '../../../assets/fonts'));
}

/**
Expand Down
4 changes: 1 addition & 3 deletions lib/utils/Minecraft_Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { parse } from '@ironm00n/nbt-ts';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
mcToAnsi,
type McItemId,
Expand All @@ -14,8 +13,7 @@ import {
type SbItemId
} from './Minecraft.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repo = path.join(__dirname, '../../../../../neu-item-repo');
const repo = path.join(import.meta.dirname, '../../../../../neu-item-repo');
const itemPath = path.join(repo, 'items');
const items = await fs.readdir(itemPath);

Expand Down
2 changes: 1 addition & 1 deletion misc/tooltip.random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function stripCodes(str: string) {
// console.log(stripCodes(item.displayname));
// tooltip(item);

// await fs.writeFile(path.join(__dirname, 'test.png'), tooltip(item));
// await fs.writeFile(path.join(import.meta.dirname, 'test.png'), tooltip(item));

// await promisify(setTimeout)(50);
// }
11 changes: 4 additions & 7 deletions misc/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ process.removeAllListeners('warning');
import canvas, { GlobalFonts } from '@napi-rs/canvas';
import fs from 'node:fs/promises';
import path from 'node:path';
import url from 'node:url';
import { promisify } from 'node:util';
import tinycolor from 'tinycolor2';
import { Color, ColorCode, FormattingCodes, formattingInfo, RawNeuItem } from './formattingInfo.js';

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

GlobalFonts.loadFontsFromDir(path.join(__dirname, '../assets/fonts'));
GlobalFonts.loadFontsFromDir(path.join(import.meta.dirname, '../assets/fonts'));

const colorRegex = /(?=§[0-9a-fklmnor])/;

// the characters that the minecraft font supports
const supportedChars = /(?=[\u0020-\u007e\u00a1\u00a5\u00a8\u00bf\u00d8\u00e5\u00e6\u00f8\u2018\u2019\u201c\u201d])/;

const repo = path.join(__dirname, '../neu-item-repo');
const repo = path.join(import.meta.dirname, '../neu-item-repo');
const itemPath = path.join(repo, 'items');
const items = (await fs.readdir(itemPath)).filter((f) => !f.endsWith('NPC.json') && !f.endsWith('MONSTER.json'));

Expand All @@ -26,7 +23,7 @@ const id = process.argv[2];
if (id) {
const item = (await import(path.join(itemPath, `${id}.json`), { assert: { type: 'json' } })).default as RawNeuItem;

await fs.writeFile(path.join(__dirname, 'test.png'), tooltip(item));
await fs.writeFile(path.join(import.meta.dirname, 'test.png'), tooltip(item));
} else {
for (const dir of items) {
const item = (await import(path.join(itemPath, dir), { assert: { type: 'json' } })).default as RawNeuItem;
Expand All @@ -35,7 +32,7 @@ if (id) {
const tooltipImg = tooltip(item);
console.timeEnd(item.internalname);

await fs.writeFile(path.join(__dirname, 'test.png'), tooltipImg);
await fs.writeFile(path.join(import.meta.dirname, 'test.png'), tooltipImg);

await promisify(setTimeout)(100);
}
Expand Down
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"build": "yarn rimraf dist && yarn tsc -b && yarn build:grammar",
"build:no-emit": "yarn tsc --noEmit",
"build:keep": "yarn tsc -b",
"build:grammar": "yarn nearleyc lib/dice/dice-grammar.ne -o dist/lib/dice/dice-grammar.umd.js && yarn rollup -c rollup-nearley.js",
"build:grammar": "./grammar.sh",
"start": "yarn build && yarn start:raw",
"start:keep": "yarn build:keep && yarn start:raw",
"start:dry": "yarn start dry",
Expand Down Expand Up @@ -62,12 +62,12 @@
"@discordjs/builders": "^1.7.0",
"@discordjs/rest": "^2.2.0",
"@ironm00n/nbt-ts": "^1.4.0",
"@napi-rs/canvas": "^0.1.45",
"@napi-rs/canvas": "^0.1.49",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-typescript": "^11.1.6",
"@sentry/integrations": "^7.101.1",
"@sentry/node": "^7.101.1",
"@sentry/tracing": "^7.101.1",
"@sentry/integrations": "^7.102.1",
"@sentry/node": "^7.102.1",
"@sentry/tracing": "^7.102.1",
"@tanzanite/deep-lock": "^1.1.1",
"@tanzanite/discord-akairo": "dev",
"@tanzanite/discord.js-minesweeper": "^1.2.0",
Expand All @@ -81,8 +81,8 @@
"fuse.js": "^7.0.0",
"googleapis": "^133.0.0",
"lodash-es": "^4.17.21",
"mathjs": "^12.3.2",
"nanoid": "^5.0.5",
"mathjs": "^12.4.0",
"nanoid": "^5.0.6",
"nearley": "^2.20.1",
"numeral": "^2.0.6",
"pg": "^8.11.3",
Expand All @@ -98,20 +98,20 @@
},
"devDependencies": {
"@sapphire/snowflake": "^3.5.3",
"@sentry/types": "^7.101.1",
"@types/eslint": "^8.56.2",
"@sentry/types": "^7.102.1",
"@types/eslint": "^8.56.3",
"@types/express": "^4.17.21",
"@types/lodash-es": "^4.17.12",
"@types/nearley": "^2.11.5",
"@types/node": "^20.11.19",
"@types/node": "^20.11.20",
"@types/node-fetch": "^2.6.11",
"@types/numeral": "^2.0.5",
"@types/pg": "^8.11.0",
"@types/pg": "^8.11.1",
"@types/tinycolor2": "^1.4.6",
"@types/validator": "^13.11.9",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"eslint": "^8.56.0",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-deprecation": "^2.0.0",
"husky": "^9.0.11"
Expand Down
6 changes: 2 additions & 4 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ Logger.init();
import { config } from '#config';
import { Sentry } from '#lib/common/Sentry.js';
import { TanzaniteClient } from '#lib/extensions/discord-akairo/TanzaniteClient.js';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';

const isDry = process.argv.includes('dry');

const __dirname = dirname(fileURLToPath(import.meta.url));
process.env.PROJECT_DIR = join(__dirname, __dirname.includes('dist') ? '../..' : '..');
process.env.PROJECT_DIR = join(import.meta.dirname, import.meta.dirname.includes('dist') ? '../..' : '..');

if (!isDry && config.credentials.sentryDsn !== null) {
new Sentry(process.env.PROJECT_DIR, config);
Expand Down
Loading

0 comments on commit 531a5fa

Please sign in to comment.