Skip to content

Commit

Permalink
feat: Use swc to run
Browse files Browse the repository at this point in the history
  • Loading branch information
PartMan7 committed Oct 28, 2024
1 parent 361af4a commit f89e613
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 31 deletions.
201 changes: 201 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"notify-unpushed": "sh ./scripts/notify-unpushed.sh",
"prettify": "prettier -w",
"prepare": "sh ./scripts/setup.sh",
"start": "ts-node src/index.ts",
"start": "ts-node --swc src/index.ts",
"test": "npm run lint && npm run tsc",
"tsc": "tsc"
},
Expand All @@ -30,6 +30,7 @@
"prettier": "^3.3.3"
},
"dependencies": {
"@swc/core": "^1.7.40",
"ansi-to-html": "^0.7.2",
"axios": "^1.6.0",
"chokidar": "^3.5.3",
Expand Down
1 change: 1 addition & 0 deletions src/config/ps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '@/env';
import { Tools } from 'ps-client';

export const ranks = ['locked', 'muted', 'regular', 'whitelist', 'voice', 'driver', 'mod', 'bot', 'owner', 'admin'] as const;
Expand Down
2 changes: 2 additions & 0 deletions src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import mongoose from 'mongoose';

import '@/env';

export function connect(): Promise<typeof mongoose> {
return mongoose.connect(process.env.MONGO_URL);
}
Expand Down
3 changes: 3 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dotenv from 'dotenv';

dotenv.config();
23 changes: 17 additions & 6 deletions src/eval.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { inspect } from 'util';
import ANSIConverter from 'ansi-to-html';
import { inspect } from 'util';

import * as cache from '@/cache';
import { Message as PSMessage } from 'ps-client';
import { PSCommandContext } from '@/types/chat';

export const convertANSI = ANSIConverter ? new ANSIConverter() : null;

Expand Down Expand Up @@ -67,14 +71,21 @@ export function formatValue(value: unknown, mode: EvalModes): string {
}
}

export async function evaluate(code: string, mode: EvalModes, context: Record<string, unknown> = {}): Promise<EvalOutput> {
export async function evaluate(
code: string,
mode: EvalModes,
passedContext: {
message: PSMessage;
context: PSCommandContext;
} // Add Discord case here, eventually
): Promise<EvalOutput> {
let success: boolean, value: unknown;
try {
const res = await (() => {
// @ts-expect-error -- Allow 'with' to forward context
with (context) {
return eval(code);
}
const { message, context } = passedContext;
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- Storing in context for eval()
const evalContext = { message, context, cache };
return eval(code);
})();
success = true;
value = res;
Expand Down
2 changes: 1 addition & 1 deletion src/globals/fs-sync.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as _fs from 'fs';
import _fs from 'fs';
export as namespace fsSync;
export = _fs;
7 changes: 5 additions & 2 deletions src/globals/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import '@/globals/prototypes';

import axios from 'axios';
import * as fsSync from 'fs';
import * as path from 'path';
import fsSync from 'fs';
import path from 'path';
import React from 'react';

global.axios = axios;
global.fs = fsSync.promises;
global.fsSync = fsSync;
Expand Down
2 changes: 1 addition & 1 deletion src/globals/path.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import * as _path from 'path';
import _path from 'path';
export as namespace path;
export = _path;
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import * as dotenv from 'dotenv';
dotenv.config();

import '@/globals/prototypes';
import '@/globals';

import PS from '@/ps';
Expand All @@ -12,8 +8,8 @@ global.PS = PS;
global.Discord = Discord;
global.Web = Web;

import * as DB from '@/database';
DB.connect().then(() => log('Connected to the database!'));
import { connect } from '@/database';
connect().then(() => log('Connected to the database!'));

import { emitter } from '@/sentinel';

Expand Down
7 changes: 1 addition & 6 deletions src/ps/commands/eval.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { evaluate } from '@/utils/eval';
import * as cache from '@/cache';

export const command: PSCommand = {
name: 'eval',
Expand All @@ -13,11 +12,7 @@ export const command: PSCommand = {
arg,
originalCommand: [originalCommand],
} = context;
const res = await evaluate(arg, originalCommand === 'exec' ? 'ABBR_OUTPUT' : 'COLOR_OUTPUT', {
message,
context,
cache,
});
const res = await evaluate(arg, originalCommand === 'exec' ? 'ABBR_OUTPUT' : 'COLOR_OUTPUT', { message, context });
if (originalCommand === 'eval')
message.replyHTML(`<br/>${res.output}`); // Add a slight gap
else if (originalCommand === 'run') message.sendHTML(res.output);
Expand Down
2 changes: 1 addition & 1 deletion src/sentinel/sentinel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as chokidar from 'chokidar';
import chokidar from 'chokidar';

import EventEmitter from 'events';

Expand Down
2 changes: 1 addition & 1 deletion src/typescript/language-service-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const path = require('path');

const TYPE_ERROR_MESSAGE = "Argument of type 'Element' is not assignable to parameter of type 'string'.";
const UMD_SUGGESTION_MESSAGE = " refers to a UMD global, but the current file is a module. Consider adding an import instead.'";
const UMD_SUGGESTION_MESSAGE = ' refers to a UMD global, but the current file is a module. Consider adding an import instead.';

function init() {
function create(info) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/empty-object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function emptyObject(obj: { [key: string]: any }): Record<string, never> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO get this working with 'unknown'
export default function emptyObject<T extends Record<string, any>>(obj: T): Record<string, never> {
Object.keys(obj).forEach(key => delete obj[key]);
return obj as Record<string, never>;
}
2 changes: 1 addition & 1 deletion src/utils/fs-path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from 'path';
import path from 'path';

export default function fsPath(...paths: string[]): string {
return path.join(__dirname, '..', ...paths);
Expand Down
2 changes: 2 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-console */
import fsSync from 'fs';
import { inspect } from 'util';
import fsPath from '@/utils/fs-path';

function dimText(str: string): string {
return `\x1b[2m${str}\x1b[22m`;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as nunjucks from 'nunjucks';
import nunjucks from 'nunjucks';

export default function render(path: string, values: object = {}): string {
return nunjucks.render(fsPath('..', 'views', path + '.njk'), values).trim();
Expand Down
2 changes: 0 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"module": "commonjs", // 'require' syntax makes HMR possible; 'import' doesn't
"esModuleInterop": true,
"allowUmdGlobalAccess": true, // Enable globals (albeit treated as UMD globals)
"noImplicitUseStrict": true, // Enable 'with' syntax in eval
"ignoreDeprecations": "5.0", // Enable noImplicitUseStrict
"plugins": [
{ "transform": "./src/typescript/jsx-to-html.ts", "transformProgram": true }, // Internally call jsxToHTML in ps
{ "name": "partbot-language-service-plugin" } // Language Service Plugin for a better IDE experience
Expand Down

0 comments on commit f89e613

Please sign in to comment.