diff --git a/src/helpers.js b/src/helpers.ts similarity index 59% rename from src/helpers.js rename to src/helpers.ts index 03f1399..d82958a 100644 --- a/src/helpers.js +++ b/src/helpers.ts @@ -1,15 +1,26 @@ -const { spawnSync } = require('child_process') -const { readFileSync, writeFileSync } = require('fs') +import { spawnSync } from 'child_process' +import { readFileSync, writeFileSync } from 'fs' const PACKAGE_SIZE_PATT = /package size:\s+([0-9]+\.?[0-9]*\s+[A-Za-z]+)/g const UNPACKED_SIZE_PATT = /unpacked size:\s+([0-9]+\.?[0-9]*\s+[A-Za-z]+)/g const SIZE_SUFFIX_PATT = /([A-Za-z]+)/ const SIZE_MAGNITUDE_PATT = /([0-9]+\.?[0-9]*)/ -const MANIFEST_FILENAME = '.packwatch.json' -const FS_OPTIONS = { encoding: 'utf-8' } +export const MANIFEST_FILENAME = '.packwatch.json' -function convertSizeToBytes(sizeString) { +type Report = { + packageSize: string + unpackedSize: string + packageSizeBytes?: number + unpackedSizeBytes?: number +} + +type Digest = { + limit: string + packageSize: string +} + +export function convertSizeToBytes(sizeString: string): number { const sizeSuffix = SIZE_SUFFIX_PATT.exec(sizeString)[1] const sizeMagnitude = SIZE_MAGNITUDE_PATT.exec(sizeString)[1] @@ -23,10 +34,13 @@ function convertSizeToBytes(sizeString) { return multiplier * parseFloat(sizeMagnitude) } -function getCurrentPackageStats() { - const { stderr } = spawnSync('npm', ['pack', '--dry-run'], FS_OPTIONS) - const packageSize = PACKAGE_SIZE_PATT.exec(stderr)[1] - const unpackedSize = UNPACKED_SIZE_PATT.exec(stderr)[1] +export function getCurrentPackageStats(): Report { + const { stderr } = spawnSync('npm', ['pack', '--dry-run'], { + encoding: 'utf-8', + }) + const stderrString = String(stderr) + const packageSize = PACKAGE_SIZE_PATT.exec(stderrString)[1] + const unpackedSize = UNPACKED_SIZE_PATT.exec(stderrString)[1] return { packageSize, @@ -36,9 +50,11 @@ function getCurrentPackageStats() { } } -function getPreviousPackageStats() { +export function getPreviousPackageStats(): Report | null { try { - const currentManifest = readFileSync(MANIFEST_FILENAME, FS_OPTIONS) + const currentManifest = readFileSync(MANIFEST_FILENAME, { + encoding: 'utf-8', + }) const parsedManifest = JSON.parse(currentManifest) return { ...parsedManifest, @@ -46,12 +62,20 @@ function getPreviousPackageStats() { unpackedSizeBytes: convertSizeToBytes(parsedManifest.unpackedSize), limitBytes: convertSizeToBytes(parsedManifest.limit), } - } catch (e) { - return {} + } catch { + /* No manifest */ } } -function createOrUpdateManifest({ previous, current, updateLimit = false }) { +export function createOrUpdateManifest({ + previous, + current, + updateLimit = false, +}: { + previous?: Digest + current: Report + updateLimit?: boolean +}) { const { limit } = previous || {} const { packageSize, unpackedSize } = current @@ -63,11 +87,3 @@ function createOrUpdateManifest({ previous, current, updateLimit = false }) { writeFileSync(MANIFEST_FILENAME, JSON.stringify(newManifest)) } - -module.exports = { - createOrUpdateManifest, - getPreviousPackageStats, - getCurrentPackageStats, - convertSizeToBytes, - MANIFEST_FILENAME, -} diff --git a/src/index.test.js b/src/index.test.ts similarity index 94% rename from src/index.test.js rename to src/index.test.ts index ae0a9aa..a178827 100644 --- a/src/index.test.js +++ b/src/index.test.ts @@ -1,18 +1,19 @@ -const childProcess = require('child_process') -const { readFileSync } = require('fs') +import * as childProcess from 'child_process' +import { readFileSync } from 'fs' -const mockFS = require('mock-fs') +import mockFS from 'mock-fs' -jest.mock('child_process') -childProcess.spawnSync = jest.fn(() => ({ stderr: mockPackOutput })) +jest.mock('child_process', () => ({ + spawnSync: () => ({ stderr: mockPackOutput }) +})) -const { +import { MANIFEST_FILENAME, convertSizeToBytes, + createOrUpdateManifest, getCurrentPackageStats, getPreviousPackageStats, - createOrUpdateManifest, -} = require('./helpers') +} from './helpers' const mockPackageSize = '1.1 kB' const mockUnpackedSize = '9000 kB' @@ -100,7 +101,7 @@ describe('Helpers', () => { [MANIFEST_FILENAME]: 'not valid JSON', }) - expect(getPreviousPackageStats()).toEqual({}) + expect(getPreviousPackageStats()).toBeUndefined() }) }) diff --git a/src/index.js b/src/index.ts similarity index 100% rename from src/index.js rename to src/index.ts