Skip to content

Commit

Permalink
refactor: moved from jest to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
USERSATOSHI committed Oct 15, 2024
1 parent a5600bb commit 142bbd5
Show file tree
Hide file tree
Showing 57 changed files with 1,583 additions and 2,570 deletions.
13 changes: 10 additions & 3 deletions bin/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env node

process.removeAllListeners('warning');
#!/usr/bin/env -S node --no-warnings=ExperimentalWarning

import { program } from 'commander';
import test from './test.mjs';
Expand All @@ -12,11 +10,13 @@ import pkg from './package.json' assert { type: 'json' };
import chalk from 'chalk';
import add from './add.mjs';
import addLicense from './addLicense.mjs';
import run from './run.mjs';

program
.command('test')
.description('run all tests for the given library')
.requiredOption('-l, --library <library>', 'the library to test')
.option('-f, --folder <folder>', 'the folder to test in the library')
.action(test);

program
Expand Down Expand Up @@ -56,6 +56,13 @@ program
.requiredOption('-l, --library <library>', 'the library to license')
.action(addLicense);

program
.command('run')
.description('run a file for the given library')
.requiredOption('-l, --library <library>', 'the library to run')
.requiredOption('-f, --file <file>', 'the file to run')
.action(run);

program
.name(pkg.name)
.version(pkg.version)
Expand Down
45 changes: 45 additions & 0 deletions bin/run.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from 'node:path';
import { spawn } from 'node:child_process';

/**
* Run given path for the given library
* @param {object} options - The options object
* @param {string} options.library - The library to test
* @param {string} options.path - The path to the file that needs to be run
* @returns {Promise<void>}
*/
const run = async ({ library, file: filePath }) => {
console.log(`Running ${filePath} from ${library}`);

// Resolve paths in a cross-platform way
const mainFolder = path.join(process.cwd(), 'lib', library);

const spwn = spawn(
`node ${filePath}`,
[],
{
stdio: 'inherit',
// Set cwd to the project root
cwd: mainFolder,
shell: true,
},
);

spwn.on('exit', (code) => {
if (code !== 0) {
console.error(`Failed to run ${filePath} from ${library}`);
process.exit(1);
}
});

spwn.on('error', (error) => {
console.error(error);
process.exit(1);
});

spwn.on('close', () => {
console.log(`finished running ${filePath} from ${library}`);
});
};

export default run;
39 changes: 24 additions & 15 deletions bin/test.mjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import path from 'node:path';
import { spawn } from 'node:child_process';

