From 696d91a5f94ad1c3e0cfa3d106664bdb6699fbea Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Thu, 18 Feb 2021 23:28:12 -0500 Subject: [PATCH] refactor: extract utils, hoist cwd up --- src/cli.ts | 3 +- src/index.ts | 89 ++++------------------------------------------------ src/utils.ts | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 84 deletions(-) create mode 100644 src/utils.ts diff --git a/src/cli.ts b/src/cli.ts index 6742f26..a13de2f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -3,5 +3,6 @@ import runPackwatch from '.' const isUpdatingManifest = process.argv.includes('--update-manifest') -const processExit = runPackwatch({ isUpdatingManifest }) +const cwd = process.cwd() +const processExit = runPackwatch({ cwd, isUpdatingManifest }) process.exit(processExit) diff --git a/src/index.ts b/src/index.ts index dc24926..faced80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,87 +1,14 @@ -import { spawnSync } from 'child_process' -import { existsSync, readFileSync, writeFileSync } from 'fs' +import { existsSync } from 'fs' import { join, resolve } from 'path' -import { Report } from './index.d' - -const PACKAGE_SIZE_PATT = /package size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/ -const UNPACKED_SIZE_PATT = /unpacked size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/ -const SIZE_SUFFIX_PATT = /([A-Za-z]+)/ -const SIZE_MAGNITUDE_PATT = /([0-9]+\.?[0-9]*)/ +import { + createOrUpdateManifest, + getCurrentPackageStats, + getPreviousPackageStats, +} from './utils' const MANIFEST_FILENAME = '.packwatch.json' -function convertSizeToBytes(sizeString: string): number { - const sizeSuffix = SIZE_SUFFIX_PATT.exec(sizeString)[1] - const sizeMagnitude = SIZE_MAGNITUDE_PATT.exec(sizeString)[1] - - let multiplier = 1 - - if (sizeSuffix === 'kB') multiplier = 1000 - else if (sizeSuffix === 'mB') { - multiplier = 1000000 - } - - return multiplier * parseFloat(sizeMagnitude) -} - -function getCurrentPackageStats(cwd: string): Report { - const { stderr } = spawnSync('npm', ['pack', '--dry-run'], { - encoding: 'utf-8', - cwd, - }) - const stderrString = String(stderr) - const packageSize = PACKAGE_SIZE_PATT.exec(stderrString)[1] - const unpackedSize = UNPACKED_SIZE_PATT.exec(stderrString)[1] - - return { - packageSize, - unpackedSize, - packageSizeBytes: convertSizeToBytes(packageSize), - unpackedSizeBytes: convertSizeToBytes(unpackedSize), - } -} - -function getPreviousPackageStats(cwd: string): Report | null { - const manifestPath = resolve(join(cwd, MANIFEST_FILENAME)) - try { - const currentManifest = readFileSync(manifestPath, { - encoding: 'utf-8', - }) - const parsedManifest = JSON.parse(currentManifest) - return { - ...parsedManifest, - packageSizeBytes: convertSizeToBytes(parsedManifest.packageSize), - unpackedSizeBytes: convertSizeToBytes(parsedManifest.unpackedSize), - limitBytes: convertSizeToBytes(parsedManifest.limit), - } - } catch { - /* No manifest */ - } -} - -function createOrUpdateManifest({ - previous, - current, - manifestPath, - updateLimit = false, -}: { - previous?: Report - current: Report - updateLimit?: boolean -}): void { - const { limit } = previous || {} - const { packageSize, unpackedSize } = current - - const newManifest = { - limit: updateLimit ? packageSize : limit || packageSize, - packageSize: packageSize, - unpackedSize: unpackedSize, - } - - writeFileSync(manifestPath, JSON.stringify(newManifest)) -} - export default function run({ cwd, isUpdatingManifest, @@ -89,10 +16,6 @@ export default function run({ cwd?: string isUpdatingManifest?: boolean }): number { - if (!cwd) { - cwd = process.cwd() - } - const packageJsonPath = resolve(join(cwd, 'package.json')) const manifestPath = resolve(join(cwd, MANIFEST_FILENAME)) diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..9e254fd --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,83 @@ +import { spawnSync } from 'child_process' +import { readFileSync, writeFileSync } from 'fs' +import { join, resolve } from 'path' + +import { Report } from './index.d' + +const PACKAGE_SIZE_PATT = /package size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/ +const UNPACKED_SIZE_PATT = /unpacked size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/ +const SIZE_SUFFIX_PATT = /([A-Za-z]+)/ +const SIZE_MAGNITUDE_PATT = /([0-9]+\.?[0-9]*)/ + +const MANIFEST_FILENAME = '.packwatch.json' + +export function convertSizeToBytes(sizeString: string): number { + const sizeSuffix = SIZE_SUFFIX_PATT.exec(sizeString)[1] + const sizeMagnitude = SIZE_MAGNITUDE_PATT.exec(sizeString)[1] + + let multiplier = 1 + + if (sizeSuffix === 'kB') multiplier = 1000 + else if (sizeSuffix === 'mB') { + multiplier = 1000000 + } + + return multiplier * parseFloat(sizeMagnitude) +} + +export function getCurrentPackageStats(cwd: string): Report { + const { stderr } = spawnSync('npm', ['pack', '--dry-run'], { + encoding: 'utf-8', + cwd, + }) + const stderrString = String(stderr) + const packageSize = PACKAGE_SIZE_PATT.exec(stderrString)[1] + const unpackedSize = UNPACKED_SIZE_PATT.exec(stderrString)[1] + + return { + packageSize, + unpackedSize, + packageSizeBytes: convertSizeToBytes(packageSize), + unpackedSizeBytes: convertSizeToBytes(unpackedSize), + } +} + +export function getPreviousPackageStats(cwd: string): Report | null { + const manifestPath = resolve(join(cwd, MANIFEST_FILENAME)) + try { + const currentManifest = readFileSync(manifestPath, { + encoding: 'utf-8', + }) + const parsedManifest = JSON.parse(currentManifest) + return { + ...parsedManifest, + packageSizeBytes: convertSizeToBytes(parsedManifest.packageSize), + unpackedSizeBytes: convertSizeToBytes(parsedManifest.unpackedSize), + limitBytes: convertSizeToBytes(parsedManifest.limit), + } + } catch { + /* No manifest */ + } +} + +export function createOrUpdateManifest({ + previous, + current, + manifestPath, + updateLimit = false, +}: { + previous?: Report + current: Report + updateLimit?: boolean +}): void { + const { limit } = previous || {} + const { packageSize, unpackedSize } = current + + const newManifest = { + limit: updateLimit ? packageSize : limit || packageSize, + packageSize: packageSize, + unpackedSize: unpackedSize, + } + + writeFileSync(manifestPath, JSON.stringify(newManifest)) +}