From b2f4dc375579e46eef16ce2c39152f86692baad2 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Tue, 12 Mar 2024 23:52:12 -0700 Subject: [PATCH] wip --- README.md | 20 +++++++++++++++++++- packages/node-plop/src/generator-runner.js | 2 +- packages/node-plop/types/index.d.ts | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e5c5b68..b46f6d9 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,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) @@ -299,6 +299,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*. + +**Example** + +```js +plop.setGenerator('test', { + actions: async (answers)=>{ + return Promise.resolve([ + ...ActionConfig... + ...ActionConfig... + ...ActionConfig... + ]) + } +}); +``` + ## Other Methods Method | Parameters | Returns | Description ------ | ---------- | ------- | ----------- diff --git a/packages/node-plop/src/generator-runner.js b/packages/node-plop/src/generator-runner.js index 961978e..2f73050 100644 --- a/packages/node-plop/src/generator-runner.js +++ b/packages/node-plop/src/generator-runner.js @@ -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. diff --git a/packages/node-plop/types/index.d.ts b/packages/node-plop/types/index.d.ts index 377f6ac..8f85a8a 100644 --- a/packages/node-plop/types/index.d.ts +++ b/packages/node-plop/types/index.d.ts @@ -154,7 +154,7 @@ export type PromptQuestion = | InputQuestion; export type DynamicPromptsFunction = (inquirer: Inquirer) => Promise; -export type DynamicActionsFunction = (data?: Answers) => ActionType[]; +export type DynamicActionsFunction = (data?: Answers) => ActionType[] | Promise; export type Prompts = DynamicPromptsFunction | PromptQuestion[]; export type Actions = DynamicActionsFunction | ActionType[]; @@ -224,6 +224,7 @@ export interface ActionConfig { abortOnFail?: boolean; // eslint-disable-next-line @typescript-eslint/ban-types skip?: Function; + [_:string]: any } type TransformFn = (