task()#
提醒:此 API 不再是推荐模式 - 导出你的任务。
¥Reminder: This API isn't the recommended pattern anymore - export your tasks.
定义任务系统内的任务。然后可以从命令行以及 series()、parallel() 和 lastRun() API 访问该任务。
¥Defines a task within the task system. The task can then be accessed from the command line and the series(), parallel(), and lastRun() APIs.
用法#
¥Usage
将命名函数注册为任务:
¥Register a named function as a task:
将匿名函数注册为任务:
¥Register an anonymous function as a task:
检索之前已注册的任务:
¥Retrieve a task that has been registered previously:
签名#
¥Signature
参数#
¥Parameters
如果未提供 taskName,则任务将由命名函数的 name 属性或用户定义的 displayName 属性引用。taskName 参数必须用于缺少 displayName 属性的匿名函数。
¥If the taskName is not provided, the task will be referenced by the name property of a named function or a user-defined displayName property. The taskName parameter must be used for anonymous functions missing a displayName property.
由于任何已注册的任务都可以从命令行运行,因此请避免在任务名称中使用空格。
¥Since any registered task can be run from the command line, avoid using spaces in task names.
| 参数 | type | note |
|---|---|---|
| taskName | string | 任务系统内任务函数的别名。使用 taskFunction 的命名函数时不需要。 |
| 任务函数 (必填) | function | 任务功能 或组合任务 - 由 series() 和 parallel() 生成。理想情况下是一个命名函数。可以附加 任务元数据 以向命令行提供额外信息。 |
返回#
¥Returns
注册任务时,不会返回任何内容。
¥When registering a task, nothing is returned.
当检索任务时,将返回注册为 taskName 的封装任务(不是原始函数)。封装的任务有一个 unwrap() 方法,该方法将返回原始函数。
¥When retrieving a task, a wrapped task (not the original function) registered as taskName will be returned. The wrapped task has an unwrap() method that will return the original function.
错误#
¥Errors
当注册缺少 taskName 且 taskFunction 为匿名的任务时,将抛出错误并显示消息 "必须指定任务名称"。
¥When registering a task where taskName is missing and taskFunction is anonymous, will throw an error with the message, "Task name must be specified".
任务元数据#
¥Task metadata
| property | type | note |
|---|---|---|
| name | string | 命名函数的特殊属性。用于注册任务。 注意: name 不可写;无法设置或更改。 |
| displayName | string | 当附加到 taskFunction 时,会为该任务创建一个别名。如果使用函数名称中不允许的字符,请使用此属性。 |
| description | string | 当附加到 taskFunction 时,提供在列出任务时由命令行打印的描述。 |
| flags | object | 当附加到 taskFunction 时,提供在列出任务时由命令行打印的标志。对象的键代表标志,值是它们的描述。 |