refactor: rough conversion
This commit is contained in:
parent
b90a1e070c
commit
987a1eaf19
3 changed files with 48 additions and 31 deletions
|
@ -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,
|
||||
}
|
|
@ -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()
|
||||
})
|
||||
})
|
||||
|
Reference in a new issue