import { pathToFileURL } from 'node:url';
import chalk from 'chalk';
/**
* Run all tests for the given library
* @param {object} options - The options object
* @param {string} options.library - The library to test
* @returns {Promise<void>}
*/
const test = async ({ library }) => {
const test = async ({ library, folder, reporter }) => {
const start = performance.now();
console.log(`Running tests for ${library}`);

// Resolve paths in a cross-platform way
const mainFolder = path.join(process.cwd(), 'lib', library);

const reporterPath = pathToFileURL(
path.join(process.cwd(), 'tools', 'testing', 'testReporter.mjs'),
);
const flags = folder ? `--folder=${folder}` : '';
const runnerPath = path.join(
process.cwd(),
'tools',
'testing',
'testRunner.mjs',
);
// "glob -c \"tsx --test\" \"./src/**/*.test.ts\""
const spwn = spawn(
'npx',
[
'jest',
'--verbose',
'--color',
'--coverage',
'--coverageProvider=v8',
`--config=${path.join(process.cwd(), 'jest.config.js')}`,
// Specify the folder where Jest should look for tests
`${mainFolder}`,
],
// `npx glob -c "tsx --test-reporter="${reporterPath.toString()}" --test" "./src/**/*.test.ts"`,
`node --import tsx "${runnerPath.toString()}" -- ${flags}`,
[],
{
stdio: 'inherit',
// Set cwd to the project root
cwd: mainFolder,
cwd: `${mainFolder}`,
shell: true,
},
);
Expand All @@ -47,6 +51,11 @@ const test = async ({ library }) => {

spwn.on('close', () => {
console.log(`Tested ${library}`);
console.log(
chalk.gray(
`Duration: ${((performance.now() - start) / 1000).toFixed(2)}s`,
),
);
});
};

Expand Down
30 changes: 0 additions & 30 deletions jest.config.js

This file was deleted.

7 changes: 0 additions & 7 deletions jsconfig.json

This file was deleted.

2 changes: 1 addition & 1 deletion lib/aoi.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"type": "commonjs",
"type": "module",
"exports": {
".": {
"import": "./dist/esm/index.js",
Expand Down
54 changes: 36 additions & 18 deletions lib/aoi.js/src/classes/AoiClient.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import Transpiler from '@aoi.js/core/Transpiler.js';
import { CommandManager } from '@aoi.js/managers/Command.js';
import FunctionManager from '@aoi.js/managers/Function.js';
import { type IAoiClientOptions } from '@aoi.js/typings/interface.js';
import { type AoiClientProps } from '@aoi.js/typings/type.js';
import { Client, DefaultWebSocketManagerOptions, Partials, type ClientOptions } from 'discord.js';
import {
type ICommandOptions,
type IAoiClientOptions,
} from '@aoi.js/typings/interface.js';
import {
type Optional,
type CommandTypes,
} from '@aoi.js/typings/type.js';
import { Client, Partials, type ClientOptions } from 'discord.js';
import * as Events from '@aoi.js/events/index.js';

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
interface AoiClient extends AoiClientProps {
client: Client;
}


// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class AoiClient {
client!: Client;

transpiler: Transpiler;
database = null;
managers!: {
Expand All @@ -24,11 +24,10 @@ class AoiClient {

readonly #options: IAoiClientOptions;


constructor(options: IAoiClientOptions) {
this.#validateOptions(options);
this.#options = options;

const transpilerOptions = {
minify: options.transpilerOptions?.minify ?? true,
customFunctions: {},
Expand All @@ -39,10 +38,12 @@ class AoiClient {
commands: new CommandManager(this),
functions: new FunctionManager(this),
};

if (options.testMode) return;

const djsOptions: ClientOptions = options.djsClientOptions ?? { intents: 0 };
const djsOptions: ClientOptions = options.djsClientOptions ?? {
intents: 0,
};

djsOptions.partials ||= [
Partials.GuildMember,
Partials.Channel,
Expand All @@ -53,17 +54,28 @@ class AoiClient {
Partials.ThreadMember,
];
djsOptions.intents = options.intents;

this.client = new Client(djsOptions);
this.#bindEvents();
}

async start() {
await this.client.login(this.#options.token);
}

command(data: Optional<ICommandOptions, '__path__' | 'type'> ) {
if (!data.type) data.type = 'basic' as CommandTypes;
data.__path__ = data.__path__ ?? 'root';

this.managers.commands.add(data as ICommandOptions);
return this;
}

#validateOptions(options: IAoiClientOptions) {
if (options.intents === undefined) {
throw new SyntaxError('Intents not provided, "Guilds" intent is required');
throw new SyntaxError(
'Intents not provided, "Guilds" intent is required',
);
}

if (isNaN(options.intents)) {
Expand All @@ -79,6 +91,12 @@ class AoiClient {
}
}

#bindEvents() {
this.#options.events.forEach((event) => {
Events[event]?.(this);
});
}

get prefix() {
return this.#options.prefix;
}
Expand All @@ -88,4 +106,4 @@ class AoiClient {
}
}

export default AoiClient;
export default AoiClient;
19 changes: 8 additions & 11 deletions lib/aoi.js/src/classes/Command.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { type AsyncFunction, type CommandTypes } from '@aoi.js/typings/type.js';
import type AoiClient from './AoiClient';
import type AoiClient from './AoiClient.js';
import { type ICommandOptions } from '@aoi.js/typings/interface.js';
import { type Snowflake } from 'discord.js';
import { escapeResult } from '@aoi.js/utils/Helpers/core.js';

export default class Command {
[key: string]: unknown;
name!: string;
type!: CommandTypes;
code!: string | (() => Promise<void>);
code!: string | AsyncFunction;
aliases?: string[];
channel?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -45,7 +46,7 @@ export default class Command {

if (this.code instanceof Function) this.__compiled__ = this.code;
else {
let channelId: Snowflake | undefined | AsyncFunction;
let channelId: Snowflake | undefined;
if (this.channel) {
if (
typeof this.channel === 'string' &&
Expand All @@ -56,23 +57,19 @@ export default class Command {
scopeData: {
name: 'GLOBAL_CHANNEL',
},
}).func;
asFunction: false,
}).result;
} else channelId = this.channel;
}

const func = transpiler.transpile(this.code, {
sendMessage: true,
reverse: this.reverseRead,
scopeData: {
functions:
typeof channelId === 'function'
? [channelId.toString()]
: undefined,
useChannel:
typeof channelId === 'function' ? `${channelId.name}()` : channelId,
useChannel: channelId?.includes('__$DISCORD_DATA$__') ? escapeResult(channelId) : channelId,
},
});
this.__compiled__ = func.func;
this.__compiled__ = func.func!;
}
}
}
Loading

0 comments on commit 142bbd5

Please sign in to comment.