From 4eb2e87fd71af4cd17d477a45da88717941a4f7d Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 19 Feb 2021 10:11:41 -0500 Subject: [PATCH] refactor: extract logging --- src/index.ts | 15 ++++++++------- src/invariants.ts | 4 +++- src/logger.ts | 11 +++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 src/logger.ts diff --git a/src/index.ts b/src/index.ts index 670f70c..5e6c827 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { } from './utils' import type { PackwatchArguments } from './index.d' import { assertInPackageRoot } from './invariants' +import logger from './logger' const MANIFEST_FILENAME = '.packwatch.json' @@ -28,12 +29,12 @@ export default async function packwatch({ if (!existsSync(manifestPath)) { createOrUpdateManifest({ manifestPath, current: currentStats }) - console.log( + logger.log( `📝 No Manifest to compare against! Current package stats written to ${MANIFEST_FILENAME}!\nPackage size (${currentStats.packageSize}) adopted as new limit.`, ) if (!isUpdatingManifest) { - console.log( + logger.log( '❗ It looks like you ran PackWatch without a manifest. To prevent accidental passes in CI or hooks, packwatch will terminate with an error. If you are running packwatch for the first time in your project, this is expected!', ) throw new Error('NO_MANIFEST_NO_UPDATE') @@ -62,7 +63,7 @@ export default async function packwatch({ updateLimit: true, manifestPath, }) - console.log( + logger.log( `📝 Updated the manifest! Package size: ${packageSize}, Limit: ${packageSize}`, ) return @@ -74,7 +75,7 @@ export default async function packwatch({ */ if (hasExceededLimit) { - console.log( + logger.log( `🔥🔥📦🔥🔥 Your package exceeds the limit set in ${MANIFEST_FILENAME}! ${packageSize} > ${limit}\nEither update the limit by using the --update-manifest flag or trim down your packed files!`, ) throw new Error('PACKAGE_EXCEEDS_LIMIT') @@ -87,15 +88,15 @@ export default async function packwatch({ */ if (packageSizeBytes > previousSizeBytes) { - console.log( + logger.log( `📦 👀 Your package grew! ${packageSize} > ${previousSize} (Limit: ${limit})`, ) } else if (packageSizeBytes < previousSizeBytes) { - console.log( + logger.log( `📦 💯 Your package shrank! ${packageSize} < ${previousSize} (Limit: ${limit})`, ) } else { - console.log( + logger.log( `📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: ${limit})`, ) } diff --git a/src/invariants.ts b/src/invariants.ts index ed8839e..01309aa 100644 --- a/src/invariants.ts +++ b/src/invariants.ts @@ -1,12 +1,14 @@ import { existsSync } from 'fs' import { join, resolve } from 'path' +import logger from './logger' + export function assertInPackageRoot(cwd: string): void { const packagePath = resolve(join(cwd, 'package.json')) const packageJsonExists = existsSync(packagePath) if (!packageJsonExists) { - console.log( + logger.log( '🤔 There is no package.json file here. Are you in the root directory of your project?', ) throw new Error('NOT_IN_PACKAGE_ROOT') diff --git a/src/logger.ts b/src/logger.ts new file mode 100644 index 0000000..9e7eaa8 --- /dev/null +++ b/src/logger.ts @@ -0,0 +1,11 @@ +export default { + log: (...args: unknown[]): void => { + console.log(...args) + }, + warn: (...args: unknown[]): void => { + console.warn(...args) + }, + error: (...args: unknown[]): void => { + console.error(...args) + }, +}