refactor: extract logging

This commit is contained in:
Marc Cataford 2021-02-19 10:11:41 -05:00
parent a5081e3db6
commit 4eb2e87fd7
3 changed files with 22 additions and 8 deletions

View file

@ -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})`,
)
}

View file

@ -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')

11
src/logger.ts Normal file
View file

@ -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)
},
}