Skip to content

Commit

Permalink
🐛 Fix: Build ESM to CJS
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed Jun 11, 2024
1 parent 1c41da6 commit 3711159
Show file tree
Hide file tree
Showing 21 changed files with 2,865 additions and 669 deletions.
469 changes: 64 additions & 405 deletions core/package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
]
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
"@types/crypto-js": "^4.2.2",
"@types/express": "^4.17.21",
"@types/marked-terminal": "^6.1.1",
Expand All @@ -42,7 +41,7 @@
},
"dependencies": {
"@types/cron": "^2.4.0",
"bcrypt": "^5.1.1",
"argon2": "^0.40.3",
"check-password-strength": "^2.0.10",
"chokidar": "^3.6.0",
"cron": "^3.1.7",
Expand Down
6 changes: 3 additions & 3 deletions core/src/controller/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { RootPATH } from '@/index.js'
import { metadata, RootPATH } from '@/index.js'
import { AccessToken, AuthData, BotInfo, User } from '@/interfaces/auth.js'
import { DataCrypted } from '@/interfaces/crypt.js'
import { CronJob } from 'cron'
import { rm } from 'fs/promises'
import prompts, { Choice, PromptObject } from 'prompts'
import { api } from '../../package.json'
import { credentials, Crypt } from './crypt.js'
import fetch from 'node-fetch'

const emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/g
const crypt = new Crypt()
Expand Down Expand Up @@ -79,6 +77,7 @@ export class Auth {

async login(): Promise<User> {
await this.timeout()
const { api } = await metadata()

const response = await fetch(`${api}/auth/login`, {
method: 'POST',
Expand Down Expand Up @@ -132,6 +131,7 @@ export class Auth {

async validator() {
await this.timeout()
const { api } = await metadata()
if (
this.accessToken === undefined ||
Auth.user === undefined
Expand Down
32 changes: 16 additions & 16 deletions core/src/controller/crypt.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { exists } from "@/functions/fs-extra.js";
import { isJson } from "@/functions/validate.js";
import { RootPATH } from "@/index.js";
import { DataCrypted } from "@/interfaces/crypt.js";
import { compare, hash } from 'bcrypt';
import * as argon2 from "argon2";
import { passwordStrength } from 'check-password-strength';
import { watch } from 'chokidar';
import { randomBytes } from 'crypto';
import CryptoJS from 'crypto-js';
import prompts from "prompts";
import { RootPATH } from "@/index.js";
import { existsSync } from "fs";
import { readFile, rm, writeFile } from "fs/promises";
import prompts from "prompts";

export const credentials = new Map<string, string | object | boolean | number>()

export class Crypt {
constructor() {}

async checker () {
if (!existsSync(`${RootPATH}/.key`) && process.env?.Token === undefined) await this.create()

if (existsSync(`${RootPATH}/.key`) && existsSync(`${RootPATH}/.hash`)) {

}
if (!(await exists(`${RootPATH}/.key`)) && process.env?.Token === undefined) await this.create()

for (const path of ['.key', '.hash']) {
const wather = watch(path, { cwd: RootPATH })
Expand Down Expand Up @@ -90,22 +86,26 @@ export class Crypt {
const data = await readFile(`${RootPATH}/.key`, { encoding: 'utf-8' }).catch(() => '')
const dataHash = await readFile(`${RootPATH}/.hash`, { encoding: 'utf-8' }).catch(() => '')

const hash = await compare(data, dataHash)
if (!hash) {
const invalid = async () => {
await this.delete()
throw new Error('Hash invalido! deletando arquivos encriptados!')
}

const hash = await argon2.verify(dataHash, data).catch(async () => await invalid())
if (!hash) await invalid()
}

async delete () {
if (existsSync(`${RootPATH}/.key`)) await rm(`${RootPATH}/.key`)
if (existsSync(`${RootPATH}/.hash`)) await rm(`${RootPATH}/.hash`)
if (existsSync(`${RootPATH}/.env`)) await rm(`${RootPATH}/.env`)
if (await exists(`${RootPATH}/.key`)) await rm(`${RootPATH}/.key`)
if (await exists(`${RootPATH}/.hash`)) await rm(`${RootPATH}/.hash`)
if (await exists(`${RootPATH}/.env`)) await rm(`${RootPATH}/.env`)
}

async read (): Promise<DataCrypted | {}> {
const token = this.getToken()
if (!existsSync(`${RootPATH}/.key`)) return {}
const existKey = await exists(`${RootPATH}/.key`)
if (!existKey) return {}

await this.validate()
console.log('⚠️ Informações sensíveis sendo lidas...')
const data = await readFile(`${RootPATH}/.key`, { encoding: 'utf-8' }).catch(() => '')
Expand Down Expand Up @@ -138,7 +138,7 @@ export class Crypt {
const AESCrypt = CryptoJS.AES.encrypt(JSON.stringify(data), token).toString()
const BlowfishCrypt = CryptoJS.Blowfish.encrypt(AESCrypt, token).toString()
const TripleDESCrypt = CryptoJS.TripleDES.encrypt(BlowfishCrypt, token).toString()
const hashCrypt = await hash(TripleDESCrypt, 10)
const hashCrypt = await argon2.hash(TripleDESCrypt)

await writeFile(`${RootPATH}/.key`, TripleDESCrypt)
await writeFile(`${RootPATH}/.hash`, hashCrypt)
Expand Down
4 changes: 3 additions & 1 deletion core/src/controller/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import TerminalRenderer from 'marked-terminal'
import { watch } from 'chokidar'
import { existsSync } from "fs";
import { readFile, writeFile } from "fs/promises";
import { join } from "path";
import { __dirname } from '@/index.js'

let watched = false

Expand All @@ -25,7 +27,7 @@ export class License {
await this.ask()
}
async ask () {
const data = await readFile(`${RootPATH}/LICENSE.md`, { encoding: 'utf-8' })
const data = await readFile(join(__dirname, '../LICENSE.md'), { encoding: 'utf-8' })
marked.setOptions({
renderer: new TerminalRenderer() as Renderer
})
Expand Down
7 changes: 4 additions & 3 deletions core/src/controller/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,11 @@ export class Plugins {
let match;
while ((match = regex.exec(code)) !== null) {
const content = match[1];
console.log(content)
if (content) {
console.log(`🔄 Convertendo ${fileName}`);
const replacedPath = `${join(__dirname, '../../')}node_modules/${content}/index.js`;
code = code.replace(new RegExp(content, 'g'), replacedPath);
const replacedPath = `"${join(__dirname, '../../')}node_modules/${content}"`;
const genRegex = new RegExp(`"${content}"`, 'g')
code = code.replace(genRegex, replacedPath);
}
}

Expand Down
10 changes: 10 additions & 0 deletions core/src/functions/fs-extra.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { stat } from "fs/promises"

export async function exists(path: string): Promise<boolean> {
try {
await stat(path)
return true
} catch {
return false
}
}
2 changes: 1 addition & 1 deletion core/src/functions/port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function generatePort () {

server.listen(port, 'localhost')
})
console.log(port)

if (!result) return await generatePort()
return port
}
2 changes: 1 addition & 1 deletion core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFile } from 'fs/promises'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'

const __dirname = dirname(fileURLToPath(import.meta.url))
export const __dirname = dirname(fileURLToPath(import.meta.url))

export const PKG_MODE = `${process.cwd()}/src` !== __dirname
export const RootPATH: string = PKG_MODE ? path.join(process.cwd()) : path.join(__dirname, '..')
Expand Down
14 changes: 7 additions & 7 deletions core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

Expand All @@ -13,18 +18,13 @@
"noUnusedParameters": true,
"noImplicitReturns": true,

"lib": ["ESNext"],
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",

"outDir": "dist",
"outDir": "./build/esm",
"rootDir": "./",
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
},
},
"include": ["src", "../tailwind.config.ts", "entries"],
"include": ["src", "../tailwind.config.ts", "entries", "build.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 3711159

Please sign in to comment.