@cslug/core 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/api.md +6 -1
- package/package.json +2 -2
- package/src/application.ts +3 -1
- package/src/command.ts +40 -0
- package/src/events.ts +17 -0
- package/src/index.ts +4 -0
package/docs/api.md
CHANGED
|
@@ -55,6 +55,10 @@ export class XProvider extends ServiceProvider /* implements ProviderHooks */ {
|
|
|
55
55
|
The hooks and `dependsOn` are not declared on the base class, so apps using `noImplicitOverride` don't need
|
|
56
56
|
`override`. `ProviderHooks` gives the hook signatures for `implements`.
|
|
57
57
|
|
|
58
|
+
`defineGenerator({ kind, description, stubs, suffix?, files: (n) => [{ stub, target }], vars? })` makes a
|
|
59
|
+
`make:<kind>` command from a package's stub files (the app's `stubs/` win); `names(input, suffix)` gives the name's
|
|
60
|
+
parts (`class`, `file`, `kebab`, `snake`, `camel`, `base`, `dir`).
|
|
61
|
+
|
|
58
62
|
In `register()`, `this.commands([...])` contributes `cslug` commands (tag `COMMANDS`) and
|
|
59
63
|
`this.describe('routes', () => data)` a `cslug describe` section (tag `DESCRIBE`, `DescribeSection`).
|
|
60
64
|
|
|
@@ -168,7 +172,8 @@ Immutable; integer minor units (`bigint`) + ISO 4217 currency; no floating point
|
|
|
168
172
|
| `@listener(X \| [X, Y], { queued?, queue?, tries?, backoff? })` | Class listener with `handle(payload, event)`, built by the container (scoped). Combine with `@inject(...)` in any order |
|
|
169
173
|
| queued listeners | Run **after the transaction commits** (`afterCommit`), never on rollback. Without `@cslug/queue`: in-process in a fresh scope, errors logged. With it: `EventBus.dispatchQueued` sends them as jobs |
|
|
170
174
|
| `afterCommit(fn)` | Run once the current transaction commits; outside a transaction, soon after the current task. `@cslug/database` provides the hook (`TransactionHooks`) |
|
|
171
|
-
| `registeredListeners()` |
|
|
175
|
+
| `registeredListeners()` | The listener classes registered so far, by event |
|
|
176
|
+
| `loadListeners(rootDir)` | Imports `app/listeners/**` (not `*.test.ts`); `Application.start()` calls it before `boot()`, so `@listener` classes are registered in every process |
|
|
172
177
|
|
|
173
178
|
## Logger
|
|
174
179
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cslug/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Application, container and @inject, providers, config, events, logger, errors, context, Clock, Money, i18n, contracts.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,6 +28,6 @@
|
|
|
28
28
|
"bun": ">=1.3.14"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@cslug/validation": "0.
|
|
31
|
+
"@cslug/validation": "0.12.0"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/application.ts
CHANGED
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
} from './contracts'
|
|
17
17
|
import { Encrypter, parseKeys } from './encrypter'
|
|
18
18
|
import { ProviderError } from './errors'
|
|
19
|
-
import { EventBus } from './events'
|
|
19
|
+
import { EventBus, loadListeners } from './events'
|
|
20
20
|
import { type I18nConfig, Translator } from './i18n'
|
|
21
21
|
import { Logger, type LoggerOptions } from './logger'
|
|
22
22
|
import { dependenciesOf, type ProviderClass, type ProviderHooks, type ServiceProvider } from './provider'
|
|
@@ -145,6 +145,8 @@ export class Application extends Container {
|
|
|
145
145
|
this.registerProviders()
|
|
146
146
|
if (this.state !== 'registered') throw new ProviderError(`Cannot start an application that is ${this.state}.`)
|
|
147
147
|
this.state = 'starting'
|
|
148
|
+
// @listener classes register when their module is imported: do it here, so every process has them
|
|
149
|
+
await loadListeners(this.rootDir)
|
|
148
150
|
for (const p of this.instances_) {
|
|
149
151
|
try {
|
|
150
152
|
await p.boot?.()
|
package/src/command.ts
CHANGED
|
@@ -340,3 +340,43 @@ export const DESCRIBE = 'cslug.describe'
|
|
|
340
340
|
|
|
341
341
|
/** One section of `cslug describe`: JSON-ready data about the app (routes, tables, jobs…). */
|
|
342
342
|
export type DescribeSection = { name: string; collect: () => unknown }
|
|
343
|
+
|
|
344
|
+
export type Names = ReturnType<typeof names>
|
|
345
|
+
|
|
346
|
+
/** One file a generator writes: `stub` from the package's stubs folder (or the app's `stubs/`), to `target`. */
|
|
347
|
+
export type GeneratedFile = { stub: string; target: string }
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* A `make:<kind> Name` command for a package: `files(n)` says what to write, from `n` (the name's parts with
|
|
351
|
+
* `suffix`: `names(name, suffix)`); every `{{ var }}` of the stubs is filled from `n` and `vars(n)`. The app's
|
|
352
|
+
* `stubs/<stub>` wins over the package's. Never overwrites without `--force`; needs no booted app.
|
|
353
|
+
*
|
|
354
|
+
* ```ts
|
|
355
|
+
* defineGenerator({ kind: 'job', description: 'Create a job', stubs: STUBS,
|
|
356
|
+
* files: (n) => [{ stub: 'job.stub', target: `app/jobs/${n.file}.ts` }] })
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
export function defineGenerator(options: {
|
|
360
|
+
kind: string
|
|
361
|
+
description: string
|
|
362
|
+
stubs: string
|
|
363
|
+
/** The class name suffix (`job` → `SendCheckinJob`, `send-checkin-job.ts`); default `kind`, `''` for none. */
|
|
364
|
+
suffix?: string
|
|
365
|
+
files: (n: Names) => GeneratedFile[]
|
|
366
|
+
vars?: (n: Names) => Record<string, string>
|
|
367
|
+
}): CommandClass {
|
|
368
|
+
const args = { name: arg.string().describe('e.g. Claim, or admin/Claim'), force: flag.boolean() }
|
|
369
|
+
return class extends Command {
|
|
370
|
+
static signature = `make:${options.kind}`
|
|
371
|
+
static description = options.description
|
|
372
|
+
static boot = false
|
|
373
|
+
static args = args
|
|
374
|
+
handle({ name, force }: Command.Args<{ args: typeof args }>) {
|
|
375
|
+
const n = names(name, options.suffix ?? options.kind)
|
|
376
|
+
const vars = { ...n, ...options.vars?.(n) }
|
|
377
|
+
let ok = true
|
|
378
|
+
for (const file of options.files(n)) ok = this.generate(options.stubs, file.stub, file.target, vars, force) && ok
|
|
379
|
+
return ok ? 0 : 1
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
package/src/events.ts
CHANGED
|
@@ -162,3 +162,20 @@ export class EventBus {
|
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Import every module under `<root>/app/listeners/` (subfolders included, not `*.test.ts`), so their `@listener`
|
|
168
|
+
* classes are registered in every process: the web server, workers, commands and tests. `Application.start()`
|
|
169
|
+
* calls it; a folder that does not exist is fine. Returns how many files were imported.
|
|
170
|
+
*/
|
|
171
|
+
export async function loadListeners(rootDir: string): Promise<number> {
|
|
172
|
+
const { existsSync, readdirSync } = await import('node:fs')
|
|
173
|
+
const { join } = await import('node:path')
|
|
174
|
+
const dir = join(rootDir, 'app/listeners')
|
|
175
|
+
if (!existsSync(dir)) return 0
|
|
176
|
+
const files = (readdirSync(dir, { recursive: true }) as string[])
|
|
177
|
+
.filter((f) => f.endsWith('.ts') && !f.endsWith('.test.ts') && !f.endsWith('.d.ts'))
|
|
178
|
+
.sort()
|
|
179
|
+
for (const file of files) await import(join(dir, file))
|
|
180
|
+
return files.length
|
|
181
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -18,8 +18,11 @@ export {
|
|
|
18
18
|
ConsoleIO,
|
|
19
19
|
DESCRIBE,
|
|
20
20
|
type DescribeSection,
|
|
21
|
+
defineGenerator,
|
|
21
22
|
flag,
|
|
23
|
+
type GeneratedFile,
|
|
22
24
|
kebab,
|
|
25
|
+
type Names,
|
|
23
26
|
names,
|
|
24
27
|
parseArgs,
|
|
25
28
|
UsageError,
|
|
@@ -88,6 +91,7 @@ export {
|
|
|
88
91
|
type EventInstance,
|
|
89
92
|
type ListenerOptions,
|
|
90
93
|
listener,
|
|
94
|
+
loadListeners,
|
|
91
95
|
type QueuedListenerDispatcher,
|
|
92
96
|
registeredListeners,
|
|
93
97
|
} from './events'
|