@hasna/events 0.1.3 → 0.1.5

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.
@@ -1,2 +1,6 @@
1
1
  #!/usr/bin/env bun
2
- export {};
2
+ export interface RunEventsCliOptions {
3
+ programName?: string;
4
+ source?: string;
5
+ }
6
+ export declare function runEventsCli(argv?: string[], options?: RunEventsCliOptions): Promise<void>;
package/dist/cli/index.js CHANGED
@@ -631,26 +631,72 @@ function output(parsed, value, human) {
631
631
  }
632
632
  human();
633
633
  }
634
- function printHelp() {
635
- console.log(`events ${version()}
634
+ function commandName(options) {
635
+ return options.programName ?? "events";
636
+ }
637
+ function printHelp(options = {}) {
638
+ const name = commandName(options);
639
+ console.log(`${name} ${version()}
636
640
 
637
641
  Usage:
638
- events [--dir <path>] [--json] webhooks add <url|command> [options]
639
- events [--dir <path>] [--json] webhooks list
640
- events [--dir <path>] [--json] webhooks remove <id>
641
- events [--dir <path>] [--json] webhooks test <id>
642
- events [--dir <path>] [--json] events emit <type> --source <source> [options]
643
- events [--dir <path>] [--json] events list [--limit <n>]
644
- events [--dir <path>] [--json] events replay [--id <event-id>] [--dry-run]
642
+ ${name} [--dir <path>] [--json] webhooks add <url|command> [options]
643
+ ${name} [--dir <path>] [--json] webhooks list
644
+ ${name} [--dir <path>] [--json] webhooks remove <id>
645
+ ${name} [--dir <path>] [--json] webhooks test <id>
646
+ ${name} [--dir <path>] [--json] events emit <type>${options.source ? "" : " --source <source>"} [options]
647
+ ${name} [--dir <path>] [--json] events list [--limit <n>]
648
+ ${name} [--dir <path>] [--json] events replay [--id <event-id>] [--dry-run]
645
649
 
646
650
  Environment:
647
651
  HASNA_EVENTS_DIR or HASNA_EVENTS_HOME overrides the default ${getEventsDataDir()}`);
648
652
  }
649
- async function main(argv = process.argv.slice(2)) {
653
+ function printWebhooksHelp(options = {}) {
654
+ const name = commandName(options);
655
+ console.log(`${name} webhooks
656
+
657
+ Usage:
658
+ ${name} [--dir <path>] [--json] webhooks add <url|command> [options]
659
+ ${name} [--dir <path>] [--json] webhooks list
660
+ ${name} [--dir <path>] [--json] webhooks remove <id>
661
+ ${name} [--dir <path>] [--json] webhooks test <id>
662
+
663
+ Options:
664
+ --id <id> Channel id for add
665
+ --type <pattern> Event type filter, supports wildcards
666
+ --source <source> Event source filter
667
+ --subject <subject> Event subject filter
668
+ --severity <severity> Event severity filter
669
+ --transport <kind> webhook or command
670
+ --secret <secret> Webhook signing secret
671
+ --header <name=value> Webhook header, repeatable
672
+ --redact <path> Redaction path, repeatable
673
+ --no-deliver Available on events emit`);
674
+ }
675
+ function printEventsHelp(options = {}) {
676
+ const name = commandName(options);
677
+ console.log(`${name} events
678
+
679
+ Usage:
680
+ ${name} [--dir <path>] [--json] events emit <type>${options.source ? "" : " --source <source>"} [options]
681
+ ${name} [--dir <path>] [--json] events list [--limit <n>]
682
+ ${name} [--dir <path>] [--json] events replay [--id <event-id>] [--dry-run]
683
+
684
+ Options:
685
+ --source <source> Event source${options.source ? ` (default: ${options.source})` : ""}
686
+ --subject <subject> Event subject
687
+ --severity <severity> Event severity
688
+ --message <message> Human-readable event message
689
+ --dedupe-key <key> Deduplicate repeated events
690
+ --data <json> JSON object payload
691
+ --metadata <json> JSON object metadata
692
+ --no-deliver Record without delivering webhooks
693
+ --dry-run Preview replay matches without delivery`);
694
+ }
695
+ async function runEventsCli(argv = process.argv.slice(2), options = {}) {
650
696
  const parsed = parseGlobalArgs(argv);
651
697
  const [group, command, ...tail] = parsed.rest;
652
698
  if (!group || group === "--help" || group === "-h") {
653
- printHelp();
699
+ printHelp(options);
654
700
  return;
655
701
  }
656
702
  if (group === "--version" || group === "-v") {
@@ -660,16 +706,24 @@ async function main(argv = process.argv.slice(2)) {
660
706
  const store = new JsonEventsStore(parsed.dir);
661
707
  const client = new EventsClient({ store });
662
708
  if (group === "webhooks") {
663
- await handleWebhooks(client, command, tail, parsed);
709
+ if (!command || command === "--help" || command === "-h") {
710
+ printWebhooksHelp(options);
711
+ return;
712
+ }
713
+ await handleWebhooks(client, command, tail, parsed, options);
664
714
  return;
665
715
  }
666
716
  if (group === "events") {
667
- await handleEvents(client, command, tail, parsed);
717
+ if (!command || command === "--help" || command === "-h") {
718
+ printEventsHelp(options);
719
+ return;
720
+ }
721
+ await handleEvents(client, command, tail, parsed, options);
668
722
  return;
669
723
  }
670
724
  throw new Error(`Unknown command group: ${group}`);
671
725
  }
672
- async function handleWebhooks(client, command, tail, parsed) {
726
+ async function handleWebhooks(client, command, tail, parsed, options) {
673
727
  if (command === "add") {
674
728
  const args = [...tail];
675
729
  const transport = takeOption(args, "--transport") ?? "webhook";
@@ -737,7 +791,7 @@ async function handleWebhooks(client, command, tail, parsed) {
737
791
  if (!id)
738
792
  throw new Error("webhooks test requires a channel id");
739
793
  const result = await client.testChannel(id, {
740
- source: takeOption(args, "--source") ?? "hasna.events",
794
+ source: takeOption(args, "--source") ?? options.source ?? "hasna.events",
741
795
  type: takeOption(args, "--type") ?? "events.test",
742
796
  subject: takeOption(args, "--subject") ?? id,
743
797
  data: parseJsonOption(takeOption(args, "--data"), { test: true })
@@ -747,13 +801,13 @@ async function handleWebhooks(client, command, tail, parsed) {
747
801
  }
748
802
  throw new Error(`Unknown webhooks command: ${command ?? ""}`);
749
803
  }
750
- async function handleEvents(client, command, tail, parsed) {
804
+ async function handleEvents(client, command, tail, parsed, options) {
751
805
  if (command === "emit") {
752
806
  const args = [...tail];
753
807
  const type = args.shift();
754
808
  if (!type)
755
809
  throw new Error("events emit requires an event type");
756
- const source = takeOption(args, "--source");
810
+ const source = takeOption(args, "--source") ?? options.source;
757
811
  if (!source)
758
812
  throw new Error("events emit requires --source");
759
813
  const noDeliver = takeFlag(args, "--no-deliver");
@@ -822,13 +876,18 @@ function severityOption(value) {
822
876
  throw new Error(`Invalid severity: ${value}`);
823
877
  return value;
824
878
  }
825
- main().catch((error) => {
826
- const parsed = parseGlobalArgs(process.argv.slice(2));
827
- const message = error instanceof Error ? error.message : String(error);
828
- if (parsed.json) {
829
- console.log(JSON.stringify({ error: message }, null, 2));
830
- } else {
831
- console.error(message);
832
- }
833
- process.exit(1);
834
- });
879
+ if (import.meta.main) {
880
+ runEventsCli().catch((error) => {
881
+ const parsed = parseGlobalArgs(process.argv.slice(2));
882
+ const message = error instanceof Error ? error.message : String(error);
883
+ if (parsed.json) {
884
+ console.log(JSON.stringify({ error: message }, null, 2));
885
+ } else {
886
+ console.error(message);
887
+ }
888
+ process.exit(1);
889
+ });
890
+ }
891
+ export {
892
+ runEventsCli
893
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/events",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Shared event envelopes, local subscriptions, and webhook delivery for Hasna open-source apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -33,6 +33,10 @@
33
33
  "./commander": {
34
34
  "types": "./dist/commander.d.ts",
35
35
  "import": "./dist/commander.js"
36
+ },
37
+ "./cli": {
38
+ "types": "./dist/cli/index.d.ts",
39
+ "import": "./dist/cli/index.js"
36
40
  }
37
41
  },
38
42
  "files": [