From d3b4dae910d57406aeae14a3e3c170613c0ba22d Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 19 Feb 2021 01:46:14 -0500 Subject: [PATCH] refactor: rename, async main --- src/__tests__/index.test.ts | 22 +++++++++++----------- src/cli.ts | 7 ++++--- src/index.ts | 4 ++-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 880a897..e2796f7 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -3,7 +3,7 @@ import { tmpdir } from 'os' import { join, resolve } from 'path' import type { Report } from '../index.d' -import runPackwatch from '..' +import packwatch from '..' async function prepareWorkspace(): Promise { const workspacePath = await fs.mkdtemp(`${tmpdir()}/`, { recursive: true }) @@ -53,7 +53,7 @@ describe('Packwatch', () => { it('warns the user and errors if run away from package.json', async () => { workspacePath = await prepareWorkspace() - runPackwatch({ cwd: workspacePath }) + await packwatch({ cwd: workspacePath }) expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls[0][0]).toEqual( @@ -68,7 +68,7 @@ describe('Packwatch', () => { workspacePath = await prepareWorkspace() await createPackageJson(workspacePath) - runPackwatch({ cwd: workspacePath }) + await packwatch({ cwd: workspacePath }) const generatedManifest = await fs.readFile( resolve(join(workspacePath, '.packwatch.json')), @@ -84,7 +84,7 @@ describe('Packwatch', () => { workspacePath = await prepareWorkspace() await createPackageJson(workspacePath) - runPackwatch({ cwd: workspacePath }) + await packwatch({ cwd: workspacePath }) expect(mockLogger.mock.calls).toHaveLength(2) expect(mockLogger.mock.calls[0][0]).toEqual( @@ -104,7 +104,7 @@ describe('Packwatch', () => { await createPackageJson(workspacePath) - runPackwatch({ cwd: workspacePath, isUpdatingManifest: true }) + await packwatch({ cwd: workspacePath, isUpdatingManifest: true }) expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls[0][0]).toEqual( @@ -125,7 +125,7 @@ describe('Packwatch', () => { packageSize: '160B', unpackedSize: '150B', }) - runPackwatch({ cwd: workspacePath }) + await packwatch({ cwd: workspacePath }) expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls[0][0]).toEqual( expect.stringMatching( @@ -144,7 +144,7 @@ describe('Packwatch', () => { unpackedSize: '150B', }) - runPackwatch({ cwd: workspacePath }) + await packwatch({ cwd: workspacePath }) expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls[0][0]).toEqual( expect.stringMatching( @@ -162,7 +162,7 @@ describe('Packwatch', () => { unpackedSize: '140B', }) - runPackwatch({ cwd: workspacePath }) + await packwatch({ cwd: workspacePath }) expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls[0][0]).toEqual( expect.stringMatching( @@ -180,7 +180,7 @@ describe('Packwatch', () => { unpackedSize: '140B', }) - runPackwatch({ cwd: workspacePath }) + await packwatch({ cwd: workspacePath }) expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls[0][0]).toEqual( expect.stringMatching( @@ -198,7 +198,7 @@ describe('Packwatch', () => { unpackedSize: '140B', }) - runPackwatch({ cwd: workspacePath }) + await packwatch({ cwd: workspacePath }) expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls[0][0]).toEqual( expect.stringMatching( @@ -217,7 +217,7 @@ describe('Packwatch', () => { unpackedSize: '140B', }) - runPackwatch({ cwd: workspacePath, isUpdatingManifest: true }) + await packwatch({ cwd: workspacePath, isUpdatingManifest: true }) expect(mockLogger.mock.calls).toHaveLength(1) expect(mockLogger.mock.calls[0][0]).toEqual( expect.stringMatching( diff --git a/src/cli.ts b/src/cli.ts index a13de2f..3d830c3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,8 +1,9 @@ #!/usr/bin/env node -import runPackwatch from '.' +import packwatch from '.' const isUpdatingManifest = process.argv.includes('--update-manifest') const cwd = process.cwd() -const processExit = runPackwatch({ cwd, isUpdatingManifest }) -process.exit(processExit) +packwatch({ cwd, isUpdatingManifest }) + .catch(() => process.exit(1)) + .then(() => process.exit(0)) diff --git a/src/index.ts b/src/index.ts index faced80..4a799f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,13 +9,13 @@ import { const MANIFEST_FILENAME = '.packwatch.json' -export default function run({ +export default async function packwatch({ cwd, isUpdatingManifest, }: { cwd?: string isUpdatingManifest?: boolean -}): number { +}): Promise { const packageJsonPath = resolve(join(cwd, 'package.json')) const manifestPath = resolve(join(cwd, MANIFEST_FILENAME))