Skip to content

Commit

Permalink
fix: don't swallow errors in hooks - fix cleaning build directory
Browse files Browse the repository at this point in the history
closes #194
  • Loading branch information
danielroe committed Nov 23, 2020
1 parent 607be0e commit 43cc5fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function removeBuildFolders(pkg: Package) {
.filter(dir => !dir.includes('src'))
)

await runInParallel(directories, remove)
await runInParallel(directories, dir => remove(dir))
}

export const build = async (
Expand Down
17 changes: 15 additions & 2 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { extname } from 'path'
import consola from 'consola'
import { readJSONSync } from 'fs-extra'
import _glob from 'glob'
import _jiti from 'jiti'
Expand Down Expand Up @@ -83,7 +84,14 @@ export const runInParallel = async <T, R extends any>(
cb: (item: T, index: number) => Promise<R> | R
) => {
if (Array.isArray(items))
return Promise.allSettled(items.map(async (item, index) => cb(item, index)))
return Promise.allSettled(
items.map(async (item, index) => cb(item, index))
).then(results =>
results.map(result => {
if (result.status === 'rejected') consola.error(result.reason)
return result
})
)

const promises: Array<Promise<R>> = []
let index = 0
Expand All @@ -95,7 +103,12 @@ export const runInParallel = async <T, R extends any>(
}
index++
}
return Promise.allSettled(promises)
return Promise.allSettled(promises).then(results =>
results.map(result => {
if (result.status === 'rejected') consola.error(result.reason)
return result
})
)
}

export const asArray = <T>(item: T | T[] | undefined): T[] =>
Expand Down
5 changes: 5 additions & 0 deletions src/core/utils/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path'
import consola from 'consola'

import type { PackageJson } from '../package'
import { loadAllSettled, loadFromEntries } from './polyfills'
Expand All @@ -15,6 +16,8 @@ import {
tryRequire,
} from '.'

jest.mock('consola')

describe('asArray', () => {
it('should return an array if passed an undefined value', () => {
expect(asArray(undefined)).toEqual([])
Expand Down Expand Up @@ -110,10 +113,12 @@ const runInParallelTest = () => {
expect(i).toBe(6)
})
it('should not fail if one task does', async () => {
jest.clearAllMocks()
const result = await runInParallel([1, 2, 3], async num => {
if (num === 2) throw new Error('')
})
expect(result.length).toBe(3)
expect(consola.error).toHaveBeenCalledTimes(1)
})
}

Expand Down

0 comments on commit 43cc5fa

Please sign in to comment.