breadc 0.8.1 → 0.8.2

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/dist/index.cjs CHANGED
@@ -8,44 +8,71 @@ function makePluginContainer(plugins = []) {
8
8
  const onPreCommand = {};
9
9
  const onPostCommand = {};
10
10
  for (const plugin of plugins) {
11
- for (const [key, fn] of Object.entries(plugin.onPreCommand ?? {})) {
11
+ if (typeof plugin.onPreCommand === "function") {
12
+ const key = "*";
12
13
  if (!(key in onPreCommand)) {
13
14
  onPreCommand[key] = [];
14
15
  }
15
- onPreCommand[key].push(fn);
16
+ onPreCommand[key].push(plugin.onPreCommand);
17
+ } else {
18
+ for (const [key, fn] of Object.entries(plugin.onPreCommand ?? {})) {
19
+ if (!(key in onPreCommand)) {
20
+ onPreCommand[key] = [];
21
+ }
22
+ onPreCommand[key].push(fn);
23
+ }
16
24
  }
17
- for (const [key, fn] of Object.entries(plugin.onPostCommand ?? {})) {
25
+ if (typeof plugin.onPostCommand === "function") {
26
+ const key = "*";
18
27
  if (!(key in onPostCommand)) {
19
28
  onPostCommand[key] = [];
20
29
  }
21
- onPostCommand[key].push(fn);
30
+ onPostCommand[key].push(plugin.onPostCommand);
31
+ } else {
32
+ for (const [key, fn] of Object.entries(plugin.onPostCommand ?? {})) {
33
+ if (!(key in onPostCommand)) {
34
+ onPostCommand[key] = [];
35
+ }
36
+ onPostCommand[key].push(fn);
37
+ }
22
38
  }
23
39
  }
24
- const run = async (container, command) => {
40
+ const run = async (container, command, result) => {
25
41
  const prefix = command._arguments.filter((a) => a.type === "const").map((a) => a.name);
42
+ if (prefix.length === 0) {
43
+ prefix.push("_");
44
+ }
26
45
  for (let i = 0; i <= prefix.length; i++) {
27
46
  const key = i === 0 ? "*" : prefix.slice(0, i).map(
28
47
  (t, idx) => idx === 0 ? t : t[0].toUpperCase() + t.slice(1)
29
48
  ).join("");
30
49
  const fns = container[key];
31
50
  if (fns && fns.length > 0) {
32
- await Promise.all(fns.map((fn) => fn()));
51
+ await Promise.all(fns.map((fn) => fn(result)));
33
52
  }
34
53
  }
35
54
  };
36
55
  return {
37
56
  async preRun(breadc) {
57
+ if (plugins.length === 0)
58
+ return;
38
59
  for (const p of plugins) {
39
60
  await p.onPreRun?.(breadc);
40
61
  }
41
62
  },
42
- async preCommand(command) {
43
- await run(onPreCommand, command);
63
+ async preCommand(command, result) {
64
+ if (plugins.length === 0)
65
+ return;
66
+ await run(onPreCommand, command, result);
44
67
  },
45
- async postCommand(command) {
46
- await run(onPostCommand, command);
68
+ async postCommand(command, result) {
69
+ if (plugins.length === 0)
70
+ return;
71
+ await run(onPostCommand, command, result);
47
72
  },
48
73
  async postRun(breadc) {
74
+ if (plugins.length === 0)
75
+ return;
49
76
  for (const p of plugins) {
50
77
  await p.onPostRun?.(breadc);
51
78
  }
@@ -329,10 +356,13 @@ function makeCommand(format, config, root, container) {
329
356
  return command;
330
357
  },
331
358
  action(fn) {
332
- command.callback = async (...args2) => {
333
- await container.preCommand(command);
334
- const result = await fn(...args2);
335
- await container.postCommand(command);
359
+ command.callback = async (parsed) => {
360
+ await container.preCommand(command, parsed);
361
+ const result = await fn(...parsed.arguments, {
362
+ ...parsed.options,
363
+ "--": parsed["--"]
364
+ });
365
+ await container.postCommand(command, parsed);
336
366
  return result;
337
367
  };
338
368
  }
@@ -452,7 +482,7 @@ function makeCommand(format, config, root, container) {
452
482
  }
453
483
  function makeVersionCommand(name, config) {
454
484
  const command = {
455
- callback() {
485
+ async callback() {
456
486
  const text = `${name}/${config.version ? config.version : "unknown"}`;
457
487
  console.log(text);
458
488
  return text;
@@ -527,9 +557,9 @@ function makeHelpCommand(name, config) {
527
557
  return commands;
528
558
  }
529
559
  const command = {
530
- callback(option2) {
531
- const context = option2.__context__;
532
- const cursor = option2.__cursor__;
560
+ async callback(parsed) {
561
+ const context = parsed.options.__context__;
562
+ const cursor = parsed.options.__cursor__;
533
563
  const output = [
534
564
  `${name}/${config.version ? config.version : "unknown"}`,
535
565
  () => {
@@ -642,10 +672,7 @@ function breadc(name, config = {}) {
642
672
  if (command) {
643
673
  if (command.callback) {
644
674
  await container.preRun(breadc2);
645
- const r = command.callback(...result.arguments, {
646
- ...result.options,
647
- "--": result["--"]
648
- });
675
+ const r = await command.callback(result);
649
676
  await container.postRun(breadc2);
650
677
  return r;
651
678
  }
package/dist/index.d.ts CHANGED
@@ -101,7 +101,7 @@ interface Breadc<GlobalOption extends object = {}> {
101
101
  run<T = any>(args: string[]): Promise<T>;
102
102
  }
103
103
  interface Command<F extends string = string, AT extends any[] = ExtractCommand<F>, CommandOption extends object = {}, GlobalOption extends object = {}> {
104
- callback?: ActionFn<ExtractCommand<F>, CommandOption & GlobalOption>;
104
+ callback?: (result: ParseResult) => Promise<any>;
105
105
  format: F;
106
106
  description: string;
107
107
  _arguments: Argument[];
@@ -133,10 +133,11 @@ interface OptionOption<T extends string | boolean, R extends any = T> {
133
133
  default?: T;
134
134
  cast?: (value: T) => R;
135
135
  }
136
+ type CommandHookFn = (result: ParseResult) => void | Promise<void>;
136
137
  interface Plugin {
137
138
  onPreRun(breadc: Breadc): void | Promise<void>;
138
- onPreCommand: Record<string, () => void | Promise<void>>;
139
- onPostCommand: Record<string, () => void | Promise<void>>;
139
+ onPreCommand: Record<string, CommandHookFn> | CommandHookFn;
140
+ onPostCommand: Record<string, CommandHookFn> | CommandHookFn;
140
141
  onPostRun(breadc: Breadc): void | Promise<void>;
141
142
  }
142
143
 
package/dist/index.mjs CHANGED
@@ -4,44 +4,71 @@ function makePluginContainer(plugins = []) {
4
4
  const onPreCommand = {};
5
5
  const onPostCommand = {};
6
6
  for (const plugin of plugins) {
7
- for (const [key, fn] of Object.entries(plugin.onPreCommand ?? {})) {
7
+ if (typeof plugin.onPreCommand === "function") {
8
+ const key = "*";
8
9
  if (!(key in onPreCommand)) {
9
10
  onPreCommand[key] = [];
10
11
  }
11
- onPreCommand[key].push(fn);
12
+ onPreCommand[key].push(plugin.onPreCommand);
13
+ } else {
14
+ for (const [key, fn] of Object.entries(plugin.onPreCommand ?? {})) {
15
+ if (!(key in onPreCommand)) {
16
+ onPreCommand[key] = [];
17
+ }
18
+ onPreCommand[key].push(fn);
19
+ }
12
20
  }
13
- for (const [key, fn] of Object.entries(plugin.onPostCommand ?? {})) {
21
+ if (typeof plugin.onPostCommand === "function") {
22
+ const key = "*";
14
23
  if (!(key in onPostCommand)) {
15
24
  onPostCommand[key] = [];
16
25
  }
17
- onPostCommand[key].push(fn);
26
+ onPostCommand[key].push(plugin.onPostCommand);
27
+ } else {
28
+ for (const [key, fn] of Object.entries(plugin.onPostCommand ?? {})) {
29
+ if (!(key in onPostCommand)) {
30
+ onPostCommand[key] = [];
31
+ }
32
+ onPostCommand[key].push(fn);
33
+ }
18
34
  }
19
35
  }
20
- const run = async (container, command) => {
36
+ const run = async (container, command, result) => {
21
37
  const prefix = command._arguments.filter((a) => a.type === "const").map((a) => a.name);
38
+ if (prefix.length === 0) {
39
+ prefix.push("_");
40
+ }
22
41
  for (let i = 0; i <= prefix.length; i++) {
23
42
  const key = i === 0 ? "*" : prefix.slice(0, i).map(
24
43
  (t, idx) => idx === 0 ? t : t[0].toUpperCase() + t.slice(1)
25
44
  ).join("");
26
45
  const fns = container[key];
27
46
  if (fns && fns.length > 0) {
28
- await Promise.all(fns.map((fn) => fn()));
47
+ await Promise.all(fns.map((fn) => fn(result)));
29
48
  }
30
49
  }
31
50
  };
32
51
  return {
33
52
  async preRun(breadc) {
53
+ if (plugins.length === 0)
54
+ return;
34
55
  for (const p of plugins) {
35
56
  await p.onPreRun?.(breadc);
36
57
  }
37
58
  },
38
- async preCommand(command) {
39
- await run(onPreCommand, command);
59
+ async preCommand(command, result) {
60
+ if (plugins.length === 0)
61
+ return;
62
+ await run(onPreCommand, command, result);
40
63
  },
41
- async postCommand(command) {
42
- await run(onPostCommand, command);
64
+ async postCommand(command, result) {
65
+ if (plugins.length === 0)
66
+ return;
67
+ await run(onPostCommand, command, result);
43
68
  },
44
69
  async postRun(breadc) {
70
+ if (plugins.length === 0)
71
+ return;
45
72
  for (const p of plugins) {
46
73
  await p.onPostRun?.(breadc);
47
74
  }
@@ -325,10 +352,13 @@ function makeCommand(format, config, root, container) {
325
352
  return command;
326
353
  },
327
354
  action(fn) {
328
- command.callback = async (...args2) => {
329
- await container.preCommand(command);
330
- const result = await fn(...args2);
331
- await container.postCommand(command);
355
+ command.callback = async (parsed) => {
356
+ await container.preCommand(command, parsed);
357
+ const result = await fn(...parsed.arguments, {
358
+ ...parsed.options,
359
+ "--": parsed["--"]
360
+ });
361
+ await container.postCommand(command, parsed);
332
362
  return result;
333
363
  };
334
364
  }
@@ -448,7 +478,7 @@ function makeCommand(format, config, root, container) {
448
478
  }
449
479
  function makeVersionCommand(name, config) {
450
480
  const command = {
451
- callback() {
481
+ async callback() {
452
482
  const text = `${name}/${config.version ? config.version : "unknown"}`;
453
483
  console.log(text);
454
484
  return text;
@@ -523,9 +553,9 @@ function makeHelpCommand(name, config) {
523
553
  return commands;
524
554
  }
525
555
  const command = {
526
- callback(option2) {
527
- const context = option2.__context__;
528
- const cursor = option2.__cursor__;
556
+ async callback(parsed) {
557
+ const context = parsed.options.__context__;
558
+ const cursor = parsed.options.__cursor__;
529
559
  const output = [
530
560
  `${name}/${config.version ? config.version : "unknown"}`,
531
561
  () => {
@@ -638,10 +668,7 @@ function breadc(name, config = {}) {
638
668
  if (command) {
639
669
  if (command.callback) {
640
670
  await container.preRun(breadc2);
641
- const r = command.callback(...result.arguments, {
642
- ...result.options,
643
- "--": result["--"]
644
- });
671
+ const r = await command.callback(result);
645
672
  await container.postRun(breadc2);
646
673
  return r;
647
674
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "breadc",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "Yet another Command Line Application Framework with fully strong TypeScript support",
5
5
  "keywords": [
6
6
  "breadc",
@@ -35,7 +35,7 @@
35
35
  "dist"
36
36
  ],
37
37
  "dependencies": {
38
- "@breadc/color": "0.8.1"
38
+ "@breadc/color": "0.8.2"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^18.11.18",