Skip to content

Commit

Permalink
🔨 improve(none): fix notifier bin path
Browse files Browse the repository at this point in the history
  • Loading branch information
kellymears committed Jun 4, 2024
1 parent 9e67052 commit 5d40f4e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 94 deletions.
44 changes: 22 additions & 22 deletions sources/@roots/blade-loader/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ const fixturePath = path(
)

describe(`@roots/blade-loader`, () => {
it(`should re-export @roots/blade-loader/plugin`, async () => {
const {name} = (await import(`../src/index.ts`)).default
expect(name).toBe(`BladeWebpackPlugin`)
})

it(`should re-export @roots/blade-loader/asset-loader`, async () => {
const {name} = (await import(`../src/index.ts`)).assetLoader
expect(name).toBe(`loader`)
})

it(`should re-export @roots/blade-loader/module-loader`, async () => {
const {name} = (await import(`../src/index.ts`)).moduleLoader
expect(name).toBe(`loader`)
})

describe(`@roots/blade-loader/plugin`, async () => {
let compiler: Compiler
let compilationStats: StatsCompilation | undefined
Expand Down Expand Up @@ -85,9 +100,9 @@ describe(`@roots/blade-loader`, () => {
expect(compilationStats?.assets).toHaveLength(2)
})

it(`should not use module-loader when extractScripts is false`, async () => {
it(`should use module-loader when extractScripts is undefined`, async () => {
const rules = []
const Plugin = new BladeWebpackPlugin({extractScripts: false})
const Plugin = new BladeWebpackPlugin()

await Plugin.apply({
hooks: {
Expand All @@ -106,16 +121,17 @@ describe(`@roots/blade-loader`, () => {
{
"test": /\\\\\\.php\\$/,
"use": [
"@roots/blade-loader/module-loader",
"@roots/blade-loader/asset-loader",
],
},
]
`)
})

it(`should use module-loader when extractScripts is undefined`, async () => {
it(`should use module-loader when extractScripts is true`, async () => {
const rules = []
const Plugin = new BladeWebpackPlugin()
const Plugin = new BladeWebpackPlugin({extractScripts: true})

await Plugin.apply({
hooks: {
Expand All @@ -142,9 +158,9 @@ describe(`@roots/blade-loader`, () => {
`)
})

it(`should use module-loader when extractScripts is true`, async () => {
it(`should not use module-loader when extractScripts is false`, async () => {
const rules = []
const Plugin = new BladeWebpackPlugin({extractScripts: true})
const Plugin = new BladeWebpackPlugin({extractScripts: false})

await Plugin.apply({
hooks: {
Expand All @@ -163,27 +179,11 @@ describe(`@roots/blade-loader`, () => {
{
"test": /\\\\\\.php\\$/,
"use": [
"@roots/blade-loader/module-loader",
"@roots/blade-loader/asset-loader",
],
},
]
`)
})
})

describe(`@roots/blade-loader`, async () => {
it(`should re-export @roots/blade-loader/plugin`, async () => {
const {name} = (await import(`../src/index.ts`)).default
expect(name).toBe(`BladeWebpackPlugin`)
})
it(`should re-export @roots/blade-loader/asset-loader`, async () => {
const {name} = (await import(`../src/index.ts`)).assetLoader
expect(name).toBe(`loader`)
})
it(`should re-export @roots/blade-loader/module-loader`, async () => {
const {name} = (await import(`../src/index.ts`)).moduleLoader
expect(name).toBe(`loader`)
})
})
})
100 changes: 53 additions & 47 deletions sources/@roots/bud-framework/src/bootstrap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,6 @@ export const lifecycle: Record<

export const services: Set<string> = new Set()

/**
* Define a filter function to validate services based on the current application context.
* This function returns true if the service is valid in the current context, false otherwise.
*
* @param app - Bud instance
* @returns filter function
*/
const filterServices =
(app: Bud) =>
(signifier: string): boolean => {
if (!isString(signifier)) return true

return (
(app.isDevelopment || !DEVELOPMENT_SERVICES.includes(signifier)) &&
(app.isRoot || !PARENT_SERVICES.includes(signifier))
)
}

/**
* Import and instantiate services, then bind them to the application context.
* If the service signifier is a string, it is imported dynamically; otherwise, it is used directly.
* Services are assigned a label based on their constructor name and added to the application's list of services.
*
* @param app - Bud instance
* @returns function to instantiate a service
*/
const instantiateServices =
(bud: Bud) =>
async (signifier: `${keyof Bud & string}`): Promise<void> => {
const Service = await bud.module.import(signifier).catch(bud.catch)

const value: BudService = new Service(() => bud)
const handle = (
value.constructor?.name
? camelCase(value.constructor.name)
: signifier
) as `${keyof Bud & string}`

bud.set(handle, new Service(() => bud), false)

services.add(handle)
}

/**
* Bootstrap the application. This involves the following steps:
*
Expand All @@ -113,15 +70,19 @@ export const bootstrap = async (bud: Bud) => {

bud.isRoot && (await bud.module.bootstrap(bud))

if (bud.isRoot) {
bud.set(`notifier`, new Notifier(() => bud), false)
await bud.notifier.make(bud)
} else {
bud.set(`notifier`, bud.root.notifier, false)
}

await Promise.all(
[...bud.context.services]
.filter(filterServices(bud))
.map(instantiateServices(bud)),
)

bud.notifier = new Notifier(() => bud)
await bud.notifier.make(bud)

bud.hooks
.fromMap({
'location.@dist': bud.context.paths.output,
Expand Down Expand Up @@ -176,7 +137,52 @@ export const bootstrap = async (bud: Bud) => {

bud.isRoot && bud.after(bud.module.after)

services.clear()
await bud.executeServiceCallbacks(`bootstrap`)
await bud.executeServiceCallbacks(`register`)
await bud.executeServiceCallbacks(`boot`)
await bud.executeServiceCallbacks(`config.before`)

return bud
}

/**
* Define a filter function to validate services based on the current application context.
* This function returns true if the service is valid in the current context, false otherwise.
*
* @param app - Bud instance
* @returns filter function
*/
const filterServices =
(app: Bud) =>
(signifier: string): boolean => {
if (!isString(signifier)) return true

return (
(app.isDevelopment || !DEVELOPMENT_SERVICES.includes(signifier)) &&
(app.isRoot || !PARENT_SERVICES.includes(signifier))
)
}

/**
* Import and instantiate services, then bind them to the application context.
* If the service signifier is a string, it is imported dynamically; otherwise, it is used directly.
* Services are assigned a label based on their constructor name and added to the application's list of services.
*
* @param app - Bud instance
* @returns function to instantiate a service
*/
const instantiateServices =
(bud: Bud) =>
async (signifier: `${keyof Bud & string}`): Promise<void> => {
const Service = await bud.module.import(signifier).catch(bud.catch)
const value = new Service(() => bud)
const handle = (
value.constructor?.name
? camelCase(value.constructor.name)
: signifier
) as `${keyof Bud & string}`

bud.set(handle, value, false)

services.add(handle)
}
37 changes: 12 additions & 25 deletions sources/@roots/bud-framework/src/bud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class Bud {
public async executeServiceCallbacks(
stage: `${keyof EventsStore & string}`,
): Promise<void> {
await this.resolvePromises().catch(this.catch)
await this.resolvePromises()
await this.hooks.fire(stage, this)
}

Expand All @@ -255,12 +255,7 @@ export class Bud {
public async initialize(context?: Context): Promise<Bud> {
if (context) this.context = {...(this.context ?? {}), ...context}

await bootstrap(this).catch(this.catch)

await this.bootstrap().catch(this.catch)
await this.register().catch(this.catch)
await this.boot().catch(this.catch)
await this.executeServiceCallbacks(`config.before`).catch(this.catch)
await bootstrap(this)

return this
}
Expand All @@ -286,11 +281,10 @@ export class Bud {
: {...this.context, ...request, root: this}

if (isUndefined(context.label)) {
return this.catch(
this.catch(
InputError.normalize(`bud.make: context.label must be a string`),
)
}

if (
!isUndefined(this.context.filter) &&
!this.context.filter.includes(context.label)
Expand All @@ -308,22 +302,23 @@ export class Bud {
if (!this.children) this.children = {}

if (this.children[context.label]) {
return this.catch(
this.catch(
InputError.normalize(
`bud.make: child instance ${context.label} already exists`,
),
)
}

logger.scope(`make`).log(`instantiating ${context.label}`)
logger.scope(`make`).log(`Instantiating ${context.label}`)

this.children[context.label] = new this.implementation()
await this.children[context.label].initialize({...context})

this.children[context.label] =
await new this.implementation().initialize({
...context,
})
if (setupFn) await setupFn(this.children[context.label])
if (setupFn) {
await setupFn(this.children[context.label])

await this.children[context.label].resolvePromises().catch(this.catch)
await this.children[context.label].resolvePromises()
}

if (typeof request !== `string` && request.dependsOn) {
this.get(context.label)?.hooks.on(
Expand Down Expand Up @@ -355,14 +350,6 @@ export class Bud {
}, Promise.resolve())
}

/**
* Register application services
*/
@bind
public async register() {
await this.executeServiceCallbacks(`register`).catch(this.catch)
}

/**
* Parent {@link Bud} instance
* @readonly
Expand Down
1 change: 1 addition & 0 deletions sources/@roots/bud-framework/src/notifier/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {open, openEditor} from '@roots/bud-support/open'
*/
const notifierPath = resolve(
dirname(fileURLToPath(import.meta.url)),
`..`, // lib
`..`, // bud-framework
`vendor`,
`mac.no-index`,
Expand Down

0 comments on commit 5d40f4e

Please sign in to comment.