diff --git a/src/commands/build.ts b/src/commands/build.ts index e99f208..c7a2244 100644 --- a/src/commands/build.ts +++ b/src/commands/build.ts @@ -7,10 +7,12 @@ import assert from 'node:assert'; import defaultEsbuild from '../utilities/defaultEsbuildConfig'; import { require } from '../utilities/require'; import { pathExists, pathExistsSync } from 'find-up'; -import { mkdir, writeFile } from 'fs/promises'; +import { mkdir, writeFile, readFile } from 'fs/promises'; import * as Preprocessor from '../utilities/preprocessor'; import { bold, magentaBright } from 'colorette'; -const validExtensions = ['.ts', '.js' ]; + +const VALID_EXTENSIONS = ['.ts', '.js' ]; + type BuildOptions = { /** * Define __VERSION__ @@ -42,13 +44,13 @@ type BuildOptions = { */ env?: string; }; + const CommandHandlerPlugin = (buildConfig: Partial, ambientFilePath: string, sernTsConfigPath: string) => { return { name: "commandhandler", setup(build) { const options = build.initialOptions - const defVersion = () => JSON.stringify(require(p.resolve('package.json')).version); options.define = { ...buildConfig.define ?? {}, @@ -64,9 +66,9 @@ const CommandHandlerPlugin = (buildConfig: Partial, ambientFilePat } const resolveBuildConfig = (path: string|undefined, language: string) => { if(language === 'javascript') { - return path ?? p.resolve('jsconfig.json') + return path ?? 'jsconfig.json' } - return path ?? p.resolve('tsconfig.json') + return path ?? 'tsconfig.json' } export async function build(options: Record) { @@ -74,7 +76,7 @@ export async function build(options: Record) { console.info(`${magentaBright('EXPERIMENTAL')}: This API has not been stabilized. add -W or --suppress-warnings flag to suppress`); } const sernConfig = await getConfig(); - let buildConfig: Partial = {}; + let buildConfig: BuildOptions; const buildConfigPath = p.resolve(options.project ?? 'sern.build.js'); const defaultBuildConfig = { @@ -83,7 +85,8 @@ export async function build(options: Record) { mode: options.mode ?? 'development', dropLabels: [], tsconfig: resolveBuildConfig(options.tsconfig, sernConfig.language), - env: options.env ?? p.resolve('.env'), + env: options.env ?? '.env', + include: [] }; if (pathExistsSync(buildConfigPath)) { //throwable, buildConfigPath may not exist @@ -100,7 +103,7 @@ export async function build(options: Record) { } assert(buildConfig.mode === 'development' || buildConfig.mode === 'production', 'Mode is not `production` or `development`'); try { - let config = require(buildConfig.tsconfig!); + let config = JSON.parse(await readFile(buildConfig.tsconfig!, 'utf8')); config.extends && console.warn("Extend the generated tsconfig") } catch(e) { console.error("no tsconfig / jsconfig found"); @@ -126,12 +129,11 @@ export async function build(options: Record) { await mkdir(genDir, { recursive: true }); } - const entryPoints = await glob(`./src/**/*{${validExtensions.join(',')}}`); -// const [commandsPaths, eventsPaths] = await Promise.all([ -// glob(`**/*`, { withFileTypes: true, ignore: { ignored: p => p.isDirectory() }, cwd: "./src/commands/" }), -// glob(`**/*`, { withFileTypes: true, ignore: { ignored: p => p.isDirectory() }, cwd: "./src/events/" }) -// ]) - + const entryPoints = await glob(`src/**/*{${VALID_EXTENSIONS.join(',')}}`,{ + ignore: { + ignored: (p) => p.name.endsWith('.d.ts'), + } + }); //https://esbuild.github.io/content-types/#tsconfig-json const ctx = await esbuild.context({ entryPoints, diff --git a/src/create-publish.mts b/src/create-publish.mts index 484094a..96f8ebd 100644 --- a/src/create-publish.mts +++ b/src/create-publish.mts @@ -2,65 +2,38 @@ * This file is meant to be run with the esm / cjs esbuild-kit loader to properly import typescript modules */ -import { readdir, stat, mkdir, writeFile } from 'fs/promises'; -import { join, basename, extname, resolve } from 'node:path'; +import { readdir, mkdir, writeFile } from 'fs/promises'; +import { basename, resolve, posix as pathpsx } from 'node:path'; import { pathExistsSync } from 'find-up'; import assert from 'assert'; import { once } from 'node:events'; import * as Rest from './rest'; -import type { sernConfig } from './utilities/getConfig'; +import type { SernConfig } from './utilities/getConfig'; import type { PublishableData, PublishableModule, Typeable } from './create-publish.d.ts'; import { cyanBright, greenBright, redBright } from 'colorette'; import { inspect } from 'node:util' import ora from 'ora'; -async function deriveFileInfo(dir: string, file: string) { - const fullPath = join(dir, file); - return { - fullPath, - fileStats: await stat(fullPath), - base: basename(file), - }; -} - -function isSkippable(filename: string) { - // empty string is for non extension files (directories) - const validExtensions = ['.js', '.cjs', '.mts', '.mjs', '.cts', '.ts', '']; - return filename[0] === '!' || !validExtensions.includes(extname(filename)); -} async function* readPaths(dir: string, shouldDebug: boolean): AsyncGenerator { - try { - const files = await readdir(dir); - for (const file of files) { - const { fullPath, fileStats, base } = await deriveFileInfo(dir, file); - - if (fileStats.isDirectory()) { - // TODO: refactor so that i dont repeat myself for files (line 71) - if (isSkippable(base)) { - if (shouldDebug) console.info(`ignored directory: ${fullPath}`); - } else { - yield* readPaths(fullPath, shouldDebug); - } - } else { - if (isSkippable(base)) { - if (shouldDebug) console.info(`ignored: ${fullPath}`); - } else { - yield 'file:///' + fullPath; - } + const files = await readdir(dir, { withFileTypes: true }); + for (const file of files) { + const fullPath = pathpsx.join(dir, file.name); + if (file.isDirectory()) { + if (!file.name.startsWith('!')) { + yield* readPaths(fullPath, shouldDebug); } + } else if (!file.name.startsWith('!')) { + yield "file:///"+resolve(fullPath); } - } catch (err) { - throw err; } } // recieved sern config const [{ config, preloads, commandDir }] = await once(process, 'message'), - { paths } = config as sernConfig; + { paths } = config as SernConfig; for (const preload of preloads) { - console.log("preloading: ", preload); await import('file:///' + resolve(preload)); } @@ -83,17 +56,16 @@ for await (const absPath of filePaths) { config = config(absPath, commandModule); } - try { - commandModule = commandModule.getInstance(); - } catch {} - if ((PUBLISHABLE & commandModule.type) != 0) { // assign defaults const filename = basename(absPath); const filenameNoExtension = filename.substring(0, filename.lastIndexOf('.')); commandModule.name ??= filenameNoExtension; commandModule.description ??= ''; - commandModule.absPath = absPath; + commandModule.meta = { + absPath + } + modules.push({ commandModule, config }); } } @@ -151,7 +123,7 @@ const makePublishData = ({ commandModule, config }: Record>(); for (const [guildId, array] of guildCommandMap.entries()) {