Skip to content

Commit 7788bf7

Browse files
committed
Update docs
1 parent 7c77707 commit 7788bf7

File tree

5 files changed

+164
-69
lines changed

5 files changed

+164
-69
lines changed

docs/plugin/module-location-resolver.md

Lines changed: 0 additions & 65 deletions
This file was deleted.

docs/plugin/plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ Here are supported plugin types:
8585
10. [Workflow Runner](workflow-runner.md)
8686
11. [Pre/Post Run Workflow Plugin](pre-post-run-workflow-plugin.md)
8787
12. [Pre/Post Run Task Plugin](pre-post-run-task-plugin.md)
88-
13. [Module Location Resolver](module-location-resolver.md)
88+
13. [Task Handler Location Resolver](task-handler-location-resolver)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# TaskHandlerLocationResolver Plugin
2+
3+
The `TaskHandlerLocationResolver` plugin is designed to resolve the location of task handlers within the system.
4+
It provides a consistent way to locate task handlers, whether they are specified by absolute paths, relative paths, or module names.
5+
6+
The default implementation will look up in this order:
7+
8+
1. If this type is `package`, we looked up in the node_modules directory, otherwise we stop immediately.
9+
2. Resolve it directly if it's an absolute path.
10+
3. Resolve it from the current directory.
11+
4. Resolve it from the runner directory.
12+
5. Lookup in the custom tasks directory (default is tasks directory).
13+
14+
## Usage
15+
16+
To create a `TaskHandlerLocationResolver` plugin, you need to implement the `TaskHandlerLocationResolver` interface and register your resolver plugin.
17+
18+
### Example
19+
20+
Here is an example of a `TaskHandlerLocationResolver` plugin:
21+
22+
```typescript
23+
import { AbstractPlugin, TASK_HANDLER_LOCATION_RESOLVER_PLUGIN } from '@letrun/core';
24+
import { ParsedHandler } from '@letrun/common';
25+
import fs from 'node:fs';
26+
import path from 'node:path';
27+
28+
export default class ExampleTaskHandlerLocationResolver extends AbstractPlugin {
29+
readonly name = 'example';
30+
readonly type = TASK_HANDLER_LOCATION_RESOLVER_PLUGIN;
31+
32+
async resolveLocation(handler: ParsedHandler, throwsIfNotFound?: boolean) {
33+
if (fs.existsSync(handler.name)) {
34+
return handler.name;
35+
}
36+
37+
if (throwsIfNotFound) {
38+
throw new Error(`Module not found: ${handler.name}`);
39+
}
40+
41+
return null;
42+
}
43+
}
44+
```
45+
46+
### Registering the Plugin
47+
48+
To register the `TaskHandlerLocationResolver` plugin, place it in the `plugins` directory (or the directory specified in your configuration) and ensure it is loaded by the CLI tool.
49+
50+
### Output
51+
52+
For the example above, the resolver checks if the task handler exists at the specified location or within the tasks directory.
53+
If the handler is not found and `throwsIfNotFound` is set to `true`, an error is thrown.
54+
55+
## Summary
56+
57+
The `TaskHandlerLocationResolver` plugin provides a standardized way to resolve task handler locations within the system.
58+
By implementing this interface, you can customize the module resolution logic to fit your specific needs.

docs/task/task-handler.md

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can write custom tasks either in a simple JS file or a node package:
4545
- A node package that exports a default class that implements the `TaskHandler` interface.
4646
- The entry point should be defined in the `main` field in `package.json`.
4747
- The package should be published to npm or a private registry.
48-
- We recommend you name the package with `letrun-task-` prefix.
48+
- We recommend you use scoped package and name the package with `@letrun-task-` prefix.
4949
- You can install the package using `letrun task install <package-name>` command.
5050
- We also support you place the package in the `tasks` directory manually.
5151

@@ -57,8 +57,6 @@ A task handler should have the following structure:
5757
- `parameters`: An object that describes the input parameters of the task for showing help.
5858
- `handle`: The function that executes the task.
5959

