refactor: more cleanup #164

Merged
mcataford merged 11 commits from more-refactor into master 2021-03-19 20:49:27 +00:00
3 changed files with 22 additions and 8 deletions
Showing only changes of commit 4eb2e87fd7 - Show all commits

View file

@ -8,6 +8,7 @@ import {
} from './utils' } from './utils'
import type { PackwatchArguments } from './index.d' import type { PackwatchArguments } from './index.d'
import { assertInPackageRoot } from './invariants' import { assertInPackageRoot } from './invariants'
import logger from './logger'
const MANIFEST_FILENAME = '.packwatch.json' const MANIFEST_FILENAME = '.packwatch.json'
@ -28,12 +29,12 @@ export default async function packwatch({
if (!existsSync(manifestPath)) { if (!existsSync(manifestPath)) {
createOrUpdateManifest({ manifestPath, current: currentStats }) 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.`, `📝 No Manifest to compare against! Current package stats written to ${MANIFEST_FILENAME}!\nPackage size (${currentStats.packageSize}) adopted as new limit.`,
) )
if (!isUpdatingManifest) { 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!', '❗ 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') throw new Error('NO_MANIFEST_NO_UPDATE')
@ -62,7 +63,7 @@ export default async function packwatch({
updateLimit: true, updateLimit: true,
manifestPath, manifestPath,
}) })
console.log( logger.log(
`📝 Updated the manifest! Package size: ${packageSize}, Limit: ${packageSize}`, `📝 Updated the manifest! Package size: ${packageSize}, Limit: ${packageSize}`,
) )
return return
@ -74,7 +75,7 @@ export default async function packwatch({
*/ */
if (hasExceededLimit) { 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!`, `🔥🔥📦🔥🔥 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') throw new Error('PACKAGE_EXCEEDS_LIMIT')
@ -87,15 +88,15 @@ export default async function packwatch({
*/ */
if (packageSizeBytes > previousSizeBytes) { if (packageSizeBytes > previousSizeBytes) {
console.log( logger.log(
`📦 👀 Your package grew! ${packageSize} > ${previousSize} (Limit: ${limit})`, `📦 👀 Your package grew! ${packageSize} > ${previousSize} (Limit: ${limit})`,
) )
} else if (packageSizeBytes < previousSizeBytes) { } else if (packageSizeBytes < previousSizeBytes) {
console.log( logger.log(
`📦 💯 Your package shrank! ${packageSize} < ${previousSize} (Limit: ${limit})`, `📦 💯 Your package shrank! ${packageSize} < ${previousSize} (Limit: ${limit})`,
) )
} else { } else {
console.log( logger.log(
`📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: ${limit})`, `📦 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 { existsSync } from 'fs'
import { join, resolve } from 'path' import { join, resolve } from 'path'
import logger from './logger'
export function assertInPackageRoot(cwd: string): void { export function assertInPackageRoot(cwd: string): void {
const packagePath = resolve(join(cwd, 'package.json')) const packagePath = resolve(join(cwd, 'package.json'))
const packageJsonExists = existsSync(packagePath) const packageJsonExists = existsSync(packagePath)
if (!packageJsonExists) { if (!packageJsonExists) {
console.log( logger.log(
'🤔 There is no package.json file here. Are you in the root directory of your project?', '🤔 There is no package.json file here. Are you in the root directory of your project?',
) )
throw new Error('NOT_IN_PACKAGE_ROOT') 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)
},
}