@@ -54,13 +54,22 @@ A task handler should have the following structure:
54
54
- ` name ` : The name of the task, this is optional. We'll use the file name or package name as the task name if not defined.
55
55
- ` description ` : A brief description of the task, this is optional.
56
56
- ` version ` : The version of the task, this is optional. We'll use the package version if not defined.
57
- - ` parameters ` : An object that describes the input parameters of the task for showing help.
58
- - ` handle ` : The function that executes the task.
57
+ - ` parameters ` : An object that describes the input parameters of the task for showing help. (calls ` Schema.describe() ` from Joi)
58
+ - ` handle ` : The function that executes the task. This is required.
59
+
60
+ There are alternative ways to define those fields by using these corresponding decorators:
61
+
62
+ - ` @Name ` : The name of the task.
63
+ - ` @Description ` : A brief description of the task.
64
+ - ` @Version ` : The version of the task.
65
+ - ` @Parameters ` : An object that describes the input parameters of the task for showing help.
66
+
67
+ > _ Vanilla JS_ does not support decorators yet, so you need to use Babel or TypeScript to work with them.
59
68
60
69
This is an example of a custom task:
61
70
62
71
``` ts
63
- import { validateParameters } from ' @letrun/core' ;
72
+ import { validateParameters , Name , Parameters } from ' @letrun/core' ;
64
73
import { TaskHandler } from ' @letrun/common' ;
65
74
import Joi from ' joi' ;
66
75
@@ -72,10 +81,9 @@ const Schema = Joi.object<TaskParameters>({
72
81
message: Joi .string ().description (' The message to say greeting' ).required (),
73
82
});
74
83
75
- export default class GreatingTaskHandler implements TaskHandler {
76
- name = ' greeting' ;
77
- parameters = Schema .describe ();
78
-
84
+ @Name (' greeting' )
85
+ @Parameters (Schema )
86
+ export default class GreetingTaskHandler implements TaskHandler {
79
87
async handle({ task }: TaskHandlerInput ) {
80
88
const { message } = validateParameters (task .parameters , Schema );
81
89
const effectiveMessage = ` Hello, ${message }! ` ;
@@ -88,7 +96,7 @@ export default class GreatingTaskHandler implements TaskHandler {
88
96
Here is another example that will uppercase the input string:
89
97
90
98
``` ts
91
- import { validateParameters } from ' @letrun/core' ;
99
+ import { validateParameters , Name , Parameters } from ' @letrun/core' ;
92
100
import { TaskHandler } from ' @letrun/common' ;
93
101
import Joi from ' joi' ;
94
102
@@ -100,10 +108,9 @@ const Schema = Joi.object<TaskParameters>({
100
108
value: Joi .string ().description (' The string to uppercase' ).required (),
101
109
});
102
110
111
+ @Name (' uppercase' )
112
+ @Parameters (Schema )
103
113
export default class UppercaseTaskHandler implements TaskHandler {
104
- name = ' uppercase' ;
105
- parameters = Schema .describe ();
106
-
107
114
async handle({ task }: TaskHandlerInput ) {
108
115
const { value } = validateParameters (task .parameters , Schema );
109
116
return value .toUpperCase ();
0 commit comments