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): cli and logging #2481

Merged
merged 1 commit into from
Oct 20, 2023
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
5 changes: 4 additions & 1 deletion sources/@roots/bud-api/src/methods/minimize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export const minimize: minimize = function (this: Bud, value = true) {
return this
}

value.map(key => this.minify[key].enable(true))
value.map(key => {
this.minify[key].enable(true)
})

return this
}
4 changes: 2 additions & 2 deletions sources/@roots/bud-api/src/methods/persist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export interface persist {
export const persist: persist = function (this: Bud, type = `filesystem`) {
if (type === false) {
this.cache.enabled = false
this.success(`cache disabled`)
this.api.logger.success(`cache disabled`)
return this
}

this.cache.enabled = true
this.cache.type = isString(type) ? type : `filesystem`

this.success(`cache enabled`)
this.api.logger.success(`cache enabled`)

return this
}
6 changes: 3 additions & 3 deletions sources/@roots/bud-api/test/persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ describe(`bud.persist`, () => {
})

it(`should call bud.success to log param`, () => {
const successSpy = vi.spyOn(bud, `success`)
const successSpy = vi.spyOn(bud.api.logger, `success`)
subject()
expect(successSpy).toHaveBeenCalledWith(`cache enabled`)
})

it(`should call bud.success to log param`, () => {
const successSpy = vi.spyOn(bud, `success`)
const successSpy = vi.spyOn(bud.api.logger, `success`)
subject(true)
expect(successSpy).toHaveBeenCalledWith(`cache enabled`)
})

it(`should call bud.success to log param`, () => {
const successSpy = vi.spyOn(bud, `success`)
const successSpy = vi.spyOn(bud.api.logger, `success`)
subject(false)
expect(successSpy).toHaveBeenCalledWith(`cache disabled`)
})
Expand Down
4 changes: 3 additions & 1 deletion sources/@roots/bud-build/src/config/entry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type {Factory} from '@roots/bud-build/config'

export const entry: Factory<`entry`> = async ({hooks}) => {
const entrypoints = hooks.filter(`build.entry`, {main: {import: [`index`]}})
const entrypoints = hooks.filter(`build.entry`, {
main: {import: [`index`]},
})

return Object.entries(entrypoints).reduce((acc, [key, value]) => {
value.import = [...new Set(value.import)]
Expand Down
3 changes: 2 additions & 1 deletion sources/@roots/bud-build/src/rule/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Registrable from '@roots/bud-build/helpers/registrable'
import {bind} from '@roots/bud-support/decorators/bind'
import isFunction from '@roots/bud-support/lodash/isFunction'
import isString from '@roots/bud-support/lodash/isString'
import logger from '@roots/bud-support/logger'

/**
* RuleSetRule
Expand Down Expand Up @@ -273,7 +274,7 @@ class Rule extends Registrable implements Interface {
return {...a, [k]: v}
}, {})

this.app.info(`built rule`, output)
logger.info(`built rule`, output)

return output
}
Expand Down
10 changes: 5 additions & 5 deletions sources/@roots/bud-build/src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ class Build extends Service implements BudBuild {
if (isUndefined(value)) return

this.config[prop] = value
this.logger.success(`built`, prop)
this.logger.log(`built`, prop)
} catch (error) {
throw error
}
}),
),
)

this.logger.success(`configuration successfully built`)
this.logger.log(`configuration successfully built`)
this.logger.info(this.config)
await this.app.hooks.fire(`build.after`, this.app)

Expand Down Expand Up @@ -157,7 +157,7 @@ class Build extends Service implements BudBuild {
: this.makeItem(maybeOptionsCallback)

this.items[ident] = item
this.logger.info(`set item`, item)
this.logger.info(item)

return this
}
Expand All @@ -177,7 +177,7 @@ class Build extends Service implements BudBuild {
: this.makeLoader(definition)

this.loaders[name] = loader
this.logger.info(`set loader`, loader)
this.logger.info(loader)

return this
}
Expand All @@ -198,7 +198,7 @@ class Build extends Service implements BudBuild {
: this.makeRule(definition as any)

this.rules[name] = rule
this.logger.info(`set rule`, rule)
this.logger.info(rule)

return this
}
Expand Down
1 change: 0 additions & 1 deletion sources/@roots/bud-compiler/src/service/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class Compiler extends Service implements BudCompiler {
})

this.logger.timeEnd(`initialize`)
this.app.dashboard.updateStatus(`compiling`)

try {
this.instance = this.implementation(this.config)
Expand Down
22 changes: 2 additions & 20 deletions sources/@roots/bud-dashboard/src/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Dashboard extends Service implements BudDashboard {
public declare status?: false | string

/**
* {@link BudDashboard.stdout}
* {@link BudDashboard.stderr}
*/
public stderr = stderr

Expand All @@ -69,7 +69,7 @@ export class Dashboard extends Service implements BudDashboard {
public stdin = stdin

/**
* {@link BudDashboard.stderr}
* {@link BudDashboard.stdout}
*/
public stdout = stdout

Expand All @@ -85,7 +85,6 @@ export class Dashboard extends Service implements BudDashboard {
this.stderr = this.app.context.stderr ?? stderr

this.formatStatsErrors = makeErrorFormatter(this.app)
this.updateStatus(`Initializing`)
this.render()
}

Expand Down Expand Up @@ -277,21 +276,4 @@ export class Dashboard extends Service implements BudDashboard {
this.render()
return this
}

/**
* {@link BudDashboard.updateStatus}
*/
@bind
public updateStatus(status: string): BudDashboard {
/**
* Update the status prop
*/
this.status = status

/**
* Render or re-render the application
*/
this.render()
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class BudEslintCommand extends BudCommand {
examples: [[`Run eslint on source files`, `$0 eslint`]],
})


public options = Option.Proxy({name: `eslint passthrough options`})

/**
Expand All @@ -29,12 +28,16 @@ export class BudEslintCommand extends BudCommand {
await this.makeBud()
await this.bud.run()

const eslintrc = Object.values(this.bud.context.files).find((file) => file.name.includes(`eslintrc`) || file.name.includes(`eslint.config`))?.path
const eslintrc = Object.values(this.bud.context.files).find(
file =>
file.name.includes(`eslintrc`) ||
file.name.includes(`eslint.config`),
)?.path

await this.run([`eslint`, `bin`, `eslint.js`], this.options, [
`--ext`,
`.js,.jsx,.ts,.tsx`,
...(eslintrc ? [`--config`, eslintrc]: []),
...(eslintrc ? [`--config`, eslintrc] : []),
this.bud.relPath(`@src`),
])
}
Expand Down
4 changes: 2 additions & 2 deletions sources/@roots/bud-extensions/src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ class Extensions extends Service implements BudExtensions {
@bind
public set(value: Extension): this {
const key = (value.label ?? randomUUID()) as any
this.repository[key] = value
this.logger.success(`set`, key)
Object.assign(this.repository, {[key]: value})
this.logger.info(`set`, key, `=>`, value)

return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import {bind, label} from '@roots/bud-framework/extension/decorators'
*/
@label(`@roots/bud-extensions/webpack-lifecycle-plugin`)
export default class BudWebpackLifecyclePlugin extends Extension {
@bind
public afterCompile(compilation: Compilation) {
this.logger.log(`compilation completed:`, compilation.hash)
this.logger.timeEnd(`compile`)
}

/**
* {@link Extension.apply}
* {@link WebpackPluginInstance.apply}
Expand Down Expand Up @@ -66,11 +60,23 @@ export default class BudWebpackLifecyclePlugin extends Extension {
)
}

/**
* Before compile hook
*/
@bind
public beforeCompile(compilation: Compilation) {
this.logger.time(`compile`)
}

/**
* After compile hook
*/
@bind
public afterCompile(compilation: Compilation) {
this.logger.log(`compilation completed:`, compilation.hash)
this.logger.timeEnd(`compile`)
}

@bind
public emit(compilation: Compilation) {
this.logger.time(`emit`)
Expand Down
38 changes: 17 additions & 21 deletions sources/@roots/bud-framework/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export const lifecycle = {
'server.before': `serverBefore`,
}

export const services: Array<string> = []

/**
* 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.
Expand Down Expand Up @@ -108,31 +110,23 @@ const instantiateServices =
throw error instanceof BudError ? BudError.normalize(error) : error
})

let service: BudService

try {
service = new Service(() => app)
} catch (error) {
const normalError =
error instanceof BudError ? BudError.normalize(error) : error
normalError.message = `Error instantiating service ${signifier}: ${normalError.message}`
return normalError
}

const value: BudService = new Service(() => app)
const label =
service.label ?? service.constructor?.name
? camelCase(service.constructor.name)
value.label ?? value.constructor?.name
? camelCase(value.constructor.name)
: signifier

app[label] = service
Object.defineProperties(app, {
[label]: {
configurable: true,
value,
writable: true,
},
})

logger.log(
chalk.blue(`bud.${label}`),
figures.arrowLeft,
chalk.cyan(!isString(signifier) ? `[object]` : signifier),
)
logger.log(chalk.blue(label), figures.arrowLeft, chalk.cyan(signifier))

app.services.push(label)
services.push(label)
}

/**
Expand Down Expand Up @@ -209,7 +203,7 @@ export const bootstrap = async function (bud: Bud) {
)

Object.entries(lifecycle).map(([eventHandle, callbackName]) =>
[...bud.services]
[...services]
.map(service => bud[service])
.filter(Boolean)
.filter(instance => callbackName in instance)
Expand Down Expand Up @@ -242,4 +236,6 @@ export const bootstrap = async function (bud: Bud) {
)

bud.after(bud.module.after)

return bud
}
Loading
Loading