diff --git a/package.json b/package.json index e66b705..810b5ca 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@moduletrace/bunext", "module": "index.ts", "type": "module", - "version": "1.0.1", + "version": "1.0.2", "bin": { "bunext": "dist/index.js" }, diff --git a/src/commands/build/index.ts b/src/commands/build/index.ts index 8ebaf38..62e62f9 100644 --- a/src/commands/build/index.ts +++ b/src/commands/build/index.ts @@ -3,12 +3,14 @@ import grabConfig from "../../functions/grab-config"; import init from "../../functions/init"; import type { BunextConfig } from "../../types"; import allPagesBundler from "../../functions/bundler/all-pages-bundler"; +import { log } from "../../utils/log"; export default function () { return new Command("build") .description("Build Project") .action(async () => { - console.log(`Building Project ...`); + log.banner(); + log.build("Building Project ..."); process.env.NODE_ENV = "production"; diff --git a/src/commands/dev/index.ts b/src/commands/dev/index.ts index 952b8b6..5f7b7e1 100644 --- a/src/commands/dev/index.ts +++ b/src/commands/dev/index.ts @@ -3,12 +3,14 @@ import grabConfig from "../../functions/grab-config"; import startServer from "../../functions/server/start-server"; import init from "../../functions/init"; import type { BunextConfig } from "../../types"; +import { log } from "../../utils/log"; export default function () { return new Command("dev") .description("Run development server") .action(async () => { - console.log(`Running development server ...`); + log.banner(); + log.info("Running development server ..."); await init(); diff --git a/src/commands/start/index.ts b/src/commands/start/index.ts index 1389067..3053cf2 100644 --- a/src/commands/start/index.ts +++ b/src/commands/start/index.ts @@ -2,12 +2,14 @@ import { Command } from "commander"; import grabConfig from "../../functions/grab-config"; import startServer from "../../functions/server/start-server"; import init from "../../functions/init"; +import { log } from "../../utils/log"; export default function () { return new Command("start") .description("Start production server") .action(async () => { - console.log(`Starting production server ...`); + log.banner(); + log.info("Starting production server ..."); await init(); diff --git a/src/functions/bundler/all-pages-bundler.ts b/src/functions/bundler/all-pages-bundler.ts index 586fcfa..44d69c1 100644 --- a/src/functions/bundler/all-pages-bundler.ts +++ b/src/functions/bundler/all-pages-bundler.ts @@ -11,6 +11,7 @@ import isDevelopment from "../../utils/is-development"; import type { BundlerCTXMap } from "../../types"; import { execSync } from "child_process"; import grabConstants from "../../utils/grab-constants"; +import { log } from "../../utils/log"; const { HYDRATION_DST_DIR, PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); @@ -94,8 +95,10 @@ export default async function allPagesBundler(params?: Params) { const artifactTracker: esbuild.Plugin = { name: "artifact-tracker", setup(build) { + let buildStart = 0; + build.onStart(() => { - console.time("build"); + buildStart = performance.now(); }); build.onEnd((result) => { @@ -150,7 +153,8 @@ export default async function allPagesBundler(params?: Params) { ); } - console.timeEnd("build"); + const elapsed = (performance.now() - buildStart).toFixed(0); + log.success(`Built in ${elapsed}ms`); if (params?.exit_after_first_build) { process.exit(); diff --git a/src/functions/server/rebuild-bundler.tsx b/src/functions/server/rebuild-bundler.tsx index 742531b..f58f6b9 100644 --- a/src/functions/server/rebuild-bundler.tsx +++ b/src/functions/server/rebuild-bundler.tsx @@ -1,5 +1,6 @@ import allPagesBundler from "../bundler/all-pages-bundler"; import serverPostBuildFn from "./server-post-build-fn"; +import { log } from "../../utils/log"; export default async function rebuildBundler() { try { @@ -13,6 +14,6 @@ export default async function rebuildBundler() { post_build_fn: serverPostBuildFn, }); } catch (error: any) { - console.error(error); + log.error(error); } } diff --git a/src/functions/server/start-server.ts b/src/functions/server/start-server.ts index b58ec93..99d0752 100644 --- a/src/functions/server/start-server.ts +++ b/src/functions/server/start-server.ts @@ -1,5 +1,6 @@ import _ from "lodash"; import AppNames from "../../utils/grab-app-names"; +import { log } from "../../utils/log"; import allPagesBundler from "../bundler/all-pages-bundler"; import serverParamsGen from "./server-params-gen"; import watcher from "./watcher"; @@ -32,7 +33,7 @@ export default async function startServer(params?: Params) { readFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, "utf-8"), ) as BundlerCTXMap[] | undefined; if (!artifacts?.[0]) { - console.error(`Please build first.`); + log.error("Please build first."); process.exit(1); } global.BUNDLER_CTX_MAP = artifacts; @@ -45,7 +46,7 @@ export default async function startServer(params?: Params) { while (!global.IS_FIRST_BUNDLE_READY) { if (bundle_ready_retries > MAX_BUNDLE_READY_RETRIES) { - console.error(`Couldn't grab first bundle for dev environment`); + log.error("Couldn't grab first bundle for dev environment"); process.exit(1); } bundle_ready_retries++; @@ -56,9 +57,7 @@ export default async function startServer(params?: Params) { global.SERVER = server; - console.log( - `${name} Server Running on http://localhost:${server.port} ...`, - ); + log.server(`http://localhost:${server.port}`); return server; } diff --git a/src/functions/server/watcher.tsx b/src/functions/server/watcher.tsx index d072b12..64bcc39 100644 --- a/src/functions/server/watcher.tsx +++ b/src/functions/server/watcher.tsx @@ -2,6 +2,7 @@ import { watch, existsSync } from "fs"; import path from "path"; import grabDirNames from "../../utils/grab-dir-names"; import rebuildBundler from "./rebuild-bundler"; +import { log } from "../../utils/log"; const { SRC_DIR } = grabDirNames(); @@ -25,11 +26,11 @@ export default function watcher() { try { global.RECOMPILING = true; - console.log(`Page ${action}: ${filename}. Rebuilding ...`); + log.watch(`Page ${action}: ${filename}. Rebuilding ...`); await rebuildBundler(); } catch (error: any) { - console.error(error); + log.error(error); } finally { global.RECOMPILING = false; } diff --git a/src/utils/exit-with-error.ts b/src/utils/exit-with-error.ts index b7ac9d9..b7e397e 100644 --- a/src/utils/exit-with-error.ts +++ b/src/utils/exit-with-error.ts @@ -1,4 +1,6 @@ +import { log } from "./log"; + export default function exitWithError(msg: string, code?: number) { - console.error(msg); + log.error(msg); process.exit(code || 1); } diff --git a/src/utils/grab-app-names.ts b/src/utils/grab-app-names.ts index 2d5317f..8ae6a7c 100644 --- a/src/utils/grab-app-names.ts +++ b/src/utils/grab-app-names.ts @@ -2,6 +2,7 @@ const AppNames = { defaultPort: 7000, defaultAssetPrefix: "_bunext/static", name: "Bunext", + version: "1.0.1", defaultDistDir: ".bunext", RootPagesComponentName: "__root", } as const; diff --git a/src/utils/log.ts b/src/utils/log.ts new file mode 100644 index 0000000..7fafec2 --- /dev/null +++ b/src/utils/log.ts @@ -0,0 +1,34 @@ +import chalk from "chalk"; +import AppNames from "./grab-app-names"; + +const prefix = { + info: chalk.cyan.bold("ℹ"), + success: chalk.green.bold("✓"), + error: chalk.red.bold("✗"), + warn: chalk.yellow.bold("⚠"), + build: chalk.magenta.bold("⚙"), + watch: chalk.blue.bold("◉"), +}; + +export const log = { + info: (msg: string) => + console.log(`${prefix.info} ${chalk.white(msg)}`), + success: (msg: string) => + console.log(`${prefix.success} ${chalk.green(msg)}`), + error: (msg: string | Error) => + console.error(`${prefix.error} ${chalk.red(String(msg))}`), + warn: (msg: string) => + console.warn(`${prefix.warn} ${chalk.yellow(msg)}`), + build: (msg: string) => + console.log(`${prefix.build} ${chalk.magenta(msg)}`), + watch: (msg: string) => + console.log(`${prefix.watch} ${chalk.blue(msg)}`), + server: (url: string) => + console.log( + `${prefix.success} ${chalk.white("Server running on")} ${chalk.cyan.underline(url)}`, + ), + banner: () => + console.log( + `\n ${chalk.cyan.bold(AppNames.name)} ${chalk.gray(`v${AppNames.version}`)}\n`, + ), +};