refactor: move to Typescript #146
3 changed files with 48 additions and 31 deletions
|
@ -1,15 +1,26 @@
|
||||||
const { spawnSync } = require('child_process')
|
import { spawnSync } from 'child_process'
|
||||||
const { readFileSync, writeFileSync } = require('fs')
|
import { readFileSync, writeFileSync } from 'fs'
|
||||||
|
|
||||||
const PACKAGE_SIZE_PATT = /package size:\s+([0-9]+\.?[0-9]*\s+[A-Za-z]+)/g
|
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 UNPACKED_SIZE_PATT = /unpacked size:\s+([0-9]+\.?[0-9]*\s+[A-Za-z]+)/g
|
||||||
const SIZE_SUFFIX_PATT = /([A-Za-z]+)/
|
const SIZE_SUFFIX_PATT = /([A-Za-z]+)/
|
||||||
const SIZE_MAGNITUDE_PATT = /([0-9]+\.?[0-9]*)/
|
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 sizeSuffix = SIZE_SUFFIX_PATT.exec(sizeString)[1]
|
||||||
const sizeMagnitude = SIZE_MAGNITUDE_PATT.exec(sizeString)[1]
|
const sizeMagnitude = SIZE_MAGNITUDE_PATT.exec(sizeString)[1]
|
||||||
|
|
||||||
|
@ -23,10 +34,13 @@ function convertSizeToBytes(sizeString) {
|
||||||
return multiplier * parseFloat(sizeMagnitude)
|
return multiplier * parseFloat(sizeMagnitude)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentPackageStats() {
|
export function getCurrentPackageStats(): Report {
|
||||||
const { stderr } = spawnSync('npm', ['pack', '--dry-run'], FS_OPTIONS)
|
const { stderr } = spawnSync('npm', ['pack', '--dry-run'], {
|
||||||
const packageSize = PACKAGE_SIZE_PATT.exec(stderr)[1]
|
encoding: 'utf-8',
|
||||||
const unpackedSize = UNPACKED_SIZE_PATT.exec(stderr)[1]
|
})
|
||||||
|
const stderrString = String(stderr)
|
||||||
|
const packageSize = PACKAGE_SIZE_PATT.exec(stderrString)[1]
|
||||||
|
const unpackedSize = UNPACKED_SIZE_PATT.exec(stderrString)[1]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
packageSize,
|
packageSize,
|
||||||
|
@ -36,9 +50,11 @@ function getCurrentPackageStats() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPreviousPackageStats() {
|
export function getPreviousPackageStats(): Report | null {
|
||||||
try {
|
try {
|
||||||
const currentManifest = readFileSync(MANIFEST_FILENAME, FS_OPTIONS)
|
const currentManifest = readFileSync(MANIFEST_FILENAME, {
|
||||||
|
encoding: 'utf-8',
|
||||||
|
})
|
||||||
const parsedManifest = JSON.parse(currentManifest)
|
const parsedManifest = JSON.parse(currentManifest)
|
||||||
return {
|
return {
|
||||||
...parsedManifest,
|
...parsedManifest,
|
||||||
|
@ -46,12 +62,20 @@ function getPreviousPackageStats() {
|
||||||
unpackedSizeBytes: convertSizeToBytes(parsedManifest.unpackedSize),
|
unpackedSizeBytes: convertSizeToBytes(parsedManifest.unpackedSize),
|
||||||
limitBytes: convertSizeToBytes(parsedManifest.limit),
|
limitBytes: convertSizeToBytes(parsedManifest.limit),
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch {
|
||||||
return {}
|
/* 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 { limit } = previous || {}
|
||||||
const { packageSize, unpackedSize } = current
|
const { packageSize, unpackedSize } = current
|
||||||
|
|
||||||
|
@ -63,11 +87,3 @@ function createOrUpdateManifest({ previous, current, updateLimit = false }) {
|
||||||
|
|
||||||
writeFileSync(MANIFEST_FILENAME, JSON.stringify(newManifest))
|
writeFileSync(MANIFEST_FILENAME, JSON.stringify(newManifest))
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
createOrUpdateManifest,
|
|
||||||
getPreviousPackageStats,
|
|
||||||
getCurrentPackageStats,
|
|
||||||
convertSizeToBytes,
|
|
||||||
MANIFEST_FILENAME,
|
|
||||||
}
|
|
|
@ -1,18 +1,19 @@
|
||||||
const childProcess = require('child_process')
|
import * as childProcess from 'child_process'
|
||||||
const { readFileSync } = require('fs')
|
import { readFileSync } from 'fs'
|
||||||
|
|
||||||
const mockFS = require('mock-fs')
|
import mockFS from 'mock-fs'
|
||||||
|
|
||||||
jest.mock('child_process')
|
jest.mock('child_process', () => ({
|
||||||
childProcess.spawnSync = jest.fn(() => ({ stderr: mockPackOutput }))
|
spawnSync: () => ({ stderr: mockPackOutput })
|
||||||
|
}))
|
||||||
|
|
||||||
const {
|
import {
|
||||||
MANIFEST_FILENAME,
|
MANIFEST_FILENAME,
|
||||||
convertSizeToBytes,
|
convertSizeToBytes,
|
||||||
|
createOrUpdateManifest,
|
||||||
getCurrentPackageStats,
|
getCurrentPackageStats,
|
||||||
getPreviousPackageStats,
|
getPreviousPackageStats,
|
||||||
createOrUpdateManifest,
|
} from './helpers'
|
||||||
} = require('./helpers')
|
|
||||||
|
|
||||||
const mockPackageSize = '1.1 kB'
|
const mockPackageSize = '1.1 kB'
|
||||||
const mockUnpackedSize = '9000 kB'
|
const mockUnpackedSize = '9000 kB'
|
||||||
|
@ -100,7 +101,7 @@ describe('Helpers', () => {
|
||||||
[MANIFEST_FILENAME]: 'not valid JSON',
|
[MANIFEST_FILENAME]: 'not valid JSON',
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(getPreviousPackageStats()).toEqual({})
|
expect(getPreviousPackageStats()).toBeUndefined()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Reference in a new issue