@cslug/core 0.11.1 → 0.13.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 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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cslug/core",
3
- "version": "0.11.1",
3
+ "version": "0.13.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.11.1"
31
+ "@cslug/validation": "0.13.0"
32
32
  }
33
33
  }
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/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,