UNPKG

3.88 kBTypeScriptView Raw
1import { ILoggerProps } from './logger';
2import { DepBuilder } from './dep-builder';
3import { GlobalOptions, TaskContext } from './task-manager';
4interface OptionConfig {
5 default?: any;
6 type?: any[];
7}
8export declare type OptionDef = [string, string, OptionConfig | undefined];
9export declare type TaskFn<O, T = any> = (ctx: TaskContext<O>) => T | Promise<T>;
10export interface TaskDep<O = any> {
11 name: string;
12 /**
13 * Dependences are executed serially by default.
14 * If order doesn't matter and you want better performance via parallel, you can mark it as asynchronized.
15 * Asynchronized will run immediately whether there are synchronized tasks before them or not.
16 * You can pass a number as the priority of asynchronized tasks, bigger is formmer.
17 */
18 async?: boolean | number;
19 /**
20 * Whether rerun it when it occured in dependences tree more then once.
21 */
22 force?: boolean;
23 /**
24 * Parsed options
25 */
26 options?: O;
27 resolveOptions?: (ctx: TaskContext) => Promise<O> | O;
28 [k: string]: any;
29}
30export declare type Dependency = TaskDep | string | DepBuilder;
31export interface Task<O = any> extends TaskDep<O> {
32 namespaces: string[];
33 dependencies?: TaskDep[];
34 desc?: string;
35 fn?: (ctx: TaskContext<O>) => void | Promise<void>;
36 /**
37 * Raw arg strings
38 */
39 rawArgs: string[];
40 /**
41 * @description Whether task options only allow defined options, default false
42 * @default false
43 */
44 strict?: boolean;
45 options: O;
46 /**
47 * @description whether show loading
48 * @default globalOptions.loading
49 */
50 loading?: boolean;
51 /**
52 * @description whether log executed command
53 * @default globalOptions.logCommand
54 */
55 logCommand?: boolean;
56 logger?: ILoggerProps;
57}
58/**
59 * Set global options for all tasks.
60 * @param options
61 */
62export declare function setGlobalOptions(options: GlobalOptions): void;
63export declare const before: (fn: (t: Task) => void | Promise<void>) => void;
64export declare const after: (fn: (t: Task) => void | Promise<void>) => void;
65export declare const onerror: (fn: (err: Error, t: Task) => void | Promise<void>) => void;
66declare namespace TaskOptions {
67 let last: {
68 desc: string | undefined;
69 optionDefs: OptionDef[];
70 strict: boolean | undefined;
71 loading: boolean | undefined;
72 };
73 function empty(): {
74 desc: string | undefined;
75 optionDefs: OptionDef[];
76 strict: boolean | undefined;
77 loading: boolean | undefined;
78 };
79}
80/**
81 * Define task description
82 * @param desc
83 */
84export declare function desc(desc: string): void;
85/**
86 * Define a task cli option
87 * @param rawName
88 * @param description
89 * @param config
90 */
91export declare function option(rawName: string, description: string, config?: OptionConfig): void;
92/**
93 * Define task cli options are strict, which means it will throw an error if you passed undefined options.
94 */
95export declare function strict(): void;
96/**
97 * Set options for next task.
98 * @param options
99 */
100export declare function setOption(options: Partial<typeof TaskOptions.last>): void;
101export declare function task<O>(name: string, fn: TaskFn<O>): Task<O>;
102export declare function task<O>(name: string, dependencies: Dependency[], fn?: TaskFn<O>): Task<O>;
103/**
104 * Create namespace prefix for inner tasks
105 * @param ns namespace
106 * @param fn
107 * @example
108 * namespace('client', ns => {
109 * task('run', async ctx => {
110 * logger.log(ns) // 'client'
111 * await ctx.exec('<run cmd>')
112 * })
113 * })
114 * namespace('server', ns => {
115 * task('run', async ctx => {
116 * logger.log(ns) // 'server'
117 * await ctx.exec('<run cmd>')
118 * })
119 * })
120 *
121 * ==========
122 * $ yarn foy client:run
123 * $ yarn foy server:run
124 */
125export declare function namespace(ns: string, fn: (ns: string) => void): void;
126export {};