build: use rome as linter+formatter (#310)
This commit is contained in:
parent
3c8cb50170
commit
f591e1b968
13 changed files with 432 additions and 2295 deletions
13
.eslintrc.js
13
.eslintrc.js
|
@ -1,13 +0,0 @@
|
|||
module.exports = {
|
||||
parser: "@typescript-eslint/parser",
|
||||
plugins: ["@typescript-eslint"],
|
||||
extends: [
|
||||
"@tophat/eslint-config/base",
|
||||
"@tophat/eslint-config/jest",
|
||||
"plugin:@typescript-eslint/eslint-recommended",
|
||||
"plugin:@typescript-eslint/recommended"
|
||||
],
|
||||
parserOptions: {
|
||||
project: ['./tsconfig.eslint.json']
|
||||
}
|
||||
}
|
15
package.json
15
package.json
|
@ -30,8 +30,8 @@
|
|||
"prepack": "yarn build",
|
||||
"prebuild": "rm -rf dist",
|
||||
"build": "tsc --project .",
|
||||
"lint": "eslint tests/**/*.ts src/**/*.ts",
|
||||
"lint:fix": "yarn lint --fix",
|
||||
"lint": "yarn rome format src tests && yarn rome check src tests",
|
||||
"lint:fix": "yarn rome format src tests --write && yarn rome check src tests --apply",
|
||||
"test": "jest tests",
|
||||
"test:watch": "yarn test --watchAll",
|
||||
"test:coverage": "yarn test --coverage",
|
||||
|
@ -39,19 +39,10 @@
|
|||
"types:watch": "yarn types --watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tophat/eslint-config": "^6.0.1",
|
||||
"@types/jest": "^29.5.0",
|
||||
"@types/node": "^18.15.5",
|
||||
"@typescript-eslint/eslint-plugin": "^5.56.0",
|
||||
"@typescript-eslint/parser": "^5.56.0",
|
||||
"eslint": "^8.36.0",
|
||||
"eslint-config-prettier": "^8.8.0",
|
||||
"eslint-import-resolver-node": "^0.3.7",
|
||||
"eslint-plugin-import": "^2.27.5",
|
||||
"eslint-plugin-jest": "^27.2.1",
|
||||
"eslint-plugin-prettier": "^4.2.1",
|
||||
"jest": "^29.5.0",
|
||||
"prettier": "^2.8.5",
|
||||
"rome": "^11.0.0",
|
||||
"ts-jest": "^29.0.5",
|
||||
"typescript": "^4.3.0"
|
||||
}
|
||||
|
|
13
rome.json
Normal file
13
rome.json
Normal file
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"formatter": {
|
||||
"enabled": true,
|
||||
"lineWidth": 120
|
||||
},
|
||||
"javascript": {
|
||||
"formatter": {
|
||||
"semicolons": "asNeeded",
|
||||
"quoteStyle": "single"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
34
src/index.ts
34
src/index.ts
|
@ -3,20 +3,13 @@ import { join, resolve } from 'path'
|
|||
|
||||
import { assertInPackageRoot } from './invariants'
|
||||
import logger from './logger'
|
||||
import {
|
||||
createOrUpdateManifest,
|
||||
getCurrentPackageStats,
|
||||
getPreviousPackageStats,
|
||||
mergeDefaultArguments,
|
||||
} from './utils'
|
||||
import { createOrUpdateManifest, getCurrentPackageStats, getPreviousPackageStats, mergeDefaultArguments } from './utils'
|
||||
|
||||
import type { PackwatchArguments } from './types'
|
||||
|
||||
const MANIFEST_FILENAME = '.packwatch.json'
|
||||
|
||||
export default async function packwatch(
|
||||
args: Partial<PackwatchArguments>,
|
||||
): Promise<void> {
|
||||
export default async function packwatch(args: Partial<PackwatchArguments>): Promise<void> {
|
||||
const { cwd, isUpdatingManifest } = mergeDefaultArguments(args)
|
||||
|
||||
const manifestPath = resolve(join(cwd, MANIFEST_FILENAME))
|
||||
|
@ -47,12 +40,7 @@ export default async function packwatch(
|
|||
|
||||
const previousStats = getPreviousPackageStats(cwd)
|
||||
const { packageSizeBytes, packageSize } = currentStats
|
||||
const {
|
||||
packageSize: previousSize,
|
||||
packageSizeBytes: previousSizeBytes,
|
||||
limit,
|
||||
limitBytes,
|
||||
} = previousStats
|
||||
const { packageSize: previousSize, packageSizeBytes: previousSizeBytes, limit, limitBytes } = previousStats
|
||||
const hasExceededLimit = limitBytes && packageSizeBytes > limitBytes
|
||||
|
||||
/*
|
||||
|
@ -66,9 +54,7 @@ export default async function packwatch(
|
|||
updateLimit: true,
|
||||
manifestPath,
|
||||
})
|
||||
logger.log(
|
||||
`📝 Updated the manifest! Package size: ${packageSize}, Limit: ${packageSize}`,
|
||||
)
|
||||
logger.log(`📝 Updated the manifest! Package size: ${packageSize}, Limit: ${packageSize}`)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -91,17 +77,11 @@ export default async function packwatch(
|
|||
*/
|
||||
|
||||
if (packageSizeBytes > previousSizeBytes) {
|
||||
logger.log(
|
||||
`📦 👀 Your package grew! ${packageSize} > ${previousSize} (Limit: ${limit})`,
|
||||
)
|
||||
logger.log(`📦 👀 Your package grew! ${packageSize} > ${previousSize} (Limit: ${limit})`)
|
||||
} else if (packageSizeBytes < previousSizeBytes) {
|
||||
logger.log(
|
||||
`📦 💯 Your package shrank! ${packageSize} < ${previousSize} (Limit: ${limit})`,
|
||||
)
|
||||
logger.log(`📦 💯 Your package shrank! ${packageSize} < ${previousSize} (Limit: ${limit})`)
|
||||
} else {
|
||||
logger.log(
|
||||
`📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: ${limit})`,
|
||||
)
|
||||
logger.log(`📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: ${limit})`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -8,9 +8,7 @@ export function assertInPackageRoot(cwd: string): void {
|
|||
const packageJsonExists = existsSync(packagePath)
|
||||
|
||||
if (!packageJsonExists) {
|
||||
logger.log(
|
||||
'🤔 There is no package.json file here. Are you in the root directory of your project?',
|
||||
)
|
||||
logger.log('🤔 There is no package.json file here. Are you in the root directory of your project?')
|
||||
throw new Error('NOT_IN_PACKAGE_ROOT')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,9 +11,7 @@ const SIZE_MAGNITUDE_PATT = /([0-9]+\.?[0-9]*)/
|
|||
|
||||
const MANIFEST_FILENAME = '.packwatch.json'
|
||||
|
||||
export function mergeDefaultArguments(
|
||||
args: Partial<PackwatchArguments>,
|
||||
): PackwatchArguments {
|
||||
export function mergeDefaultArguments(args: Partial<PackwatchArguments>): PackwatchArguments {
|
||||
return {
|
||||
cwd: args.cwd ?? '.',
|
||||
isUpdatingManifest: args.isUpdatingManifest ?? false,
|
||||
|
|
|
@ -23,9 +23,7 @@ async function prepareWorkspace(): Promise<string> {
|
|||
}
|
||||
|
||||
async function cleanUpWorkspace(paths: string[]): Promise<void> {
|
||||
await Promise.all(
|
||||
paths.map(async (path) => fs.rmdir(path, { recursive: true })),
|
||||
)
|
||||
await Promise.all(paths.map(async (path) => fs.rmdir(path, { recursive: true })))
|
||||
}
|
||||
|
||||
async function createFile(path: string, content: string): Promise<void> {
|
||||
|
@ -34,16 +32,10 @@ async function createFile(path: string, content: string): Promise<void> {
|
|||
|
||||
async function createPackageJson(cwd: string): Promise<void> {
|
||||
const path = resolve(join(cwd, 'package.json'))
|
||||
await createFile(
|
||||
path,
|
||||
'{ "name": "wow", "version": "0.0.0", "files": ["!.packwatch.json"] }',
|
||||
)
|
||||
await createFile(path, '{ "name": "wow", "version": "0.0.0", "files": ["!.packwatch.json"] }')
|
||||
}
|
||||
|
||||
async function createManifest(
|
||||
cwd: string,
|
||||
configuration: Report,
|
||||
): Promise<void> {
|
||||
async function createManifest(cwd: string, configuration: Report): Promise<void> {
|
||||
const path = resolve(join(cwd, '.packwatch.json'))
|
||||
await createFile(path, JSON.stringify(configuration))
|
||||
}
|
||||
|
@ -63,15 +55,11 @@ describe('Packwatch', () => {
|
|||
const workspacePath = await prepareWorkspace()
|
||||
const mockLogger = jest.spyOn(console, 'log')
|
||||
|
||||
await expect(async () =>
|
||||
packwatch({ cwd: workspacePath }),
|
||||
).rejects.toThrow('NOT_IN_PACKAGE_ROOT')
|
||||
await expect(async () => packwatch({ cwd: workspacePath })).rejects.toThrow('NOT_IN_PACKAGE_ROOT')
|
||||
|
||||
expect(mockLogger.mock.calls).toHaveLength(1)
|
||||
expect(mockLogger.mock.calls[0][0]).toEqual(
|
||||
expect.stringMatching(
|
||||
'There is no package.json file here. Are you in the root directory of your project?',
|
||||
),
|
||||
expect.stringMatching('There is no package.json file here. Are you in the root directory of your project?'),
|
||||
)
|
||||
})
|
||||
|
||||
|
@ -80,14 +68,9 @@ describe('Packwatch', () => {
|
|||
const workspacePath = await prepareWorkspace()
|
||||
await createPackageJson(workspacePath)
|
||||
|
||||
await expect(async () =>
|
||||
packwatch({ cwd: workspacePath }),
|
||||
).rejects.toThrow('NO_MANIFEST_NO_UPDATE')
|
||||
await expect(async () => packwatch({ cwd: workspacePath })).rejects.toThrow('NO_MANIFEST_NO_UPDATE')
|
||||
|
||||
const generatedManifest = await fs.readFile(
|
||||
resolve(join(workspacePath, '.packwatch.json')),
|
||||
{ encoding: 'utf8' },
|
||||
)
|
||||
const generatedManifest = await fs.readFile(resolve(join(workspacePath, '.packwatch.json')), { encoding: 'utf8' })
|
||||
|
||||
expect(generatedManifest).toBe(
|
||||
`{"limit":"${actualSize} B","packageSize":"${actualSize} B","unpackedSize":"68 B"}`,
|
||||
|
@ -100,9 +83,7 @@ describe('Packwatch', () => {
|
|||
const mockError = jest.spyOn(console, 'error')
|
||||
await createPackageJson(workspacePath)
|
||||
|
||||
await expect(async () =>
|
||||
packwatch({ cwd: workspacePath }),
|
||||
).rejects.toThrow()
|
||||
await expect(async () => packwatch({ cwd: workspacePath })).rejects.toThrow()
|
||||
|
||||
expect(mockWarn.mock.calls).toHaveLength(1)
|
||||
expect(mockWarn.mock.calls[0][0]).toEqual(
|
||||
|
@ -150,9 +131,7 @@ describe('Packwatch', () => {
|
|||
await packwatch({ cwd: workspacePath })
|
||||
expect(mockLogger.mock.calls).toHaveLength(1)
|
||||
expect(mockLogger.mock.calls[0][0]).toEqual(
|
||||
expect.stringMatching(
|
||||
/Nothing to report! Your package is the same size as the latest manifest reports!/,
|
||||
),
|
||||
expect.stringMatching(/Nothing to report! Your package is the same size as the latest manifest reports!/),
|
||||
)
|
||||
})
|
||||
|
||||
|
@ -191,9 +170,7 @@ describe('Packwatch', () => {
|
|||
await packwatch({ cwd: workspacePath })
|
||||
expect(mockLogger.mock.calls).toHaveLength(1)
|
||||
expect(mockLogger.mock.calls[0][0]).toEqual(
|
||||
expect.stringMatching(
|
||||
/Your package grew! \d+ B > 150B \(Limit: 180B\)/,
|
||||
),
|
||||
expect.stringMatching(/Your package grew! \d+ B > 150B \(Limit: 180B\)/),
|
||||
)
|
||||
})
|
||||
it('messages when the size is lower than the limit (shrinkage)', async () => {
|
||||
|
@ -211,9 +188,7 @@ describe('Packwatch', () => {
|
|||
await packwatch({ cwd: workspacePath })
|
||||
expect(mockLogger.mock.calls).toHaveLength(1)
|
||||
expect(mockLogger.mock.calls[0][0]).toEqual(
|
||||
expect.stringMatching(
|
||||
/Your package shrank! \d+ B < 170B \(Limit: 180B\)/,
|
||||
),
|
||||
expect.stringMatching(/Your package shrank! \d+ B < 170B \(Limit: 180B\)/),
|
||||
)
|
||||
})
|
||||
it('messages when the size exceeds the limit', async () => {
|
||||
|
@ -228,9 +203,7 @@ describe('Packwatch', () => {
|
|||
unpackedSizeBytes: 140,
|
||||
})
|
||||
|
||||
await expect(async () =>
|
||||
packwatch({ cwd: workspacePath }),
|
||||
).rejects.toThrow('PACKAGE_EXCEEDS_LIMIT')
|
||||
await expect(async () => packwatch({ cwd: workspacePath })).rejects.toThrow('PACKAGE_EXCEEDS_LIMIT')
|
||||
expect(mockError.mock.calls).toHaveLength(1)
|
||||
expect(mockError.mock.calls[0][0]).toEqual(
|
||||
expect.stringMatching(
|
||||
|
@ -254,9 +227,7 @@ describe('Packwatch', () => {
|
|||
await packwatch({ cwd: workspacePath, isUpdatingManifest: true })
|
||||
expect(mockLogger.mock.calls).toHaveLength(1)
|
||||
expect(mockLogger.mock.calls[0][0]).toEqual(
|
||||
expect.stringMatching(
|
||||
/Updated the manifest! Package size: \d+ B, Limit: \d+ B/,
|
||||
),
|
||||
expect.stringMatching(/Updated the manifest! Package size: \d+ B, Limit: \d+ B/),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -6,10 +6,7 @@ describe('utils', () => {
|
|||
${'1 B'} | ${1}
|
||||
${'1 kB'} | ${1000}
|
||||
${'1 mB'} | ${1000000}
|
||||
`(
|
||||
'converts sizes properly ($initialSize -> $expectedSize)',
|
||||
({ initialSize, expectedSize }) => {
|
||||
`('converts sizes properly ($initialSize -> $expectedSize)', ({ initialSize, expectedSize }) => {
|
||||
expect(convertSizeToBytes(initialSize)).toEqual(expectedSize)
|
||||
},
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": ["src", "tests", ".eslintrc.js"],
|
||||
"exclude": [
|
||||
"dist/**/*"
|
||||
]
|
||||
}
|
Reference in a new issue