UNPKG

4.37 kBTypeScriptView Raw
1import inquirer = require('inquirer');
2// @types/globby doesn't export types for GlobOptions, so we have to work a little bit to extract them:
3// GlobOptions is the second parameter of the sync function, which can be extracted with the Parameters<T> type
4import { sync as _sync } from 'globby';
5type GlobOptions = Parameters<typeof _sync>[1];
6import { HelperDelegate as HelperFunction } from 'handlebars';
7
8interface NodePlopAPI {
9 getGenerator(name: string): PlopGenerator;
10 setGenerator(name: string, config: PlopGeneratorConfig): PlopGenerator;
11
12 setPrompt(name: string, prompt: inquirer.PromptModule): void;
13 setWelcomeMessage(message: string): void;
14 getWelcomeMessage(): string;
15 getGeneratorList(): { name: string; description: string }[];
16 setPartial(name: string, str: string): void;
17 getPartial(name: string): string;
18 getPartialList(): string[];
19 setHelper(name: string, fn: HelperFunction): void;
20 getHelper(name: string): Function;
21 getHelperList(): string[];
22 setActionType(name: string, fn: CustomActionFunction): void;
23 getActionType(name: string): ActionType;
24 getActionTypeList(): string[];
25
26 setPlopfilePath(filePath: string): void;
27 getPlopfilePath(): string;
28 getDestBasePath(): string;
29
30 // plop.load functionality
31 load(target: string[] | string, loadCfg: PlopCfg, includeOverride: boolean): void;
32 setDefaultInclude(inc: object): void;
33 getDefaultInclude(): object;
34
35 renderString(template: string, data: any): string; //set to any matching handlebars declaration
36
37 // passthroughs for backward compatibility
38 addPrompt(name: string, prompt: inquirer.PromptModule): void;
39 addPartial(name: string, str: string): void;
40 addHelper(name: string, fn: Function): void;
41}
42
43interface PlopActionHooksFailures {
44 type: string;
45 path: string;
46 error: string;
47 message: string;
48}
49
50interface PlopActionHooksChanges {
51 type: string;
52 path: string;
53}
54
55interface PlopActionHooks {
56 onComment?: (msg: string) => void;
57 onSuccess?: (change: PlopActionHooksChanges) => void;
58 onFailure?: (failure: PlopActionHooksFailures) => void;
59}
60
61export interface PlopGeneratorConfig {
62 description: string;
63 prompts: Prompts;
64 actions: Actions;
65}
66
67export interface PlopGenerator extends PlopGeneratorConfig {
68 runPrompts: (bypassArr?: string[]) => Promise<any>;
69 runActions: (
70 answers: any,
71 hooks?: PlopActionHooks
72 ) => Promise<{
73 changes: PlopActionHooksChanges[];
74 failures: PlopActionHooksFailures[];
75 }>;
76}
77
78export type PromptQuestion = inquirer.Question
79 | inquirer.CheckboxQuestion
80 | inquirer.ListQuestion
81 | inquirer.ExpandQuestion
82 | inquirer.ConfirmQuestion
83 | inquirer.EditorQuestion
84 | inquirer.RawListQuestion
85 | inquirer.PasswordQuestion
86 | inquirer.NumberQuestion
87 | inquirer.InputQuestion;
88
89export type DynamicPromptsFunction = (inquirer: inquirer.Inquirer) => Promise<inquirer.Answers>;
90export type DynamicActionsFunction = (data?: inquirer.Answers) => ActionType[];
91
92export type Prompts = DynamicPromptsFunction | PromptQuestion[]
93export type Actions = DynamicActionsFunction | ActionType[];
94
95export type CustomActionFunction = (
96 answers: object,
97 config?: ActionConfig,
98 plopfileApi?: NodePlopAPI
99) => Promise<string> | string; // Check return type?
100
101export type ActionType =
102 | ActionConfig
103 | AddActionConfig
104 | AddManyActionConfig
105 | ModifyActionConfig
106 | AppendActionConfig
107 | CustomActionFunction;
108
109export interface ActionConfig {
110 type: string;
111 force?: boolean;
112 data?: object;
113 abortOnFail?: boolean;
114}
115
116export interface AddActionConfig extends ActionConfig {
117 type: 'add';
118 path: string;
119 template: string;
120 templateFile: string;
121 skipIfExists: boolean;
122}
123
124export interface AddManyActionConfig extends Pick<AddActionConfig, Exclude<keyof AddActionConfig, 'type'>> {
125 type: 'addMany';
126 destination: string;
127 base: string;
128 templateFiles: string;
129 globOptions: GlobOptions;
130 verbose: boolean;
131}
132
133export interface ModifyActionConfig extends ActionConfig {
134 type: 'modify';
135 path: string;
136 pattern: string | RegExp;
137 template: string;
138 templateFile: string;
139}
140
141export interface AppendActionConfig extends ActionConfig {
142 type: 'append';
143 path: string;
144 pattern: string | RegExp;
145 unique: boolean;
146 separator: string;
147 template: string;
148 templateFile: string;
149}
150
151export interface PlopCfg {
152 force: boolean;
153 destBasePath: string;
154}
155
156declare function nodePlop(plopfilePath: string, plopCfg?: PlopCfg): NodePlopAPI;
157export default nodePlop;