Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ improve(patch): bud.hooks #1903

Merged
merged 6 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cacheFolder: storage/yarn/cache

defaultSemverRangePrefix: ""
defaultSemverRangePrefix: ''

enableMessageNames: false

Expand All @@ -12,21 +12,21 @@ installStatePath: storage/yarn/install-state.gz

nodeLinker: node-modules

npmAuthToken: "${NPM_AUTH_TOKEN:-fallback}"
npmAuthToken: '${NPM_AUTH_TOKEN:-fallback}'

npmPublishAccess: public

npmPublishRegistry: "https://registry.npmjs.org"
npmPublishRegistry: 'https://registry.npmjs.org'

npmRegistryServer: "https://registry.npmjs.org"
npmRegistryServer: 'https://registry.npmjs.org'

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"
spec: '@yarnpkg/plugin-typescript'
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
spec: '@yarnpkg/plugin-interactive-tools'
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"
spec: '@yarnpkg/plugin-workspace-tools'
- sources/@repo/yarn-plugin-bud/bundles/@yarnpkg/plugin-bud.js
- sources/@repo/yarn-plugin-package/bundles/@yarnpkg/plugin-package.js

Expand Down
8 changes: 5 additions & 3 deletions examples/tailwindcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"extends @roots/browserslist-config/current"
],
"bud": {
"denylist": [
"@roots/bud-swc"
]
"extensions": {
"denylist": [
"@roots/bud-swc"
]
}
},
"devDependencies": {
"@roots/browserslist-config": "latest",
Expand Down
177 changes: 177 additions & 0 deletions sources/@roots/bud-hooks/src/async/async.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import {beforeEach, describe, expect, it, vi} from 'vitest'
import {factory, Bud} from '@repo/test-kit/bud'

import {AsyncHooks} from './async.js'

describe(`@roots/bud-hooks/async`, () => {
describe(`class`, () => {
let bud: Bud
let asyncHooks: AsyncHooks

beforeEach(async () => {
bud = await factory()
asyncHooks = new AsyncHooks(() => bud)
})

it(`should be instantiable`, () => {
expect(asyncHooks).toBeInstanceOf(AsyncHooks)
})

it(`should be instantiable`, () => {
asyncHooks.store = {
'build.plugins': [bud.value.make(async () => [])],
}
expect(asyncHooks.has(`build.plugins`)).toBe(true)

asyncHooks.store = {}
expect(asyncHooks.has(`build.plugins`)).toBe(false)
})
})

describe(`set`, () => {
let bud: Bud
let asyncHooks: AsyncHooks

beforeEach(async () => {
bud = await factory()
asyncHooks = new AsyncHooks(() => bud)
})

it(`should have a set method that sets a store value`, async () => {
asyncHooks.set(`build.plugins`, () => `bar`)
expect(asyncHooks.store[`build.plugins`]).toHaveLength(1)
})

it(`should have a set method that appends a new value onto existing store key`, async () => {
asyncHooks.set(`build.plugins`, () => `bar`)
asyncHooks.set(`build.plugins`, () => `baz2`)
expect(asyncHooks.store[`build.plugins`]).toHaveLength(2)
})

it(`should have a set method that replaces all existing values when the given is not a function`, async () => {
asyncHooks.set(`build.plugins`, () => `bar`)
asyncHooks.set(`build.plugins`, `baz2`)
expect(asyncHooks.store[`build.plugins`]).toHaveLength(1)
})

it(`should have a set method that calls has for id check`, async () => {
const spy = vi.spyOn(asyncHooks, `has`)
asyncHooks.set(`build.plugins`, [])
expect(spy).toHaveBeenCalledWith(`build.plugins`)
})

it(`should have a set method that accepts n-parameters`, async () => {
asyncHooks.set(
`build.plugins`,
() => [],
() => [],
() => [],
)
expect(asyncHooks.store[`build.plugins`]).toHaveLength(3)
})

it(`should have a set method that accepts n-parameters`, async () => {
const spy = vi.spyOn(asyncHooks, `has`)
asyncHooks.set(
`build.plugins`,
() => [],
() => [],
1,
)
expect(spy).toHaveBeenCalledWith(`build.plugins`)
expect(asyncHooks.store[`build.plugins`]).toHaveLength(1)
})

it(`should return bud`, async () => {
const returnValue = asyncHooks.set(`build.plugins`, [])
expect(returnValue).toBe(bud)
})
})

describe(`get`, () => {
let bud: Bud
let asyncHooks: AsyncHooks

beforeEach(async () => {
vi.clearAllMocks()
bud = await factory()
asyncHooks = new AsyncHooks(() => bud)
})

it(`should have a get method that gets a store value`, async () => {
const spy = vi.fn()
const callback = () => spy
asyncHooks.set(`build.plugins`, callback)
const value = await asyncHooks.get(`build.plugins`)
expect(value).toBe(spy)
})

it(`should have a get method that gets a store value`, async () => {
const spy1 = vi.fn(value => `1`)
const spy2 = vi.fn(value => `2`)

asyncHooks.set(`build.plugins`, () => spy1)
asyncHooks.set(`build.plugins`, () => spy2)

const value = await asyncHooks.get(`build.plugins`)
expect(value).toBe(spy2)
})

it(`should have a get method that returns fallback if no store value exists`, async () => {
const fallback = vi.fn(value => `1`)

// @ts-ignore
const value = await asyncHooks.get(`build.plugins`, fallback)
expect(value).toBe(fallback)
})

it(`should have a get method that returns fallback if no store value exists`, async () => {
const fallback = 1
const spy1 = vi.fn(value => 2)

asyncHooks.set(`build.plugins`, spy1)
const value = await asyncHooks.get(
`build.plugins`,
// @ts-ignore
fallback,
)
expect(spy1).toHaveBeenCalledWith(1)
expect(value).toBe(2)
})

it(`should have a get method that returns fallback if no store value exists`, async () => {
const existing = 2
const fallback = 1

asyncHooks.set(`build.plugins`, existing)
const value = await asyncHooks.get(
`build.plugins`,
// @ts-ignore
fallback,
)
expect(value).toBe(2)
})
})

describe(`setRecords`, () => {
let bud: Bud
let asyncHooks: AsyncHooks

beforeEach(async () => {
bud = await factory()
asyncHooks = new AsyncHooks(() => bud)
})

it(`should set store records`, async () => {
asyncHooks.setRecords({
// @ts-ignore
foo: 'bar',
bar: () => 'baz',
})
expect(asyncHooks.store['foo']).toHaveLength(1)
expect(asyncHooks.store['bar']).toHaveLength(1)
// @ts-ignore
expect(await asyncHooks.get(`foo`)).toBe(`bar`)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import type {
AsyncRegistry,
AsyncStore,
} from '@roots/bud-framework/registry'
import type Value from '@roots/bud-framework/value'
import {bind} from '@roots/bud-support/decorators'
import {isFunction, isUndefined} from '@roots/bud-support/lodash-es'
import {isFunction} from '@roots/bud-support/lodash-es'

import {Hooks} from './base.js'
import {Hooks} from '../base/base.js'

/**
* Asynchronous hooks registry
Expand All @@ -24,13 +25,16 @@ export class AsyncHooks extends Hooks<AsyncStore> {
@bind
public set<T extends keyof AsyncStore & string>(
id: T,
input: AsyncCallback[T],
...input: AsyncCallback[T]
): Bud {
if (this.has(id) && isFunction(input)) {
this.store[id].push(this.app.value.make(input))
} else {
this.store[id] = [this.app.value.make(input)]
}
if (!this.has(id)) this.store[id] = []

input
.map(this.app.value.make)
.map((value: Value<AsyncCallback[T]>) => {
if (typeof value.get() === `function`) this.store[id].push(value)
else this.store[id] = [value]
})

return this.app
}
Expand Down Expand Up @@ -58,15 +62,11 @@ export class AsyncHooks extends Hooks<AsyncStore> {
id: T,
fallback?: AsyncRegistry[T],
): Promise<AsyncRegistry[T]> {
if (isUndefined(this.store[id])) return fallback

const result = await this.store[id]
return await [this.app.value.make(fallback), ...(this.store[id] ?? [])]
.map(this.app.value.get)
.reduce(async (accumulated, current) => {
const thisValue = await accumulated
return isFunction(current) ? await current(thisValue) : current
}, fallback)

return result
const previous = await accumulated
return isFunction(current) ? await current(previous) : current
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import type {
EventsCallback,
EventsStore,
} from '@roots/bud-framework/registry'
import type Value from '@roots/bud-framework/value'
import {bind} from '@roots/bud-support/decorators'

import {Hooks} from './base.js'
import {Hooks} from '../base/base.js'

/**
* Synchronous hooks registry
Expand All @@ -27,9 +28,12 @@ export class EventHooks extends Hooks<EventsStore> {
id: T,
...input: Array<EventsCallback>
): Bud {
const value = input.map(this.app.value.make)
if (this.store[id]) this.store[id] = [...this.store[id], ...value]
else this.store[id] = value
if (!this.has(id)) this.store[id] = []

input
.map(this.app.value.make)
.map((value: Value<EventsCallback>) => this.store[id].push(value))

return this.app
}

Expand All @@ -50,15 +54,16 @@ export class EventHooks extends Hooks<EventsStore> {
): Promise<Bud> {
if (!this.has(id)) return this.app

await this.store[id]
const events = [...this.store[id]]
this.store[id] = []

await events
.map(this.app.value.get)
.reduce(async (promise, action) => {
await promise
await action(this.app)
}, Promise.resolve())

this.store[id] = []

return this.app
}
}
11 changes: 3 additions & 8 deletions sources/@roots/bud-hooks/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Bud, Hooks as Service, Service as Base} from '@roots/bud-framework'

import {AsyncHooks} from './async.js'
import {EventHooks} from './event.js'
import {SyncHooks} from './sync.js'
import {AsyncHooks} from './async/async.js'
import {EventHooks} from './event/event.js'
import {SyncHooks} from './sync/sync.js'

/**
* Hooks and events registry
Expand Down Expand Up @@ -50,11 +50,6 @@ import {SyncHooks} from './sync.js'
* @public
*/
export class Hooks extends Base implements Service {
/**
* Service label
*
* @public
*/
public static override label = `hooks`

public asyncStore: AsyncHooks
Expand Down
Loading