From 6e5f03f14cb0919fa27dbd7e02a04ec942ad8877 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 19 Feb 2021 02:00:39 -0500 Subject: [PATCH] refactor: fix types --- package.json | 4 +++- src/__tests__/index.test.ts | 12 ++++++++---- src/index.ts | 11 ++++++----- src/utils.ts | 1 + 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 776128f..48b8694 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,9 @@ "lint:fix": "yarn lint --fix", "test": "jest src", "test:watch": "yarn test --watchAll", - "test:coverage": "yarn test --coverage" + "test:coverage": "yarn test --coverage", + "types": "tsc --noEmit src/**/*.ts", + "types:watch": "yarn types --watch" }, "devDependencies": { "@babel/cli": "^7.8.4", diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index e2796f7..e44cd52 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -6,12 +6,12 @@ import type { Report } from '../index.d' import packwatch from '..' async function prepareWorkspace(): Promise { - const workspacePath = await fs.mkdtemp(`${tmpdir()}/`, { recursive: true }) + const workspacePath = await fs.mkdtemp(`${tmpdir()}/`) return workspacePath } async function cleanUpWorkspace(paths: string[]): Promise { - return Promise.all( + await Promise.all( paths.map(async path => fs.rmdir(path, { recursive: true })), ) } @@ -68,7 +68,9 @@ describe('Packwatch', () => { workspacePath = await prepareWorkspace() await createPackageJson(workspacePath) - await packwatch({ cwd: workspacePath }) + await expect(async () => + packwatch({ cwd: workspacePath }), + ).rejects.toThrow() const generatedManifest = await fs.readFile( resolve(join(workspacePath, '.packwatch.json')), @@ -84,7 +86,9 @@ describe('Packwatch', () => { workspacePath = await prepareWorkspace() await createPackageJson(workspacePath) - await packwatch({ cwd: workspacePath }) + await expect(async () => + packwatch({ cwd: workspacePath }), + ).rejects.toThrow() expect(mockLogger.mock.calls).toHaveLength(2) expect(mockLogger.mock.calls[0][0]).toEqual( diff --git a/src/index.ts b/src/index.ts index 4a799f6..c08abf2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ export default async function packwatch({ console.log( '🤔 There is no package.json file here. Are you in the root directory of your project?', ) - return 1 + return } const currentStats = getCurrentPackageStats(cwd) @@ -46,7 +46,8 @@ export default async function packwatch({ } // If the update flag wasn't specified, exit with a non-zero code so we // don't "accidentally" pass CI builds if the manifest didn't exist - return isUpdatingManifest ? 0 : 1 + if (!isUpdatingManifest) throw new Error() + else return } const previousStats = getPreviousPackageStats(cwd) @@ -73,7 +74,7 @@ export default async function packwatch({ console.log( `📝 Updated the manifest! Package size: ${packageSize}, Limit: ${packageSize}`, ) - return 0 + return } /* @@ -85,7 +86,7 @@ export default async function packwatch({ console.log( `🔥🔥📦🔥🔥 Your package exceeds the limit set in ${MANIFEST_FILENAME}! ${packageSize} > ${limit}\nEither update the limit by using the --update-manifest flag or trim down your packed files!`, ) - return 1 + return } /* @@ -107,5 +108,5 @@ export default async function packwatch({ `📦 Nothing to report! Your package is the same size as the latest manifest reports! (Limit: ${limit})`, ) } - return 0 + return } diff --git a/src/utils.ts b/src/utils.ts index 9e254fd..0c5182b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -68,6 +68,7 @@ export function createOrUpdateManifest({ }: { previous?: Report current: Report + manifestPath: string updateLimit?: boolean }): void { const { limit } = previous || {}