Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh: async dynamic generator actions #420

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ Property | Type | Default | Description
-------- | ---- | ------- | -----------
**description** | *[String]* | | short description of what this generator does
**prompts** | *Array[[InquirerQuestion](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/inquirer/README.md/#question)]* | | questions to ask the user
**actions** | *Array[[ActionConfig](#interface-actionconfig)]* | | actions to perform
**actions** | *Array[[ActionConfig](#interface-actionconfig)] / Function[[DynamicActionsFunction](#interface-dynamicactionsfunction)]* | | actions to perform

> If your list of actions needs to be dynamic, take a look at [using a dynamic actions array.](#using-a-dynamic-actions-array)

Expand Down Expand Up @@ -335,6 +335,24 @@ Property | Type | Default | Description

> Instead of an Action Object, a [function can also be used](#custom-action-function)

### *Interface* `DynamicActionsFunction`

The dynamic actions function is a function that accepts an *Answers* collection and returns *Array[[ActionConfig](#interface-actionconfig)]* or *Promise<Array[[ActionConfig](#interface-actionconfig)]>*.

**Example**

```js
plop.setGenerator('test', {
actions: async (answers)=>{
return Promise.resolve([
...ActionConfig...
...ActionConfig...
...ActionConfig...
])
}
});
```

## Other Methods
Method | Parameters | Returns | Description
------ | ---------- | ------- | -----------
Expand Down
2 changes: 1 addition & 1 deletion packages/node-plop/src/generator-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function (plopfileApi, flags) {

// if action is a function, run it to get our array of actions
if (typeof actions === "function") {
actions = actions(data);
actions = await actions(data);
}

// if actions are not defined... we cannot proceed.
Expand Down
3 changes: 2 additions & 1 deletion packages/node-plop/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export type PromptQuestion =
| InputQuestion;

export type DynamicPromptsFunction = (inquirer: Inquirer) => Promise<Answers>;
export type DynamicActionsFunction = (data?: Answers) => ActionType[];
export type DynamicActionsFunction = (data?: Answers) => ActionType[] | Promise<ActionType[]>;

export type Prompts = DynamicPromptsFunction | PromptQuestion[];
export type Actions = DynamicActionsFunction | ActionType[];
Expand Down Expand Up @@ -224,6 +224,7 @@ export interface ActionConfig {
abortOnFail?: boolean;
// eslint-disable-next-line @typescript-eslint/ban-types
skip?: Function;
[_:string]: any
}

type TransformFn<T> = (
Expand Down
Loading