refactor: extract utils, hoist cwd up
This commit is contained in:
parent
33bfd474b6
commit
696d91a5f9
3 changed files with 91 additions and 84 deletions
|
@ -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)
|
||||
|
|
89
src/index.ts
89
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))
|
||||
|
||||
|
|
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