60-
We support grouping tasks by placing them in subdirectories, the group name will be the directory name.
61-
6260
This is an example of a custom task:
6361

6462
```ts
@@ -142,3 +140,77 @@ The output of the workflow will be: `HELLO, WORLD!`.
142140
> If you write custom task in TypeScript, you need to compile and bundle it to JavaScript before using.
143141
144142
To show all available custom tasks, you can use the `letrun task list -c` command.
143+
144+
## Task Group
145+
146+
A task group is a collection of tasks that are grouped together.
147+
It is useful when you have a set of tasks that are related to each other.
148+
149+
Currently, we support grouping tasks by placing them in subdirectories or multiple exported classes in a node package.
150+
151+
### Subdirectory
152+
153+
You can group tasks by placing them in subdirectories. The group name will be the directory name.
154+
The only one level of nesting is supported, so you can't nest a group inside another group.
155+
156+
Here is an example of a task group:
157+
158+
```
159+
tasks/
160+
└── my-group
161+
├── task1.js
162+
└── task2.js
163+
```
164+
165+
The task's handler will follow this format `script:group-name/script-file`.
166+
For example, the handler for `task1.js` will be `script:my-group/task1.js`.
167+
168+
> This approach is limited to simple tasks.
169+
> If you need to use external libraries, we recommend you create a node package.
170+
171+
### Node Package
172+
173+
You can group tasks by exporting multiple classes in a node package.
174+
The group name will be the package name.
175+
176+
Here is an example of a task group:
177+
178+
**package.json**
179+
180+
```json
181+
{
182+
"name": "my-group",
183+
"version": "1.0.0",
184+
"main": "index.js"
185+
}
186+
```
187+
188+
**index.js**
189+
190+
```js
191+
export class Task1Handler {
192+
name = 'task1';
193+
handle() {
194+
console.log('Task 1');
195+
}
196+
}
197+
198+
export class Task2Handler {
199+
name = 'task2';
200+
handle() {
201+
console.log('Task 2');
202+
}
203+
}
204+
```
205+
206+
The task's handler will follow this format `package:package-name:task-name`.
207+
For example, the handler for `Task1Handler` will be `package:my-group:task1`.
208+
209+
The group information will be obtained from the package.json:
210+
211+
- `name`: The name of the task group.
212+
- `description`: A brief description of the task group.
213+
- `version`: The version of the task group.
214+
- `author`: The author of the task group.
215+
216+
> We recommend you use scoped package and name the package with `@letrun-task-` prefix.

docs/task/task-structure.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ This is an example of a task definition:
3535
}
3636
```
3737

38+
## Task Handler Format
39+
40+
A task handler is a string that represents the task type.
41+
It can be a built-in task or a custom task.
42+
The task handler should be defined in the `handler` field.
43+
44+
The handler should follow the format: `type:identify[:task-name]`.
45+
46+
- `type`: The type of the task, it can be:
47+
- `package`: A task handler from a node package which is published to npm.
48+
- `external`: A task handler from an external node package which is not published.
49+
- `script`: A task handler from a standalone script file.
50+
- `identify`: The identifier of the task handler, it can be:
51+
- The package name if the type is `package`. The version can be specified by using the `@` symbol, e.g. `@my-scope/[email protected]`.
52+
- The path to the node module if the type is `external`.
53+
- The path to the script file if the type is `script`.
54+
- `task-name`: The name of the task handler, this is optional if the identify isn't a group task package.
55+
56+
> If the handler is a path, it will treat as a script task handler.
57+
58+
This is some valid task handler examples:
59+
60+
```text
61+
package:@letrun-task/[email protected]:read
62+
package:example-task
63+
64+
external:./tasks/example-task
65+
script:./tasks/example-task.js
66+
```
67+
3868
## Nested Tasks
3969

4070
You can nest tasks inside other tasks by defining the nested tasks in the `tasks` field. This is useful when you want to group tasks together.

0 commit comments

Comments
 (0)