refactor: mockless tests #163
3 changed files with 91 additions and 84 deletions
|
@ -3,5 +3,6 @@
|
||||||
import runPackwatch from '.'
|
import runPackwatch from '.'
|
||||||
|
|
||||||
const isUpdatingManifest = process.argv.includes('--update-manifest')
|
const isUpdatingManifest = process.argv.includes('--update-manifest')
|
||||||
const processExit = runPackwatch({ isUpdatingManifest })
|
const cwd = process.cwd()
|
||||||
|
const processExit = runPackwatch({ cwd, isUpdatingManifest })
|
||||||
process.exit(processExit)
|
process.exit(processExit)
|
||||||
|
|
89
src/index.ts
89
src/index.ts
|
@ -1,87 +1,14 @@
|
||||||
import { spawnSync } from 'child_process'
|
import { existsSync } from 'fs'
|
||||||
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
|
||||||
import { join, resolve } from 'path'
|
import { join, resolve } from 'path'
|
||||||
|
|
||||||
import { Report } from './index.d'
|
import {
|
||||||
|
createOrUpdateManifest,
|
||||||
const PACKAGE_SIZE_PATT = /package size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/
|
getCurrentPackageStats,
|
||||||
const UNPACKED_SIZE_PATT = /unpacked size:\s*([0-9]+\.?[0-9]*\s+[A-Za-z]{1,2})/
|
getPreviousPackageStats,
|
||||||
const SIZE_SUFFIX_PATT = /([A-Za-z]+)/
|
} from './utils'
|
||||||
const SIZE_MAGNITUDE_PATT = /([0-9]+\.?[0-9]*)/
|
|
||||||
|
|
||||||
const MANIFEST_FILENAME = '.packwatch.json'
|
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({
|
export default function run({
|
||||||
cwd,
|
cwd,
|
||||||
isUpdatingManifest,
|
isUpdatingManifest,
|
||||||
|
@ -89,10 +16,6 @@ export default function run({
|
||||||
cwd?: string
|
cwd?: string
|
||||||
isUpdatingManifest?: boolean
|
isUpdatingManifest?: boolean
|
||||||
}): number {
|
}): number {
|
||||||
if (!cwd) {
|
|
||||||
cwd = process.cwd()
|
|
||||||
}
|
|
||||||
|
|
||||||
const packageJsonPath = resolve(join(cwd, 'package.json'))
|
const packageJsonPath = resolve(join(cwd, 'package.json'))
|
||||||
const manifestPath = resolve(join(cwd, MANIFEST_FILENAME))
|
const manifestPath = resolve(join(cwd, MANIFEST_FILENAME))
|
||||||
|
|
||||||
|
|
83
src/utils.ts
Normal file
83
src/utils.ts
Normal file
|
@ -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))
|
||||||
|
}
|
Reference in a new issue