takin
Version:
Front end engineering base toolchain and scaffold
163 lines (162 loc) • 4.45 kB
TypeScript
import glob from 'fast-glob';
import prompts from 'prompts';
import * as utils from './deps';
/**
* 生成器回调函数
*/
export declare type GeneratorCallbackFunction = (generator: Generator) => Promise<void> | void;
/**
* 生成器支持的 callback 名称
*/
export declare type GeneratorCallbackNames = 'downloaded' | 'customized' | 'prompted' | 'written';
/**
* 生成器 callback 对象
*/
export declare type GeneratorCallbacks = {
[callback in GeneratorCallbackNames]?: GeneratorCallbackFunction;
};
/**
* 生成器选项(含 callback)
*/
export declare type GeneratorOptions = {
/**
* 模版地址, 支持 本地目录、Git、Npm、Tar 等
*/
from: string;
/**
* 目标目录地址
*/
to: string;
/**
* 基础目录, 默认为 process.cwd()
*/
baseDir?: string;
/**
* 模版解析选项默认值
*/
defaults?: Record<string, any>;
/**
* 问题列表, 用于获取用户选择
*/
questions?: prompts.PromptObject[];
/**
* 拷贝文件的 glob 配置
*/
globOptions?: glob.Options;
/**
* 自定义生成器文件名称, 默认为 custom-generator.js
*/
customGeneratorName?: string;
} & GeneratorCallbacks;
/**
* 生成器拷贝选项
*/
export interface GeneratorCopyOptions {
path: string;
context: Record<string, any>;
to: string;
}
export declare type GeneratorConstructor = typeof Generator;
/**
* 自定义生成器工厂函数
*/
export interface CustomGeneratorFactory {
(Generator: GeneratorConstructor): GeneratorConstructor;
}
export declare class Generator implements Required<GeneratorOptions> {
from: string;
to: string;
baseDir: string;
defaults: Record<string, any>;
questions: prompts.PromptObject<string>[];
globOptions: glob.Options;
customGeneratorName: string;
/**
* 用户选项结果
*/
answers: Record<string, any>;
/**
* 工具方法, 包含:
* - chalk
* - debug
* - execa
* - esbuild
* - fastGlob
* - fsExtra
* - got
* - json5
* - jsoncParser
* - lodash
* - prompts
* - tapable
* - tarFs
* - zod
*/
utils: typeof utils;
/**
* 日志工具
*/
logger: import("./logger").Logger;
static run(options: GeneratorOptions): Promise<void>;
constructor(options: GeneratorOptions);
/**
* 下载完成后执行
* @param generator - 当前 generator 实例
*/
downloaded(generator: Generator): ReturnType<GeneratorCallbackFunction>;
/**
* 获取并初始化自定义 Generator 之后执行
* @param generator - 当前 generator 实例
*/
customized(generator: Generator): ReturnType<GeneratorCallbackFunction>;
/**
* 终端交互问答提示后执行
* @param generator - 当前 generator 实例
*/
prompted(generator: Generator): ReturnType<GeneratorCallbackFunction>;
/**
* 写入到目标文件夹之后执行
* @param generator - 当前 generator 实例
*/
written(generator: Generator): ReturnType<GeneratorCallbackFunction>;
/**
* 运行生成器, 步骤为
* 1. 下载模版, 完成后执行 downloaded 回调
* 2. 获取自定义生成器, 完成后执行 customized 回调
* 3. 获取问题并在终端交互, 完成后执行 prompted 回调
* 4. 写入模版到目标文件夹, 完成后执行 written 回调
*/
run(): Promise<void>;
/**
* 下载模版内容
* @returns 自定义 Generator 或 空
*/
downloading(): Promise<void>;
/**
* 尝试获取自定义生成器
* @returns 自定义生成器 或 空
*/
customizing(): Promise<Generator | void>;
/**
* 生成终端的 prompts 问题
*/
prompting(): Promise<void>;
/**
* 写入逻辑
*/
writing(): Promise<void>;
/**
* 拷贝模版文件
*
* 基于 lodash.template 方法来解析模版,并增加了对
* 1. <%_ _%> 的支持,可用于移除前后的空格或 Tab 符号
* 2. 增加了对 -%> 的支持,可用于移除控制语句引入的换行符
* @param opts - 拷贝选项
*/
copyTemplate(opts: GeneratorCopyOptions): Promise<void>;
/**
* 拷贝目录
* @param opts - 拷贝选项
*/
copyDirectory(opts: GeneratorCopyOptions): Promise<void>;
}