2021-02-19 04:31:33 +00:00
import { existsSync } from 'fs'
import { join , resolve } from 'path'
2020-12-13 00:18:25 +00:00
2021-02-19 04:31:33 +00:00
import {
createOrUpdateManifest ,
getCurrentPackageStats ,
getPreviousPackageStats ,
2021-06-05 15:29:15 +00:00
mergeDefaultArguments ,
2021-02-19 04:31:33 +00:00
} from './utils'
2021-06-05 15:29:15 +00:00
import type { PackwatchArguments } from './types'
2021-03-19 20:49:27 +00:00
import { assertInPackageRoot } from './invariants'
import logger from './logger'
2020-12-13 00:18:25 +00:00
const MANIFEST_FILENAME = '.packwatch.json'
2021-06-05 15:29:15 +00:00
export default async function packwatch (
args : Partial < PackwatchArguments > ,
) : Promise < void > {
const { cwd , isUpdatingManifest } = mergeDefaultArguments ( args )
2021-02-19 04:31:33 +00:00
const manifestPath = resolve ( join ( cwd , MANIFEST_FILENAME ) )
2020-12-13 00:18:25 +00:00
2021-03-19 20:49:27 +00:00
assertInPackageRoot ( cwd )
2020-12-13 00:18:25 +00:00
2021-02-19 04:31:33 +00:00
const currentStats = getCurrentPackageStats ( cwd )
2020-12-13 00:18:25 +00:00
/ *
* If there is no manifest file yet , we can use the current package stats as
* a base to build one . The current package size becomes the limit .
* /
2021-02-19 04:31:33 +00:00
if ( ! existsSync ( manifestPath ) ) {
createOrUpdateManifest ( { manifestPath , current : currentStats } )
2021-03-19 20:49:27 +00:00
logger . warn (
2020-12-13 00:18:25 +00:00
` 📝 No Manifest to compare against! Current package stats written to ${ MANIFEST_FILENAME } ! \ nPackage size ( ${ currentStats . packageSize } ) adopted as new limit. ` ,
)
if ( ! isUpdatingManifest ) {
2021-03-19 20:49:27 +00:00
logger . error (
2020-12-13 00:18:25 +00:00
'❗ 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!' ,
)
2021-03-19 20:49:27 +00:00
throw new Error ( 'NO_MANIFEST_NO_UPDATE' )
2020-12-13 00:18:25 +00:00
}
2021-03-19 20:49:27 +00:00
return
2020-12-13 00:18:25 +00:00
}
2021-02-19 04:31:33 +00:00
const previousStats = getPreviousPackageStats ( cwd )
2020-12-13 00:18:25 +00:00
const { packageSizeBytes , packageSize } = currentStats
const {
packageSize : previousSize ,
packageSizeBytes : previousSizeBytes ,
limit ,
limitBytes ,
} = previousStats
2021-06-05 15:29:15 +00:00
const hasExceededLimit = limitBytes && packageSizeBytes > limitBytes
2020-12-13 00:18:25 +00:00
/ *
* If we are updating the manifest , we can write right away and terminate .
* /
if ( isUpdatingManifest ) {
createOrUpdateManifest ( {
previous : previousStats ,
current : currentStats ,
updateLimit : true ,
2021-02-19 04:31:33 +00:00
manifestPath ,
2020-12-13 00:18:25 +00:00
} )
2021-03-19 20:49:27 +00:00
logger . log (
2020-12-13 00:18:25 +00:00
` 📝 Updated the manifest! Package size: ${ packageSize } , Limit: ${ packageSize } ` ,
)
2021-03-19 20:49:27 +00:00
return
2020-12-13 00:18:25 +00:00
}
/ *
* If there is a manifest file and the current package busts its limit
* we signal it and terminate with an error .
* /
if ( hasExceededLimit ) {
2021-03-19 20:49:27 +00:00
logger . error (
2020-12-13 00:18:25 +00:00
` 🔥🔥📦🔥🔥 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! ` ,
)
2021-03-19 20:49:27 +00:00
throw new Error ( 'PACKAGE_EXCEEDS_LIMIT' )
2020-12-13 00:18:25 +00:00
}
/ *
* If there is a manifest file and the limit is not busted , we give
* the user some feedback on how the current package compares with
* the previous one .
* /
if ( packageSizeBytes > previousSizeBytes ) {
2021-03-19 20:49:27 +00:00
logger . log (
2020-12-13 00:18:25 +00:00
` 📦 👀 Your package grew! ${ packageSize } > ${ previousSize } (Limit: ${ limit } ) ` ,
)
} else if ( packageSizeBytes < previousSizeBytes ) {
2021-03-19 20:49:27 +00:00
logger . log (
2020-12-13 00:18:25 +00:00
` 📦 💯 Your package shrank! ${ packageSize } < ${ previousSize } (Limit: ${ limit } ) ` ,
)
} else {
2021-03-19 20:49:27 +00:00
logger . log (
2020-12-13 00:18:25 +00:00
` 📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: ${ limit } ) ` ,
)
}
2021-03-19 20:49:27 +00:00
return
2020-12-13 00:18:25 +00:00
}